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

Introduction

Object oriented programming should have other features besides the ability to bundle data members and methods and provide access specifiers. It should also have inheritance and polymorphism.

A class can be derived from from another class. Let's say we have a Person class. We can have an Employee class that is derived from it. The Employee class will inherit all the member data variables and the member functions from the Person class. Since an Employee class is derived from a Person class it is a Person class also. This is called upcasting. However a Person may not be an employee so we cannot downcast.

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

class  Person
{
    public:
    string firstName  ;
    string lastName ;
    string getName()
    {
        return ( firstName + " " + lastName ) ;
    }
};

class Employee : public Person
{
    public:
     string jobTitle ;
     void print()
     {
         cout << getName() << " " << jobTitle << endl ;
     }
};

int main()
{
    Employee e1  ;
    //Can access the base class variables
    e1.firstName = "Alan"  ;
    e1.lastName = "Turing"  ;
    e1.jobTitle = "Programmer" ;
    e1.print() ;
    Person p1 ;
    p1 = e1 ;
    //Can't do
    // e1 = p1 ;
    return 0 ;
}
In the above example we have a base class called "Person" and a derived class "Employee". An employee is a person but has more attributes; in this case "jobTitle". It can inherit the attributes and the member functions from the base class.
There is a new access specifier called "protected" . A protected data member can be seen by the derived class but not outside the derived class.

File: i2.cpp
class A1
{
    public:
     int x1 ;
    protected :
     int x2 ;
    private :
     int x3 ;
};

//B1 is derived from A1class

class B1 : public A1
{
    void method1()
    {
        x2 = 4 ;
        //A derived class can access
        //a protected member
        //Cannot access a private member of a base class
        //x3 = 5 ;

    }
};

int main()
{
    A1 A1Object ;
    B1 B1Object ;
    int c1 = A1Object.x1 ;
    //Access issue
    //c1 = A1Object.x2 ;
    //c1 = A1Object.x3 ;

    //ok x1 is public
    c1 = B1Object.x1 ;

    //Protected not accessible outside
    //c1 = B1Object.x2 ;

    //Private not accessible outside
    //c1 = B1Object.x3 ;
    return 0 ;
}
The "protected" scope is not visible outside the class but is visible to the derived class.

Exercise

1) Modify the firstName and lastName in the Person example above to be protected and "getName" to be public. Comment out the below 2 lines.
e1.firstName = "Alan" ; e1.lastName = "Turing" ;
Compile and run the program.

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

class  Person
{
    public:
    string firstName  ;
    string lastName ;
    string getName()
    {
        return ( firstName + " " + lastName ) ;
    }
};

class Employee : public Person
{
    public:
     string jobTitle ;
     void print()
     {
         cout << getName() << " " << jobTitle << endl ;
     }
};

int main()
{
    Employee e1  ;
    //Can access the base class variables
    e1.firstName = "Alan"  ;
    e1.lastName = "Turing"  ;
    e1.jobTitle = "Programmer" ;
    e1.print() ;
    Person p1 ;
    p1 = e1 ;
    //Can't do
    // e1 = p1 ;
    return 0 ;
}


















File: ex1.cpp