Home C++ Introduction Decisions Loops Input/Output Functions Stack and Heap References Arrays Searching and Sorting Recursion Pointers Character and Strings Structures Classes Inheritance Exceptions Templatess STL Modern C++ Misc Books ----

Inheritance


Contents

Abstract Class

Sometimes we don't want an instance of a class to be created. We can have a pure virtual function in the class without a body in that case.

File: a1.cpp
#include <iostream>
using namespace std ;


//Abstract Class
class  Person
{
    public:
    string firstName  ;
    string lastName ;
    //virtual
    virtual void getName() = 0 ;
    Person()
    {
      cout << "Person Constructor." << endl ;
    }

    virtual ~Person()
    {
      cout << "Person Destructor." << endl ;
    }

};

class Employee : public Person
{
    public:
     string jobTitle ;

     virtual
     void getName()
     {
         cout <<  firstName << " " <<  lastName <<
         " " << jobTitle << endl ;

     }

          Employee()
          {
            cout << "Employee Constructor." << endl ;
          }

         virtual ~Employee()
         {
           cout << "Employee Destructor." << endl ;
         }

};

class Manager : public Employee
{
    public:
     //What if the method is missing
     //Climb up the hierarchy
     virtual void getName()
     {
         cout <<  firstName << " " <<  lastName <<
         " " << jobTitle <<  " Manager" << endl ;

     }
          Manager()
          {
            cout << "Manager Constructor." << endl ;
          }

         virtual ~Manager()
         {
           cout << "Manager Destructor." << endl ;
         }
};


int main()
{
    Person* p1 ;
    Employee* e1  ;
    Manager* m1  ;

   // Compiler error
   // p1 = new Person() ;

    //Allowed
    e1 = new Employee() ;
    delete e1 ;


    return 0 ;
}
In the above code "Person" is an abstract class because it has a pure virtual function named "getName" . The derived class "Employee" implements this class and is not an abstract class.

If the pure virtual function is not implemented in the derived class then the derived class becomes abstract also.

File: a2.cpp

#include <iostream>
using namespace std ;


//Abstract Class
class  Person
{
    public:
    string firstName  ;
    string lastName ;
    //virtual
    virtual void getName() = 0 ;

    Person()
    {
      cout << "Person Constructor." << endl ;
    }

    virtual ~Person()
    {
      cout << "Person Destructor." << endl ;
    }

};

class Employee : public Person
{
    public:
     string jobTitle ;
     //if no implementation for getName
     //then this class is also abstract
     /*
     virtual
     void getName()
     {
         cout <<  firstName << " " <<  lastName <<
         " " << jobTitle << endl ;

     } */

          Employee()
          {
            cout << "Employee Constructor." << endl ;
          }

         virtual ~Employee()
         {
           cout << "Employee Destructor." << endl ;
         }

};

//This is not an abstract class
class Manager : public Employee
{
    public:

     virtual void getName()
     {
         cout <<  firstName << " " <<  lastName <<
         " " << jobTitle <<  " Manager" << endl ;

     }
          Manager()
          {
            cout << "Manager Constructor." << endl ;
          }

         virtual ~Manager()
         {
           cout << "Manager Destructor." << endl ;
         }
};


int main()
{
    Person* p1 ;
    Employee* e1  ;
    Manager* m1  ;

   // Compiler error
   // p1 = new Person() ;

    //Not Allowed
    //e1 = new Employee() ;
    //delete e1 ;

    //Allowed
    m1 = new Manager() ;
    delete m1 ;



   /*
    e1 = new Employee() ;
    e1->firstName = "Chuck" ;
    e1->lastName = "Wepner" ;
    e1->jobTitle = "Boxer" ;
    p1 = e1 ;
    //p1->getName() ;
    delete e1 ;

    cout << endl << endl ;
    m1 = new Manager() ;
    m1->firstName = "Chuck" ;
    m1->lastName = "Wepner" ;
    m1->jobTitle = "Boxer" ;
    p1 = m1 ;
    //e1->getName() ;
    //Make sure we delete the right type
    //or program will crash
    delete m1 ;
      */
    return 0 ;
}
Using pure and normal virtual functions a userful hierarchy can be implemented where the base class and the derived class work together. In the below program we have a "Vector" object that is able to call a base class method called "printElements" that in turn is able to call the derived class methods even though it has no idea of what the implmentations are at the time the class was written. In fact tomorrow if a new derived class from "Collection" then the method "printElements" will still work.

This area is part of the area that is called design patterns. A book on this topic is listed in the books section of this site.

File: a3.cpp

#include <iostream>
using namespace std ;


//Abstract Class
class  Collection
{
    public:


    virtual int  getSize() = 0 ;
    virtual int getElement( int index ) = 0 ;

    void printElements()
     {
          int size = getSize() ;
          for( int i1=0 ; i1 < size ; i1++ )
            {
              cout << getElement( i1 ) << " " ;
            }
          cout << endl ;
     }


};

class Vector : public Collection
{
    public:
      int getSize()
        {
            return 2 ;
        }


        int getElement( int index )
         {
          if ( index == 0 )
            return 100 ;
          else
            return 200 ;
      }



};


int main()
{

   Vector vectorObj ;
   vectorObj.printElements() ;




    return 0 ;
}