Hi,

I'm having trouble understanding how I should use pure virtual functions in my inheritance tree.

I've drawn a class diagram which shows what I'm trying to achieve:

[img]http://i42.tinypic.com/2qjdttd.jpg[/img]

Here is what I've written:

main.cpp

#include <iostream>
#include <string>
#include <list>
#include "base.h"
using namespace std;

int main()
{
	list<GrandParent*>theList;

	GrandParent *ptr = new Parent("a","b");
	theList.push_back( ptr );

	GrandParent *ptr1 = new Child("a","b","c");
	theList.push_back( ptr1 );

	system ("pause");
	return 0;
}

base.h

#ifndef BASE_H
#define BASE_H

using namespace std;
#include <string>

/********************************************************
	Abstract base/grandparent class AIRCRAFT
********************************************************/
class GrandParent
{
public:
	//constructor
	GrandParent(string name);

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

	//Pure virtual function for concrete Parent class
	virtual string getAddress() = 0;

	//Pure virtual function for ParentAbstract class
	virtual string getTelephone() = 0;

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

private:

protected:

	string _name;
};

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

/********************************************************
	Concrete parent class
********************************************************/
class Parent : public GrandParent
{
public:
	//constructor
	Parent(string name, string address);

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

private:

protected:

	string _address;
};
//constructor
Parent::Parent(string name, string address) : GrandParent(name)
{
	_address = address;
}

/********************************************************
	Abstract parent class
********************************************************/
class ParentAbstract : public GrandParent
{
public:
	//constructor
	ParentAbstract(string name, string telephone);

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

private:

protected:

	string _telephone;

};

//constructor
ParentAbstract::ParentAbstract(string name, string telephone) : GrandParent(name)
{
	_telephone = telephone;
}

/********************************************************
	Concrete child class
********************************************************/
class Child : public ParentAbstract
{
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) : ParentAbstract(name, telephone)
{
	_DOB = dob;
}

/*********************************************************
and two other concrete child class (not in this code), bit of the
same level of inheritance
*********************************************************/
#endif

Since I'm storing base class pointers in my list, I'm using GrandParent as the interface. This is definitely what i want to do.

But since I need to instantiate both Parent and Child, my current use of the pure virtual functions is causing 'cannot build abstract class' compile errors.

What is the normal procedure for dealing with an inheritance structure like mine?

Thanks very much for your help, it really is most appreciated :)

aPure virtual function needs to be defined in the derived class so

you parent needs to have a function called DOB etc that is the same signitaure as the virtual method.

class base1
{
publc:
base1();
virtual int get() {return 0;}
virtual int get_int() = 0;
}

class test1 : public base1
{
public: 
test1();
//must define virtual functions if = 0
virtual int get_int() {return 4;}
}

That doesn't make sense. Why do you derive parent from a GrandParents.
Its like saying a parent "is a" GrandParents. Where we know thats not true. A GrandParents, has a child. A parent has a child. That suggests composition, not inheritance. And why do you create a Abstract parent class and a non-abstract parent class. The name,
GrandParent and Parent, is centered around the child. You need to rethink the design and the name. What are you trying to accomplish exactly?

commented: appreciated +1

>>aPure virtual function needs to be defined in the derived class
Not true. It does not have to be defined.

>>aPure virtual function needs to be defined in the derived class
Not true. It does not have to be defined.

Apologies :$, I thought that the compiler complained you left out the definition completely.

Hi thanks for replying.

I'm trying to instantiate Parent (the concrete version) and Child and store pointers to those objects in the std::list.

I know the names are slightly offputting, in my real program they are:

Vehicle : [B]Red car[/B]
         Blue car : [B]Blue Car 3speed[/B]
                  : [B]Blue Car 4speed[/B]
                  : [B]Blue Car5speed[/B]

Bold indicates a concrete class

>>'cannot build abstract class' compile errors.

If thats the error, then what its probably trying to tell you is that
the class you tried to instantiate is an abstract class. If it is not mean to
be then check that you override the proper function with the exact signature.

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.