Solved

Changing member data in a derived class

Posted on 2004-03-28
13
219 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
  • 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 50 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
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.

 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

828 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