Inheritance: Extending Classes in C++
What is Inheritance ?
One of the most important concepts of OOP is inheritance.
Inheritance allows us to define a class in terms of another class, which makes
it easier to create and maintain an application.
A class has its own property
and the property of its existing class called inheritance. The existing class
is called base class and new class is
called derived class.
Base and Derived Class
A class can be derived from more than one class. To define a
derived class, we use a class derivation list names one or more base classes
and have the form:
class
derived-class: access-specifier base-class
Where access specifier is one of the public , protected and
private.
For ex. consider base class is shape and its derived class
rectangle-
#include<iostream.h>
class shape
{
protected :
int width;
int height;
public:
void setwi(int w)
{
width=w;
}
void seth(int h)
{
height=h;
}
};
class rectangle: public shape
//Derived class
{
public:
int area()
{
return (width*height);
}
};
void main()
{
rectangle rect;
rect.setwi(5);
rect.seth(7);
cout<<”Total area:”<<rect.area()<<endl;
}
Output:
Total area:35
Access control and inheritance
A derived class can access all the non-private members of
its base class. We can summarize the difference access type-
Access
|
public
|
protected
|
private
|
Same Class
|
yes
|
yes
|
yes
|
Derived Class
|
yes
|
yes
|
no
|
Outside Class
|
yes
|
no
|
no
|
Type of inheritance
In C++, there is five different type of inheritance-
1.
Single inheritance
2.
Multiple inheritance
3.
Multilevel inheritance
4.
Hierarchical inheritance
5.
Hybrid inheritance(also known as virtual
inheritance)
1.Single inheritance
In this type of inheritance one derived class inherits from
only one base class. It is most simple form of inheritance.
Ex. class sub: public super
{
};
2. Multiple inheritance
In this type of inheritance a class derived from more than
one base class. In a multiple inheritance a derived class inherits all the
public and protected members of all the base class.
Eg. class D:public
A, public B, public C
{
};
3. Multilevel inheritance
In this type of inheritance the derived class inherits from
a class, which in turns inherits from some other class. The super class of one,
is sub class for the other.
· Visibility Modes
The public visibility mode
Public Derivation Of Class |
Private visibility mode
class super
{
};
class sub :private super
{
};
Private Derivation Of Class |
Protected visibility mode
Protected Derivation Of Class |
Class super
{
};
class sub :protected super
{
};
It is evident from above that “public” derivation inherits
public and protected members of the base class in their original form i.e.
public members remain public and protected member remain protected in derived
class.
The “private derivation inherits” inherits public and protected members
of the base class and makes them private ion the derived class.
The “protected derivation” also inherits public and protected members of
the base class and makes them protected in the derived class.
Q. Program to illustrate the access control in public derivation of a class.
Super class
Sub class
name-employee name-Manager
data-member-name, title
emp.no,basic
member-function-getdata(), getdata( ), putdata()
putdata(),getbasic()
#include<iostream.h>
#include<stdio.h>
class Emp {
private:
char name [25];
long enumb;
public :
void getdata()
{
cout<<”Enter Name”;
gets(name);
cout<<”Enter Employee Number:”;
cin>>enum;
}
void
putdata()
{
cout<<”Name:”<<name<<”\t”;
cout<<”Emp. Number:”<<enumb<<”\t”;
cout<<”Basic Salary:”<<basic;
}
protected:
float basic;
void getbasic()
{
cout<<”\n
Enter Basic:”;
cin>>basic;
}
};
class Manager: public Emp
{
Private:
char title[25];
public:
void getdata()
{
Emp::getdata();
getbasic();
cout<<”Enter Title:”;
gets(title);
cout<<”\n”;
}
void putdata()
{
Emp:putdata();
cout<<”\t Title:”<<title<<”\n”;
}
};
void main()
{
Manager m1;
cout<<”Manager1\n”;
m1.getdata();
cout<<”\n Manager Details\n”;
m1.putdata();
}
Output:
Manager1
Enter Name :Uma Mahadevan
Enter Employee Number :1112
Enter Basic :11800
Manager 1 Details
Name :Uma Mahadevan
Emp.Number :1112 Basic
Salary :11800 Title : Ms
This post is about Inheritance: Extending Classes in C++. In the next post we will discuss Data File Handling .
0 Comments