Hi all,
Looking for some direction on how to implement my class functions.
I drew a class diagram to show my inheritance structure:
http://i42.tinypic.com/2qjdttd.jpg
I'm trying to use an abstract base class as the programs interface.
In that interface I'm declaring all the getter functions as pure virtual functions, and then defining them in their respective classes.
My issue is that this is clashing with the inheritance structure, as not all the derived datamembers are shared by both of the concrete classes (I think the image describes this a bit better than I can).
I'm storing base class pointers in a std::list, so need to use the interface, but if don't declare all the getter functions are pure virtual, then I suffer from slicing.
Here is an example of my program (the class names are a little 'offputting', but the two concrete classes are Parent and Child). :
base.h :
main.cpp :Code:#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; } #endif
I think I'm suffering from 'can't see wood for the trees' syndrome.Code:#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; }
Can anyone advice me on how this situation is normally dealt with?
Thanks very much for looking![]()



LinkBack URL
About LinkBacks




