Changing member data in a derived class

If I have a virtual function in a derived class how do use it to change a member variable of the base class.  I'm using an example that I've used recently on this site.  Its basically a hierachy of computers, with the PC class being the base and Compaq's, Dells etc being the derived ones.  I have function that needs to perform an update of the PC components.  The components are basically base class pointers to seperate pc compoents.  Therefore the overloaded upgraded function accepts a pointer for a new component as a paremeter and then needs to change the existing component pointer to that of the new component.  when I try and access the pointer which is a private member variable of the base PC class I use a function that returns the pointer I need to update.  Do I have to create another base member function called (swapPointer()) or something just to update the member variable or is their something else I can do.....

Please help

Janusz
jstabikAsked:
Who is Participating?
 
mnashadkaConnect With a Mentor Commented:
It may be smarter to make the data members in the base class protected rather than private.  That way any class that's derived from the base class (PC) can access and modify these data members, but any class or function that isn't derived from PC can't modify the data members.  That's what I usually do if it's an option.  Hope this helps.
0
 
mrwad99Commented:
Difficult to say without seeing code; please post yours !
0
 
leflonCommented:
hi janusz,

i am not sure i understand what you do with the pointer and the funcs, but if your upgrade function is in the derived class, you can not access the private data members. you have to use a base class function to do this.

but i agree with mrwad99, some code would be really nice.

leflon
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
jstabikAuthor Commented:
Heres a go at supplying the code, though I think I may have sorted the problem out but have come accross a stumbling block.

Base Class
----------------------------------------

class cPC
{
public:
      cPC(cCPU*, cCDRom*, cHardDrive*, cMemory*, cMotherBoard*, cVideoCard*);
      virtual ~cPC();
      void identify();
      cCPU * upgrade(cCPU*);
      cMemory * upgrade(cMemory*);
      cCDRom * upgrade(cCDRom*);
      cHardDrive * upgrade(cHardDrive*);
      cMotherBoard * upgrade(cMotherBoard*);
      cVideoCard * upgrade(cVideoCard*);
      bool validateChip();
      virtual cMemory* testF();
      
private:
      cCPU *pCPU;
      cCDRom *pCDRom;
      cHardDrive *pHDrive;
      cMemory *pMemory;
      cMotherBoard *pMother;
      cVideoCard *pVideo;
};

Base Class CPP
-------------------------------------------------
cMemory* cPC::testF()
{
      cMemory * omem = new cMemory(500,1,2,"Crucial","SpeedMem");
      return omem;
}

Derived Class
------------------------------------------------
class cPcCompaqPresario : public cPcCompaq
{
      public:
            cPcCompaqPresario(cCPU*, cCDRom*, cHardDrive*, cMemory*, cMotherBoard*, cVideoCard*);
            cMemory* testF();
            
      private:
            
};

Derived Class CPP
-------------------------------------------------
cMemory* cPcCompaqPresario::testF()
{
      cMemory * Mem2 = new cMemory(500,1,2,"Crucial","SpeedMem");
      cout <<"testf" << endl;
      return Mem2;
}



I've cut out all of the irrelevant code.  The problem now being that I'm getting a link error with the virtual function - testF.  I stopped working with the upgrade function I mentioned earlier as I thought it may just be a typo, so I created the testF function and removed the functionality.  If the function returns a data type such as an int or string there is no error.  But in changing the return type to a pointer I get the following error:

pcCompaq.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall cPC::testF(void)" (?testF@cPC@@UAEXXZ)
Debug/Inheritance.exe : fatal error LNK1120: 1 unresolved external

Whu am I getting this error????

HELP!!!!
0
 
mrwad99Commented:
This is because you are not defining the body for testF() in all of the base classes.

Post the code in main() that uses the above then we can give a clearer explanation.
0
 
mnashadkaCommented:
It looks like the return type for the version of testF is void, but the testF that you're showing us has a return type of cMemory*.  There's probably a typo somewhere there.
0
 
itsmeandnobodyelseCommented:
You have a a virtual prototype of function testF in your base class, but the implementation is missing.

You may do this in in your base class header:

        virtual cMemory* testF() { return NULL; }

Then, you have an implementation and it should compile.

Regards, Alex


0
 
mnashadkaCommented:
itsmeandnobodyelse, jstabik posted this code above:
cMemory* cPC::testF()
{
     cMemory * omem = new cMemory(500,1,2,"Crucial","SpeedMem");
     return omem;
}

This is the implementation of testF().  The issue is that somewhere he's declaring it as return void, but it's actually returning cMemory *.
0
 
mrwad99Commented:
I think that mnashadka has given the solution....
0
 
itsmeandnobodyelseCommented:
I think, that mnashadka is right, so he should get the points.

The link error states "virtual void cPC::testF is missing". Most likely the questioner changed the return type somewhen and there is still an object file or precompiled header referring to the old definition.

Regards, Alex
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.