Inheritance
by hkesavaraju on Jan.28, 2010, under Advanced Data Structures
Inheritance
Reusability is the important feature of OOPS. If reusing something that already exists rather than doing all over again. It saves time and money and also increases reliability.
Inheritance provides reusability by reusing the classes in C++. Once class is written and tested, it can be used by other programmer depending on the need. The mechanism of deriving new class from old class is called Inheritance. The old class is called base class and new class is called derived class.
Defining Derive class:
The derive class specifies a relationship with its base class in addition to its own details.
class derived-class-name: visibility-mode base-class-name
{
// members of derived class
}
The colon indicates derived-class-name is a derived from base-class-name. The visibility mode is optional. If it is not specified, it takes private by default. If specified, the possible modes are private, protected and public.
-
When base class is privately inherited by the derived class, the public members of base class become private members of the derived class and are accessed by only member functions of the derived class. They are not accessed by the objects of the derived class.
-
When the base class is protect inherited by the derived class, the public and protected members of the base class become protected members of the derived class and are accessed in the member functions of the derived class.
-
When derived class is publicly derived from base class, the public members of the base class should become public members of the derived class and are accessed by the objects of the derived class.
In any of these derivations, the private members of the base class are not inherited and these are not become members of the derived class.
Base class visibility
Derived class visibility
Public derivation
Protected derivation
Private derivation
private
Not inherited
Not inherited
Not inherited
protected
protected
protected
private
public
public
protected
private
fig: Visibility of inherited members
The derived class inherits some or all the properties of the base class. A class can also inherit properties from more than one class or even more than one level also.
There are five types of inheritances. They are as follows.
-
Single Inheritance
-
Multiple Inheritance
-
Hierarchical Inheritance
-
Multilevel Inheritance
-
Hybrid Inheritance
-
Single Inheritance: The derived class with only one base class is called Single Inheritance.
The following C++ program is an example for single inheritance. In this, base class B and derived class D were created and an object to derived class is created. The D is publicly derived from base class B. So, private members of B were not became members of D where as public members of B were became public members of D.
Code:
#include<iostream.h>
#include<conio.h>
class B
{
int a;
public:
int b;
v
oid get_ab();
i
class D
nt get_a();
void show_a();
}
int c
void get_ab()
int get_a()
void show_a()
void mul()
void display()
private:
;
class D:public B
{
i
public:
nt c;
public:
void mul();
void display();
};
void B::get_ab()
{
a=5;
b=10;
}
int B::get_a()
{
return a;
}
Public derivation adds more members to derived class
void B::show_a()
{
cout<<"a:"<<a<<endl;
}
void D::mul()
{
c=b*get_a();
}
void D::display()
{
cout<<"a:"<<get_a()<<endl;
cout<<"b:"<<b<<endl;
cout<<"c:"<<c<<"\n";
}
main()
{
clrscr();
D d;
d.get_ab();
d.mul();
d.display();
d.b=20;
d.mul();
d.display();
getch();
}
/* Output: a:5
b=10
c=50
a=5
b=20
c=100 */
b) Multiple Inheritance: A class can inherit attributes from two or more classes. This is known as Multiple Inheritance. Multiple Inheritance allows to combine the features of several existing classes as a starting point for defining new classes. An example to this is like a child inheriting intelligence from one parent and physical properties from another parent.
base-1
base-2
base-n
D
Multiple Inheritance
The syntax for this inheritance is as follows:
class D:visibility base-1,visibility base-2,………visibility base-n
{
. . . . .. .. . . . .. .
. .. .. . .. .. .. . . .. ( body)
};
where visibility may be either public or private. The base classes are separated by commas.
code:
#include<iostream.h>
#include<conio.h>
class M
{
int m;
public:
void get_m(int a)
{
m=a;
}
};
class N
{
int n;
public:
void get_n(int b)
{
n=b;
}
};
class MN:public M,public N
{
public:
void display()
{
cout<<"m is:"<<m<<"\n";
cout<<"n is:"<<n;
}
};
main()
{
clrscr();
MN ob;
ob.get_m(10);
ob.get_n(5);
ob.display();
getch();
}
/* Output:
m is: 10
n is: 5 */
There is a ambiguity problem in multiple inheritance when same function defined in both base and derived classes, the function in derived class overrides the function in base classes.
Example:
class M
{
public:
void display()
{
cout<<”class M \n”;
}
};
class N
{
public:
void display()
{
cout<<”class N \n”;
}
};
class P:public M,public N
{
public:
void display() // overrides display() of M & N
{
M::display();
}
};
To solve overriding in derived class ( ambiguity), a named instance within the derived class is defined using class and scope resolution operator as shown above.
Ambiguity cal also occur in single inheritance. Consider the following situation
class M
{
public:
void display()
{
cout<<”class M \n”;
}
};
class N: public M
{
public:
void display()
{
cout<<”class N \n”;
}
};
When display() of N class need to be accessed, the object of N with dot operator are used to access display of N. When display() of M need to be accessed, ambiguity occurs in N. To avoid, ambiguity, the object, scope resolution operator and its class are used. The following is an example.
main()
{
N b;
b.display(); // display() of N
b.M::display(); // display of M
b.N::display(); // display() of N
}
Output: class N
class M
class N
c) Multilevel Inheritance:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int r_no;
public:
void getr_no(int);
void putr_no();
};
void student::getr_no(int a)
{
r_no=a;
}
void student::putr_no()
{
cout<<" roll number is"<<r_no<<endl;
}
class test:public student
{
protected:
int m1,m2;
public:
void getmarks(float,float);
void putmarks();
};
void test::getmarks(float s1,float s2)
{
m1=s1;
m2=s2;
}
void test::putmarks()
{
cout<<"marks in m1"<<m1<<"\n";
cout<<"marks in m2"<<m2<<endl;
}
class result:public test
{
float t;
public:
void display();
};
void result::display()
{
t=m1+m2;
putr_no();
putmarks();
cout<<"total of m1 & m2 is"<<t;
}
main()
{
clrscr();
result r1;
r1.getr_no(532);
r1.getmarks(65.0,81.0);
r1.display();
getch();
}
/* Output: roll number is :532
Marks in m1: 65
Marks in m2: 81
Total of m1 & m2: 146 */