Inheritance in C++

Inheritance is a OOP feature which helps in software reuse. New classes can be created by building them upon existing classes.

1. The original class, from which we derive one or more other classes, is called a base class. A class, which is derived from another class, is called a derived class.

2. A class may be derived from one or more base classes. Also, a single class can be used as a base class to derive any number of classes.

3. In general, a derived class has more features than the base class. That is, in a sense, a derived class describes an object that is more specific that than one described by its base class. Hence, in general, a base class represents a superset of objects represented by a class derived from it (which is why, in some languages, base classes are known as superclasses, and derived classes as subclasses).

4. In general. public and protected member variables and member functions of a base class become member variables and member functions of its derived classes.

5. e.g.

——-

// File : point.h

#ifndef POINT_H

#define POINT_H

class Point

{

friend ostream& operator << (ostream&, const Point& );

public:

Point (float a = 0.0, float b = 0.0);

void setPoint (float, float);

float getX ( ) const;

float getY ( ) const;

~Point ( );

protected:

float x, y;

};

#endif

——–

// File : point.cpp

#include “point.h”

#include < iostream.h>

Point : : Point (float a, float b)

{

setPoint (a, b);

cout << “Point created : [ ” << a << “,” << b <<“] \n”;

}

void Point : : setPoint (float x_val, float y_val)

{

x = x_val;

y = y_val;

}

float Point : : getX ( ) const

{

return x;

}

float Point : : getY ( ) const

{

return y;

}

Point : : ~ Point ( )

{

cout << “Destructor for : [” << x << “,” << y <<“] \n”;

}

ostream& operator << (ostream& output, const Point& p )

{

output << “Printing point : [ ” << p.x << “, ” << p.y << ” ]” << endl;

return output;

}

#endif

——

// File : circle.h

#ifndef CIRCLE_H

#define CIRCLE_H

class Circle : public Point

{

friend ostream& operator << (ostream&, const Circle&);

public:

Circle (float r = 0.0, float a = 0.0, float b = 0.0);

float getRadius ( ) const;

float area ( ) const;

~Circle ( );

protected:

float radius;

};

#endif

——–

// File : circle.cpp

#include “circle.h”

#include < iostream.h>

Circle : : Circle ( float r, float a, float b)

: Point (a, b)

{

radius = r;

cout << ” Circle created : ” << r << ” : [” << a << “,” << b <<“] \n”;

}

float Circle : : area ( ) const

{

return 3.14159 * radius * radius;

}

float Circle : : getRadius ( ) const

{

return radius;

}

Circle : : ~ Circle ( )

{

cout << ” Destructor for : ” << radius << “: [” << x << y << “] \n”;

}

ostream& operator << (ostream& output, const Circle& c)

{

output << “Printing Circle : ” << c.radius << ” : [ ” << c.x << “, ” << c.y << ” ]” << endl;

}

#endif

——

// program to demonstrate order of invocation of constructors/ destructors.

// File prog1.cpp

#include < iostream.h>

#include “point.h”

#include “circle.h”

main ( )

{

{

Point p1 (2.0, 5.0);

}

cout << endl;

Circle c1 (5.0, 1.0, 3.0);

Circle c2 (10.0, 2.0, 4.0);

cout << endl;

return 0;

}

——-

The output of prog1.cpp would be something like:

Point created : [2.0,5.0]

Destructor for : [2.0,5.0]

Point created : [1.0,3.0]

Circle created : 5.0 : [1.0,3.0]

Point created : [2.0,4.0]

Circle created : 10.0 : [2.0,4.0]

Destructor for : 10.0 : [2.0,4.0]

Destructor for : [2.0,4.0]

Destructor for : 5.0 : [1.0,3.0]

Destructor for : [1.0,3.0]

——

6. Order of invocation of constructors/ destructors:

  • When object of derived class is created, constructors of base class is invoked first, followed by constructor of derived class.
  • When object of derived class is created, and it is derived from multiple base classes, the constructors of the base classes are invoked first (in the order in which the base classes are specified in the inheritance statement in the derived class’s definition, followed by constructor of derived class.
  • Destructors are invoked in the reverse order of invocation of constructors.

e.g.

class D: public B1, public B2

{

};

When an object of class D is created:

  • Constructor for B1 is invoked first.
  • This is followed by constructor for B2.
  • This is followed by constructor for D.

At the time of destruction:

  • Destructor for D is invoked.
  • This is followed by destructor for B2.
  • This is followed by destructor for B1.

——

7. Derived class object can be treated as a base class object. h

8. Consider the following declarations:

—-

Point p1(1.0, 3.0), *pointPtr;

Circle c1(12.0, 1.5, 2.5), *circlePtr;

—–

Now, the statements,

pointPtr = &p1;

cout << *pointPtr;

will lead to the output:

Printing Point : [ 1.0, 3.0 ]

—–

The statements,

circlePtr = &c1;

cout << *circlePtr;

will lead to the output:

Printing Circle : 12.0 : [ 1.5, 2.5 ]

—-

The statements,

pointPtr = &c1;

cout << *pointPtr;

will lead to the output:

Printing Point : [1.5, 2.5 ]

—-

The statements,

pointPtr = &c1;

circlePtr = (Circle *) pointPtr;

cout << circlePtr;

will lead to the output :

Printing Circle : 12.0 : [ 1.5, 2.5 ]

—–

9. Public, private and protected inheritance: A derived class may be derived from a base class via public, private or protected inheritance. Public inheritance is most commonly used.

10. A class D derived publicly from class B, is defined as follows:

class D: public B

{

};

When class D is derived from class B via public inheritance:

– public members of B become public members of D.

– protected members of B become protected members of D.

– private members of B do not become members of D.

– An object of class D can be thought of as an object of base class B.

11. A class D can be derived privately from class B as follows:

class D: private B

{

};

When class D derives from B via private inheritance:

– public and protected members of B become private members of D.

– private members of B do not become members of D.

12. A class D deriving from B via protected inheritance is defined as follows:

class D: protected B

{

};

When D derives from B via protected inheritance:

– public and protected members of B become protected members of D.

– private members of B do not become members of D.

13. Multiple inheritance: A class can derive from multiple base classes.

e.g. A class D deriving from classes A, B and C could be defined as follows:

class A : public A, public B, public C

{

} ;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: