Inheritance and pure virtual functions

This is a discussion on Inheritance and pure virtual functions within the C++ Programming forums, part of the General Programming Boards category; Hi all, Looking for some direction on how to implement my class functions. I drew a class diagram to show ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Inheritance and pure virtual functions

    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 :
    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
    main.cpp :
    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;
    }
    I think I'm suffering from 'can't see wood for the trees' syndrome.

    Can anyone advice me on how this situation is normally dealt with?

    Thanks very much for looking

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    I suggest that you read this article in PDF document format about the Liskov Substitution Principle.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    So are you trying the shotgun approach or simply ignoring the kind people who are helping you on another forum?
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Quote Originally Posted by laserlight View Post
    I suggest that you read this article in PDF document format about the Liskov Substitution Principle.
    Thanks laserlight, I've never heard of this before, and it seems to fit what I'm trying to do exactly.

    Appreciated, as was the help with my last thread. I have not replied to that thread as I have quite a few programs I am currently working on, and the advice will take me a good amount of time to go through, which I will as it introduces me to many new ideas.

    Quote Originally Posted by Prelude View Post
    So are you trying the shotgun approach or simply ignoring the kind people who are helping you on another forum?
    I am not ignoring anyone who helps me, that thread (thanks for mentioning it) ended up off topic by two people, and so I posted my question here. I did not post it hear first as I felt wrong in posting another when I had not replied to the last thread I made, for the reason stated.

    I think I've explained my issue with more clarity here, and so am hoping for some more easily understood advice.

    I always rep people who help me as I would here if it was possible, instead of bumping a thread just to say thanks.

    I appreciate anyones help 200%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 39
    Last Post: 11-30-2007, 01:16 AM
  2. Polymorphism, inheritance and containers
    By musicalhamster in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2007, 09:23 AM
  3. Inheritance, overriding, problems
    By CodeMonkey in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2007, 12:26 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Information Regarding Pure Virtual Functions
    By shiv_tech_quest in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2003, 03:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21