Oops Using C++ Notes Unit - 3
Oops Using C++ Notes Unit - 3
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.
1.SINGLE INHERITANCE:one derived class inherits from only one baseclass.It is the most simplest form of
Inheritance.
//Base class
A
//Derivedclass
#include<iostream> B
Using namespacestd;
class A
{
public:
int a,b;
voidget()
{
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 1 2
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 subclass for the other.
#include<iostream.h>
class A
{
public:
int a,b;
voidget()
{
cout<<"Enter any two Integer values"<<endl;
cin>>a>>b;
}
};
Class B:public A
{
public:
int c;
void add()
{
c=a+b;
}
};
Class C:publicB
{
public:
void show()
{
cout<<a<<"+"<<b<<"="<<c;
}
};
int main()
{
C c;
c.get();
c.add();
c.show();
}
Output:
Enter any two Integer values 1214
12+14=26
3.MultipleInheritance:In this type of inheritance a single derived class may inherit from two or more than two
base classes.
A B
C
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();
}
OUTPUT:
Enter an Integer value12Enteran
Integervalue13
12+13=25
4.HybridInheritance:Hybrid inheritance is combination of two or more inheritances such as
single,multiple,multilevel or Hierarchical inheritances.
B C
D
#include<iostream.h>
class arithmetic
{
protected:
int num1,num2;
public:
Void get data()
{
cout<<"For Addition:"; cout<<"\nEnter the first number:"; cin>>num1;
cout<<"\nEnter these cond number:"; cin>>num2;
}
};
Class plus:public arithmetic
{
protected:
int sum;
public: Void add()
{
sum=num1+num2;
}
};
class minus
{
protected:
int n1,n2,diff;
public: void sub()
{
cout<<"\nFor Subtraction:"; cout<<"\n Enter the first number:"; cin>>n1;
cout<<"\nEnter these cond number:";
cin>>n2; diff=n1-n2;
}
};
Class result:public plus,public minus
{
public:
void display()
{
cout<<"\nSumof"<<num1<<"and"<<num2<<"="<<sum;
cout<<"\nDifference of "<<n1<<" and "<<n2<<"= "<<diff;
}
};
int main()
{
resultz;
z.getdata();
z.add();
z.sub();
z.display();
}
OUTPUT:
ForAddition:
Enter the first number: 1 Enter these cond number:2
For Subtraction:
Enter the first number: 3 Enter the second number:4
Sum of1 and 2= 3
Difference of 3 and 4=-1
5.Hierarchical Inheritance:-Inheriting is a method of inheritance where one or more derived classes is derived
from common base class.
B C D
#include<iostream.h>
class A //Base Class
{
public:
int a,b;
void get number()
{