- Get link
- X
- Other Apps
Inheritance in c++ programming language :
Inheritance in C++ programming language ->
Inheritance help us to derive characteristic from another class and us it is the one of the most important feature of object oriented programming
we called
Existing = old class = parent class = base class
New class = child class = Derived class
##Syntax - ##
class base_class{
..
};
// Visiblity_Mode - Private , Public ,Protected
class derived_class:Visiblity_Mode base_class
{
..
};
Type of inheritance -
1.Single
2.Multilevel
3.Multiple
4.Hierarchical
5.Hybrid
1.Single -
single inheritance means there are one parent class and one in child class by using inheritance syntax .
Example -
Let take there is a two class one is class A and one is class B in it class A was a parent class and class B was a child class so B inherit A .
syntax -
class first
{
..
};
class second:public first{
..
};
2. multilevel inheritance -
There are multilevel inheritance lets take example so there are three class A, B, C here B class is the parent of C class and A class is the parent of B class.
Example -
There are three class in a program class A , class B , class C so class A is parent class of class B and class B is a parent of derived class C this is multilevel inheritance
syntax -
class first
{
..
};
class second:public first{
..
};
class three:public second{
..
};
3.Multiple inheritance -
Multiple class are the base class of new class let explain it by syntax.
Example -
There are three class in a program class A1 and class A2 are both is a parent class of derived class B
Syntax -
class A1
{
..
};
class A2
{
..
};
class B:public A1,public A2
{
..
};
4.Hierarchical inheritance -
Two or more child class can access the parent class by using inheritance.
Example -
There are three class in a program class A ,class B1, class B2 A is parent class of both B1 and B2 so this is a hierarchical inheritance
Syntax -
class A1
{
..
};
class B1:public A
{
..
};
class B2:public A
{
..
};
5.Hybrid inheritance -
It is combination of more than one type of inheritance.
Syntax -
class First
{
..
};
class second:Public First
{
..
};
class Third
{
..
};
class fourth:public second,public Third
{
..
};
###Visibilty Modes in inheritance -->
// Visibility modes
#include <bits/stdc++.h>
using namespace std;
// Base class
class first {
public:
int x;
protected:
int y;
private:
int z;
};
// Derived class
class second : public first {
};
int main()
{
second obj;
cout<<"Enter the value"<<endl;
cin>>obj.x;
// x is public
// so its value will be printed
cout << obj.x << endl;
// y is protected
// so it will give visibility error
cout << obj.y << endl;
// z is not accessible from second
// so it will give visibility error
cout << obj.z << endl;
return 0;
};
##Public -
It can access directly because it is visible for all
##Private -
It cannot access to derived class because it is private member
##Protected -
It is only access by derived class
PROGRAM OF INHERITANCE -
#include <iostream>
using namespace std;
// Base class
class first
{
public:
int a;
};
// derived class
class second:public first
{
public:
int b;
};
int main()
{
second obj;
obj.b=5;
obj.a=70;
cout<<"second b "<<obj.b<<endl;
cout<<"first a "<<obj.a<<endl;
return 0;
}
output -->>
second b 5
one a 70
Comments
Post a Comment