• Now Online : 35
  • admin@codemyne.net

Introduction

Objectives:

  • What is Inheritance?
  • Types of Inheritance
  • Friend classes and Friend functions

Introduction to Inheritance:

The basic philosophy behind object orientation is to represent things as they exist in the real world. For example, unlike the procedural programming languages, C++ treats everything (well, almost everything) as objects. Like objects exist in the real world, objects in C++ contain properties (data members) and behavioral attributes (methods). Similarly, another concept has been implemented in C++ based on the real world inheritance.

What is inheritance?

Inheritance is the concept by which the properties of one entity are available to another. It allows new classes to be built from older and less specialized classes instead of being rewritten from scratch. Looking at the real life part of it, a child inherits his looks, mannerisms and qualities from his parents. In order to simulate this environment, C++ allows a class to inherit properties and functions from another.

The class that inherits properties and functions is called the subclass or the derived class and the class from which they are inherited is called the super class or the base class. The derived class inherits all the properties of the base class and can add properties and refinement of its own. The base class remains unchanged.

Base class: A
Data one
Data two
Data three
Data four
Derived class: B
Data five
Data six
Data seven

In the above diagram, B inherits from A which consists of data members one, two, three and four. All these members are inherited by B. Apart from this; B’s own data members include five, six and seven.

Any class can be base class. More than one class can be derived from a single base class and vice versa. A derived class can also act as a base class for another class.

Assuming that a class A already exists, the general format for inheriting class B from A is as follows:

    class B:public A
    {
        private:
        ….
        ….
        public:
            ….
    };
    

In the above declaration, the single (‘ : ’) is used to specify that the class being declared is derived from another class (the name of which is specified after the colon). The keyword ‘public’ follows the colon, and it is an access qualifier used to specify what kind of an inheritance it is. If it is a public inheritance, it means that the public members of the base class are brought into the derived class as public members.

However, there is one drawback here. In the previous section we had mentioned that private members of a class can’t be accessed outside the class definition. This holds true even for derived classes. Even though all the data members and methods of class A are derived by class B, the private data members of class A still remain private to B. The idea behind this is never to compromise on the concept encapsulation enforced by data hiding. For the sake of understanding, just assume that a derived class is given special privileges and private members are made accessible to derived classes alone. If this is done, the strong wall of encapsulation can be shattered simply by creating a dummy derived class.

On the other hand, the members of a base class can’t be declared private just so that they can be accessed in the derived class, for this beats the entire purpose of encapsulation, as they can be accessed anywhere from the program as well. For this purpose, C++ introduces another access specifier-protected.

The members of a class declared as protected can be accessed from the derived classes, but can’t be accessed from anywhere in other classes or the main program. In short, protected members behave like public members when it comes to derived classes, and private members with respect to the rest of the program.

Example:

//derived classes
#include<iostream.h>
class Cpolygon
{
    protected:
    int width, height;
    public:
    void set_values(int a, int b)
    {
    width=a;
    height=b;
    }
};
class CRectangle : public CPolygon  
{
    public:
    int area()
    {
        return(width*height);
    }
};
class CTriangle : public CPolygon
{
    public:
    int area()
    {
        return(width*height/2); 
    }
};
int main()
{
    CRectangle rect;
    CTriangle trgl;
    rect.set_values(4,5);
    trgl.set_values(4,5);
    cout<<rect.area()<<end1;
    cout<<trgl.area()<<end1;
    return 0;
}

Example: Constructors and Derived Classes

// constructors and derived classes
#include<iostream.h>
class mother
{
    public:
    mother()
    {
    cout<<”mother : no parameter \n”;
    }
    mother(int a)
    {
        cout<<”mother : int parameter \n”;
    }
};
class daughter : public mother
{
    public:
    daughter (int a)
    {
        cout<<”daughter : int parameter \n\n”;
    }
};
class son : public mother
{
    public:
    son(int a) : mother(a)
    {
        cout<<”son : int parameter\n\n”;
    }
};
int main()
{
    daughter cynthia(0);
    son daniel(0);
    return 0;
}

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment