Hi there,
I have a problem in that my derived concrete classes are loosing the data members which are part of my derived classes.
I have wrote a simple program to show the problem.
GrandParent.h :
#ifndef GRANDPARENT_H
#define GRANDPARENT_H
using namespace std;
#include <string>
/********************************************************
Abstract base class
********************************************************/
class GrandParent
{
public:
//constructor
GrandParent(string name);
//Getter
string getName() const { return _name; }
private:
protected:
string _name;
};
//constructor
GrandParent::GrandParent(string name)
{
_name = name;
}
/********************************************************
Concrete parent class
********************************************************/
class ConcreteParent : public GrandParent
{
public:
//constructor
ConcreteParent(string name, string address);
//Getter
string getAddress() { return _address; }
private:
protected:
string _address;
};
//constructor
ConcreteParent::ConcreteParent(string name, string address) : GrandParent(name)
{
_address = address;
}
/********************************************************
Abstract parent class
********************************************************/
class AbstractParent : public GrandParent
{
public:
//constructor
AbstractParent(string name, string telephone);
//Getter
string getTelephone() { return _telephone; }
////Pure virtual function for concrete Child class
virtual string getDOB() = 0;
private:
protected:
string _telephone;
};
//constructor
AbstractParent::AbstractParent(string name, string telephone) : GrandParent(name)
{
_telephone = telephone;
}
/********************************************************
Concrete child class
********************************************************/
class Child : public AbstractParent
{
public:
//constructor
Child(string name, string telephone, string dob);
//Getter
string getDOB() { return _dob; }
private:
protected:
string _dob;
};
//constructor
Child::Child(string name, string telephone, string dob) : AbstractParent(name, telephone)
{
_dob = dob;
}
#endif
main.cpp :
#include <iostream>
#include <string>
#include <list>
#include "GrandParent.h"
using namespace std;
int main()
{
list<GrandParent*>theList;
GrandParent *ptrA = new ConcreteParent("a","b");
theList.push_back( ptrA );
GrandParent *ptrB = new Child("a","b","c");
theList.push_back( ptrB );
system ("pause");
return 0;
}
As you can see, I'm storing base class pointers in a std::list.
But for some reason, only the datamember which is part of the GrandParent class has a value, the rest are missing.
Can anyone tell me how I can correct this problem?
Thanks very much! You help is most appreciated :)