One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
Inheritance
is the capability of one class to acquire properties and characters from
another class whose properties are inherited by other class is called a PARENT , BASE , SUPER class and the
class which inherits by other class is called CHILD , DERIVED ,SUB class .
inheritance makes the code reusable when we inherit an existing class all its
method and fields become available in the new class hence code is reused.
Base & Derived Classes
A class can be derived from more than one classes, which means it
can inherit data and functions from multiple base classes. To define a derived
class, we use a class derivation list to specify the base class(es). A class
derivation list namesone or more base classes and has the form:
Where access-specifier is one of public, protected, or private,
and base-class is the name of a previously defined class. If the
access-specifier is not used, then it is private by default.
Access Control and Inheritance
A derived class can access all the non-private members of its base
class. Thus base-class members that should not be accessible to the member
functions of derived classes should be declared private in the base class. We
can summarize the different access types according to - who can access them, in
the following way:
Access |
Public |
Protected |
Private |
Same class |
Yes |
Yes |
Yes |
Derived classes |
Yes |
Yes |
No |
Outside classes |
Yes |
No |
No |
A derived class inherits all base class methods with the following
exceptions:
·
Constructors,
destructors and copy constructors of the base class.
·
Overloaded
operators of the base class.
·
The friend
functions of the base class.
Example of access control
#include<iostream.h>
#include<conio.h>
class A{
protected:
void display(){
cout<<"Display\n";
}
};
class B:public A{
public:
void show(){
display();
cout<<"Show\n";
}
};
void main(){
clrscr();
B obj;
obj.show();
getch();
}
Output
Display
Show
Types of Inheritance
1. Single
Inheritance
2. Multilevel
Inheritance
3. Multiple
Inheritance
4. Hierarchical
Inheritance
5. Hybrid
Inheritance
6. Multipath
Inheritance
1. Single Inheritance
In
this type one derived class inherits from only one base class it is the simplest form of inheritance.
|
Class A{
}
Class B : public A{
};
#include<iostream.h>
#include<conio.h>
class A{
public:
void display(){
cout<<"Display\n";
}
};
class B:public A{
public:
void show(){
cout<<"Show\n";
}
};
void main(){
clrscr();
B obj;
obj.show();
obj.display();
getch();
}
Output
Show
Display
2. Multilevel Inheritance
In this type of inheritance the derived
class inherits from a class. which in turns from some other class the super
class for one is sub class for other class.
Stntax
–
class A{
};
};
public:
};
Example of multilevel inheritance
#include<iostream.h>
#include<conio.h>
class A{
public:
void display(){
cout<<"Display\n";
}
};
class B:public A{
public:
void show(){
cout<<"Show\n";
}
};
class C:public B{
public:
void print(){
cout<<"Print\n";
}
};
void main(){
clrscr();
C obj;
obj.show();
obj.display();
obj.print();
getch();
}
Output
Show
Display
3. Multiple Inheritance
In this type a single derived class may
inherit from two or more than two base class.
Syntax –
class A{
public:
class B{
};
class C : public A ,
public B{
public:
};
Example of multiple inheritance
#include<iostream.h>
#include<conio.h>
class A{
public:
void display(){
cout<<"Display\n";
}
};
class B{
public:
void show(){
cout<<"Show\n";
}
};
class C:public A,public B{
public:
void print(){
cout<<"Print\n";
}
};
void main(){
clrscr();
C obj;
obj.show();
obj.display();
obj.print();
getch();
}
Output
Show
Display
4. Hierarchical Inheritance
In this type of inheritance multiple derived
class inherits from a single base class.
Syntax
–
class A{
public:
};
class C : public A{
public:
};
Example of hierarchical inheritance
#include<iostream.h>
#include<conio.h>
class A{
public:
void display(){
cout<<"Display\n";
}
};
class B:public A{
public:
void show(){
cout<<"Show\n";
}
};
class C:public A{
public:
void print(){
cout<<"Print\n";
}
};
void main(){
clrscr();
B obj;
C obj1;
obj.display();
obj.show();
obj1.display();
obj1.print();
getch();
}
Output
Display
Show
Display
5. Hybrid Inheritance
Hybrid inheritance is a combination of
hierarchial and multilevel inheritance. (more
than one type inheritance)
Syntax
–
class A{
};
};
public:
};
class D : public B ,
public C{
public:
};
Or
};
};
public:
};
class D : public B , public C{
public:
};
Example of hybrid inheritance
#include<iostream.h>
#include<conio.h>
class A{
public:
void show(){
cout<<"Show\n";
}
};
class B:public A{
public:
void display(){
cout<<"Display\n";
}
};
class C{
public:
void test(){
cout<<"Test\n";
}
};
class D:public B,public C{
public:
void print(){
cout<<"Print\n";
}
};
void main(){
clrscr();
D obj;
obj.show();
obj.display();
obj.test();
obj.print();
getch();
}
Output
Show
Display
Test
6. Multipath Inheritance
Multipath inheritance is a combination of
hierarchial and multilevel inheritance. (more
than two type inheritance)
Syntax –
};
public:
};
class C : public virtual A{
public:
};
class D : public B , public C{
public:
};
Example of multipath inheritance
#include<iostream.h>
#include<conio.h>
class A{
public:
void show(){
cout<<"Show\n";
}
};
class B:public virtual A{
public:
void display(){
cout<<"Display\n";
}
};
class C:public virtual A{
public:
void test(){
cout<<"Test\n";
}
};
class D:public B,public C{
public:
void print(){
cout<<"Print\n";
}
};
void main(){
clrscr();
D obj;
obj.show();
obj.display();
obj.test();
obj.print();
getch();
}
Output
Show
Display
Test
Scope Resolution Operator {::} (Ambiguity)
Ambiguity can be resolved by using the
scope resolution operator to specify the class in which the member function.
Ambiguity in c++ occur when a derived class have two base classes and these two
base classes have one comma base class.
Example
of scope resolution operator
#include<iostream.h>
#include<conio.h>
class A{
public:
static void display(){
cout<<"Hello";
}
};
void main(){
clrscr();
A::display(); //scope resolution operator
getch();
}
Output
Hello
0 Comments