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 :)

Do not put executable code such as lines 27-30 in header files. If you want to make that inline then put it in the class declaration the way you did getName(). You have several functions mis-coded like that.

class GrandParent
{
public:
	//constructor
	GrandParent(string name) {_name = name;}

	//Getter
	string getName() const { return _name; }

private:

protected:

	string _name;
};
commented: Thanks for trying to help me! +1

Thanks for the advice.

I have followed what you suggested:

#ifndef GRANDPARENT_H
#define GRANDPARENT_H

using namespace std;
#include <string>

/********************************************************
	Abstract base class
********************************************************/
class GrandParent
{
public:
	//constructor
	GrandParent(string name) {_name = name;}

	//Getter
	string getName() const { return _name; }

private:

protected:

	string _name;
};


/********************************************************
	Concrete parent class
********************************************************/
class ConcreteParent : public GrandParent
{
public:
	//constructor
	ConcreteParent(string name, string address) : GrandParent(name) { _address = address;}

	//Getter
	string getAddress() { return _address; }

private:

protected:

	string _address;
};

/********************************************************
	Abstract parent class
********************************************************/
class AbstractParent : public GrandParent
{
public:
	//constructor
	AbstractParent(string name, string telephone) : GrandParent(name) { _telephone = telephone;}

	//Getter
	string getTelephone() { return _telephone; }

	////Pure virtual function for concrete Child class
	virtual string getDOB() = 0;

private:

protected:

	string _telephone;

};

/********************************************************
	Concrete child class
********************************************************/
class Child : public AbstractParent
{
public:
	//constructor
	Child(string name, string telephone, string dob) : AbstractParent(name, telephone) { _dob = dob;}

	//Getter
	string getDOB() { return _dob; }

private:

protected:

	string _dob;

};
#endif

Do you think there could be another reason as to why the slicing is occurring?

Using this code has made no difference and the inherited data members are still being sliced off.

I have solved it.

The base class needed it's member function to be declared as virtual.

This seems to have been the only problem.

commented: Glad you found the problem yourself :) More rewarding that way. +26
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.