?
Solved

Changing member data in a derived class

Posted on 2004-03-28
13
Medium Priority
?
233 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:jstabik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
13 Comments
 
LVL 19

Expert Comment

by:mrwad99
ID: 10698506
Difficult to say without seeing code; please post yours !
0
 
LVL 7

Expert Comment

by:leflon
ID: 10698634
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
 
LVL 8

Accepted Solution

by:
mnashadka earned 200 total points
ID: 10698747
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jstabik
ID: 10698753
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
 
LVL 19

Expert Comment

by:mrwad99
ID: 10699061
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
 
LVL 8

Expert Comment

by:mnashadka
ID: 10699072
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10699340
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
 
LVL 8

Expert Comment

by:mnashadka
ID: 10699350
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
 
LVL 19

Expert Comment

by:mrwad99
ID: 10947222
I think that mnashadka has given the solution....
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10947380
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question