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