Unit 3 Inheritance
Unit 3 Inheritance
INHERITANCE:
The mechanism of deriving a new class from an old one is called inheritance or derivation. The old class is
referred to as the base class and the new one is called the derived class or sub class. The derived class inherits
some or all of the traits from the base class. A class can also inherit properties from more than one class or
from more than one level. Reusability is an important feature of OOP. A derived class can be defined by
specifying its relationship with the base class in addition to its own details.
The colon indicates that the derived class name is derived from the base-class-name. the visibility mode is
optional and if present, may be either private or protected or public. The default is private. Visibility mode
specifies whether the features of the base class are privately derived or publicly derived.
When a base class is privately inherited by a derived class, public members of the base class can only be
accessed by the member functions of the derived class. Private members of base class are inaccessible to the
objects of the derived class
When a base class is protected inherited by a derived class, public members of the base class can only be
accessed by the member functions of the derived class. Private members of base class are inaccessible to the
objects of the derived class. If private members of base class are to be inherited to derived class then declare
them as protected. When the base class is publicly inherited, public members of the base class is publicly
inherited, public members of the base class become public members of the derived class and therefore they are
accessible to the objects of the derived class. In both the cases, the private members are not inherited and
therefore, the private members of a base class will never become the members of its derived class.
In inheritance, some of the base class data elements and member functions are inherited into the derived class.
We can add our own data and member functions and thus extend the functionality of the base class.
Inheritance, when used to modify and extend the capability of the existing classes, becomes a very powerful
tool for incremental program development
Types of Inheritance:
1.Single Inheritance
2.Multi level Inheritance
3.Mutiple Inheritance
4.Hybrid inheritance
5. Hierarchical Inheritance
1. SINGLE INHERITANCE: One derived class inherits from only one base class. It is the most simplest
form of Inheritance.
#include<iostream>
using namespace std;
class A
{
public:
int a,b;
void get()
{
cout<<"Enter any two Integer values"<<endl;
cin>>a>>b;
}
};
class B:public A
{
int c;
public:
void add()
{
c=a+b;
cout<<a<<"+"<<b<<"="<<c;
}
};
int main()
{
B b;
b.get();
b.add();
}
Output:
Enter any two Integer values
12
1+2=3
2.MULTILEVEL INHERITANCE: In this type of inheritance the derived class inherits from a
class, which in turn inherits from some other class. The Super class for one, is sub class for the other.
#include<iostream.h>
class A
{
public:
int a,b;
void get()
{
cout<<"Enter any two Integer values"<<endl;
cin>>a>>b;
}
};
class B:public A
{
public:
int c;
void add()
{
c=a+b;
}
};
class C:public B
{
public:
void show()
{
cout<<a<<"+"<<b<<"="<<c;
}
};
int main()
{
C c;
c.get();
c.add();
c.show();
}
Output:
Enter any two Integer values
12 14
12+14=26
3.Multiple Inheritance:In this type of inheritance a single derived class may inherit from two or more than
two base classes.
Syntax:
class D : visibility A, visibility B,….
{
………………
}
#include<iostream.h>
class A
{
public:
int a;
void getA()
{
cout<<"Enter an Integer value"<<endl;
cin>>a;
}
};
class B
{
public:
int b;
void getB()
{
cout<<"Enter an Integer value"<<endl;
cin>>b;
}
};
class C:public A,public B
{
public:
int c;
void add()
{
c=a+b;
cout<<a<<"+"<<b<<"="<<c<<endl;
}
};
int main()
{
C obj;
obj.getA();
obj.getB();
obj.add();
}
Enter an Integer
value 12 Enter an
Integer value 13
12+13=25
Pointers to Objects:Pointers to objects are useful for creating objects at run time. To access members
arrow operator ( ) and de referencing operator or indirection (*) are used.
Declaration of pointer:
className*ptr
ex:
item *obj;
Here obj is a pointer to object of type item.
class item
{
int code;
float price;
public:
void getdata(int a,float b)
{
code=a;
price=b;
}
void show()
{
cout<<”code:”<<code<<”\n”<<”Price:”<<price<<endl;
}
};
Above declaration creates memory space for an array of 10 objects of type item.
#include<iostream.h>
class item
{
int code;
float price;
public:
void getdata(int a,float b)
{
code=a;
price=b;
}
void show()
{
cout<<code<<"\t"<<price<<endl;
}
};
int main()
{
int n;
int cd;
float pri;
cout<<"Enter number of objects to be created:";
cin>>n;
item *ptr=new item[n];
item *p;
p=ptr;
for(int i=0;i<n;i++)
{
cout<<"Enter data for object"<<i+1;
cout<<"\nEnter Code:";cin>>cd;
cout<<"Enter price:";cin>>pri;
p->getdata(cd,pri);
p++;
}
p=ptr;
cout<<"Data in various objects are "<<endl;
cout<<"Sno\tCode\tPrice\n";
for(i=0;i<n;i++)
{
cout<<i+1<<"\t";
ptr->show();
ptr++;
}
return 0;
}
Pointers to Derived Classes: Pointers can be declared to derived class. It can be used to access
members of base class and derived class. A base class pointer can also be used to point to object of derived
class but it can access only members that are inherited from base class.
#include<iostream.h>
class base
{
public:
int a;
void get_a(int x)
{
a=x;
}
void display_a()
{
cout<<"In base"<<"\n"<<"a="<<a<<endl;
}
};
class derived:public base
{
int b;
public:
void get_ab(int x,int y)
{
a=x;
b=y;
}
void display_ab()
{
cout<<"In Derived "<<"\n"<<"a="<<a<<"\nb="<<b<<endl;
}
};
int main()
{
base b;
base *bptr;
bptr=&b;//points to the object of base class
bptr->get_a(100);
bptr->display_a();
derived d;
derived *dptr;
dptr=&d;//points to the object of derived class
dptr->get_a(400);
dptr->display_a();
dptr->get_ab(300,200);
dptr->display_ab();
bptr=&d;//points to the object of derived class
bptr->get_a(400);
bptr->display_a();
return 0;
}
Output:
In base
a=100
In base
a=400
In Derived
a=300
b=200
In base
a=400