1. Contents
Access Specifiers
Derived Classes
Type of Inheritance
Derived class Constructors (Constructors in single
Inheritance)
Multiple Inheritance
Constructors in multiple Inheritance
Constructors in single/multiple Inheritance with
arguments.
2. Inheritance
Inheritance is the second most important feature of OOP.
In inheritance, the code of existing classes is used for making new classes.
This saves time for writing and debugging the entire code for a new class.
To inherit means to receive.
In inheritance a new class is written such that it can access or use the members of
an existing class.
The new class that can access the members of an existing class is called the derived
class or child class. The existing class is called the base class or parent class.
3. Inheritance
The derived class can use the data members and member functions of the
base class.
It can also have its own data members and member functions.
Thus a derived class can even be larger than a base class.
4. Inheritance
The figure shows the relationship
between a derived class and the base
class.
The arrow is drawn from the derived
class to the base class.
The direction of the arrow indicates
that the derived class can access
members of the base class but base
class cannot access members of its
derived class.
In the figure shown on the left, the
derived class has only one member of
its own. The two members shown in
dotted lines are the members of the
base class. The derived class can also
access these two members of the base
class. Thus, whereas an object of the
base class can access only two
members, an object of the derived
class can access three members. A
new class can be derived from one or
more existing Classes.
5. Specifiers
Public [Already Discussed]
Private [Already Discussed]
Protected
The public members of a class are accessible by all functions in the program and
the private members of a class are accessible only by member functions and friend
functions of that class. Similarly, the protected members of a class are accessible
by the member functions and friend functions of that Class.
The protected members of a base class are, however, accessible by members of its
derived classes but the private members of the base class are not accessible
directly by members of its derived classes. This is the main difference between the
protected and the private access specifiers.
The protected members of a base class fall between private and public member.
These members are public for the derived class but for the rest of the program,
these are treated as private.
6. Defining Derived Class
The syntax for defining a derived class is slightly different from the syntax of the
base class definition. The declaration of a derived class also includes the name
of the base class from which it is derived. The general syntax for defining a
derived class is:
class sub_class_name : specifier
base_class_name{ ………………………………….
members of derived class
………………………………………}
Where
sub_class_name : represents name of the derived class.
: (colon) sets relation between the classes.
specifier represents the access specifier. It may be public, private or
Protected, base_class_name : represents name of the base class.
7. For example, a class "student" is
defined as:
class student {
private: Char name[15], address[15];
public:
void input (void);
void show(void);
};
8. Example Explanation
The class student has two data members and two member
functions. Suppose the marks obtained by a student in three
different subjects and the total marks of these subjects are to
be included as new data members in the above class. This is
done by adding new members in the class. There are two ways
in which these new members can be added to the class:
• Add new members in the original
• Define a new class that has the new members and that also
uses members of the existing "student" class. Using the
members of an existing class is the principle of inheritance.
The new class is the derived class. The existing class serves as
the base class for the derived class.
9. Defining Derived Class (cont…)
Driving a new class from the existing class reduces the size of the
program. It also eliminates duplication of code within the
program. For example, let the name of the new class be marks.
This class uses the members of the existing student class.
class marks : public student {
private:
int s1,s2,s3, total;
public:
void inputmarks(void);
void show_detail(void); };
10. Defining Derived Class (cont..)
The cIass marks is derived from the class student. The class marks is the
derived class. The class student is the base class.
The derived class has four data members of integer type and two member
functions.
It also uses the code of the base class student.
The derived class cannot directly access the private data members the base
class student by using the dot operator.
These members are only accessible to the derived class through the
interface functions within the base class.
11. Types of Inheritance
There are three kinds of inheritance.
• Public • Private • Protected
Public Inheritance
In Public Inheritance, the public members of the base class
become the public members of the derived class.
Thus the objects of the derived class can access public
members (both data and functions) of the base class.
Similarly, the protected data members of the base class also
become the protected members of derived class.
12. The general syntax for deriving a public class
from base class is:
class sub_class_name : public base_class_name {
…………………
…………………
) ;
where
public: specifies the public inheritance.
sub_class__name: represents name of the derived class.
base_class_name: represents name of the base class.
13. Example: Public Inheritance
#include<iostream>
using namespace std;
class A {
private:
int a1,a2;
protected:
int pa1,pa2;
public:
void ppp (void){
cout<<"Value of pal of class
A="<<pa1<<endl;
cout<<"Value of pa2 of class
A="<<pa2<<endl; }};
class B : public A {
public:
void get(void){
cout << "Enter value pal ? ";
cin>>pa1;
cout<<"Enter value pa2 ?";
cin>>pa2;}};
main (){
B ob;
ob.get();
ob.ppp();
};
14. Example Explanation
In the above program the class B is publicly derived from class
A. The object of the class B
Cannot access the private data member’s a1 & a2 of base class
A.
Can access the public function member ppp of base class A.
Can access the protected data members pa1 & pa2 of base
class A.
15. Private Inheritance
In private Inheritance the objects of the derived class cannot
access the public members of the base class. Its objects can only
access the protected data members of the base class.
The general syntax for deriving private class from base class is:
class sub_class_name : private base_class_name {
………………………………….}
Private: specifies private inheritance.
sub_class_name: represents name of the derived class.
base_class_name: represents name of the base class.
16. Example: Private Inheritance
#include<iostream>
using namespace std;
class A {
private:
int a1,a2;
protected:
int pa1,pa2;
public:
void ppp (void){
cout<<"Value of pa1 of class
A="<<pa1<<endl;
cout<<"Value of pa2 of class
A="<<pa2<<endl;}
};
class B : private A
{ public:
void get(void)
{cout<<"Enter value of pa1 ";
cin>>pa1;
cout<<"Enter value of pa2 =";
cin>>pa2;
cout<<"Value of pa1 of class
A="<<pa1<<endl;
cout<<"Value of pa2 of class
A="<<pa2<<endl;}};
main ( ){
B ob;
ob.get(); }
17. Example Explanation
In the above program, the class B is derived as private for the
base class A. The objects of class B:
cannot access the private data members a1 & a2 of base class
A.
cannot access the public function member ppp of base class A.
can only access the protected data members pal & pa2 of base
class A.
18. Protected Inheritance
The object of the class that is derived as protected can access
only the protected member of the base class. The general syntax
for deriving a protected Class from base class is:
class sub_class_name: protected base_class_name {
…………………
…………………
};
where protected: specifies protected inheritance.
sub_class_name: represents name of the derived class.
Base_class_name: represents name of the base class.
19. Example: Protected Inheritance
#include<iostream>
using namespace std;
class A
{
private :
int a1,a2;
protected:
int pa1,pa2;
public:
void get(void){
cout<<"value of pa1 of class A ";
cout<<"value of pa2 of class A ?";}};
class B: protected A{
public:
void get (void){
cout<<" Enter value of pa1=";
cin>>pa1;
cout<<"Value of pa2";
cin>>pa2;
cout<<"Value of pa1 of class
A="<<pa1<<endl;
cout<<"Value of pa2 of class
A="<<pa2<<endl;}};
main(){
B ob;
ob.get(); }
20. Example Explanation
In the above program, thc class B is derived as protected from the
base class A. The objects of class B:
can only access the protected data members pal &.pa2 of base
class A.
21. Derived Class Constructors
In inheritance, a constructor of the derived class as well the
constructor functions of the base class are automatically
executed when an object of the derived class is created.
The following example explains the concept of constructor
functions in single inheritance.
22. Example: Derived Class Constructors
#include<iostream>
using namespace std;
class bb{
public:
bb(void){
cout<<"Constructor of
base class"<<endl;
}
};
class dd:public bb{
public:
dd(void){
cout<<"Constructor of
derived class"<<endl;
}
};
main(){
dd abc;
}
23. Example Explanation
In the above program, when an object of the derived class dd
is created, the constructors of both the derived class as well as
the base class are executed.
24.
Multiple Inheritance
In Multiple inheritances, a class is derived by using more than
one classes.
In multiple inheritance the derived class receives the members
of two or more base classes.
Multiple inheritance is a powerful feature of Object Oriented
Programming.
This technique reduces the program size and save
programmer's. time. The programmer uses the existing base
classes in the program coding to solve problems.
The figure shown below illustrates the relation of derived class
with base classes.
26.
Multiple Inheritance
In the figure, Class C is derived from two base classes A & B. The syntax of
multiple inheritance is similar to that of single inheritance class. The name of
base classes are written separated by comma(,).
Syntax:
Class sub_class : sp1 b_class_1, sp2 b_class_2……..
{
………………………..
};
Where :
sp1 represents the access specifier of the first base class.
sp2 represents the access specifier of the second base class.
27. Example: Multiple Inheritance
#include<iostream>
using namespace std;
class student {
private:
char name[20],address[20];
public:
void input (void){
cout<<"Enter name:";cin>>name;
cout<<"Enter address:";cin>>address;
}
void print(void){
cout<<"Name:"<<name<<endl;
cout<<"Address:"<<address<<endl;
}
};
class marks {
private:
int s1,s2,total;
public:
void inputmarks(void){
cout<<"Enter marks of
S1=";cin>>s1;
cout<<"Enter marks of
S2=";cin>>s2;
total=s1+s2;}
void showmarks(void){
cout<<"Marks in s1="<<s1<<endl;
28. Example: Multiple Inheritance (Cont..)
cout<<"Marks in
s2="<<s2<<endl;
cout<<"Total
Marks="<<total<<endl;
}
};
class show: public student,
public marks{
public:
void showrec(){
cout<<"Record is"<<endl;
print();
showmarks();
}
};
main(){
show m;
m.input();
m.inputmarks();
m.showrec();
}
29. Constructor in Multiple Inheritance
When a class is derived from more than one base classes, the constructor of
the derived class as well as all of its base classes are executed when an
object of the derived class is created.
If the constructor functions have no parameters then first the constructor
functions of the base class are executed and then the constructor functions
of the derived class is executed.
30. Example: Constructor in Multiple Inheritance
#include<iostream>
using namespace std;
class aa{
public:
aa(){
cout<<"Class aa"<<endl;
}
};
class bb{
public:
bb(){
cout<<"Class bb"<<endl;
};
class cc:public aa,public bb{
public:
cc(){
cout<<"Class cc"<<endl;
}
};
main(){
cc o;
}
31. Example Explanation
In the above program, three classes are created.
The cc class is publicly derived from two class aa and bb.
All the classes have constructor functions are executed in the
following sequence.
1. Constructor of class aa is executed first.
2. Constructor of class bb is executed second.
3. Constructor of class cc is executed last.