5 Day Inheritance C++

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Asterisc Computer Institute C++

C++ Inheritance
Inheritance in Object Oriented Programming can be described as
a process of creating new classes from existing classes.

New classes inherit some of the properties and behavior of the


existing classes. An existing class that is "parent" of a new class is
called a base class. New class that inherits properties of the base
class is called a derived class.

Inheritance is a technique of code reuse. It also provides


possibility to extend existing classes by creating derived classes.

Inheritance Syntax

The basic syntax of inheritance is

Class derived class_name: Access_Specifier Base class

Access specifier can be public, protected and private. The default


access specifier is private. Access specifiers affect accessibility of
data members of base class from the derived class. In addition, it
determines the accessibility of data members of base class outside
the derived class.

Types of Inheritance

In C++, we have 5 different types of Inheritance. Namely,

1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance (also known as Virtual Inheritance)
Asterisc Computer Institute C++

Single Inheritance
In this type of inheritance one derived class inherits from only one
base class. It is the most simplest form of Inheritance.

Multiple Inheritance
In this type of inheritance a single derived class may inherit from two
or more than two base classes.

Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base class.
Asterisc Computer Institute C++

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.

Hybrid (Virtual) Inheritance


Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance.
Asterisc Computer Institute C++

Single Inheritance

Single inheritance represents a form of inheritance when there is only one base class and

one derived class

#include <iostream>

using namespace std;

class A

public:

intx,y;

void assign()

cout<<"\nEnter value of x : ";

cin>>x;

cout<<"\nEnter value of y : ";

cin>>y;

}
Asterisc Computer Institute C++

void addition()

cout<<"\nAddition "<<x+y;

};

class B:public A

public:

void subtraction()

cout<<"\nSubtraction : "<<x-y;

};

int main()

B b;

cout<<"\nFor Addition : ";

b.assign();

b.addition();
Asterisc Computer Institute C++
cout<<"\nFor Subtraction : ";

b.assign();

b.subtraction();

return 0;

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>

using namespace std;

class A

public:

intx,y;

void assign()

cout<<"\nEnter value of x : ";

cin>>x;

cout<<"\nEnter value of y : ";

cin>>y;

void addition()

cout<<"\nAddition "<<x+y;
Asterisc Computer Institute C++
}

};

class B:public A

public:

void subtraction()

cout<<"\nSubtraction : "<<x-y;

};

class C:public B

public:

void multiplication()

cout<<"\nMultiplication : "<<x*y;

};

int main()

C c;

cout<<"\nFor Addition : ";

c.assign();

c.addition();
Asterisc Computer Institute C++

cout<<"\nFor Subtraction : ";

c.assign();

c.subtraction();

cout<<"\nFor multiplication : ";

c.assign();

c.multiplication();

return 0;

Multiple Inheritance
In this type of inheritance a single derived class may inherit from two or more than two
base classes.

#include <iostream>

using namespace std;

classStudDetail

public :

string name;

introllno;

voidacceptStudDetail()

cout<<"\nEnter Roll Number : ";

cin>>rollno;
Asterisc Computer Institute C++
cout<<"\nEnter Name : ";

cin>>name;

};

classExamDetail

public :

int m1,m2,m3,m4,m5;

voidacceptExamDetail()

cout<<"\nEnter Mark1 : ";

cin>>m1;

cout<<"\nEmter Mark2 : ";

cin>>m2;

cout<<"\nEmter Mark3 : ";

cin>>m3;

cout<<"\nEmter Mark4 : ";

cin>>m4;

cout<<"\nEmter Mark5 : ";

cin>>m5;

};
Asterisc Computer Institute C++
class Stud : public StudDetail,publicExamDetail

public:

int total;

voidcal()

total=m1+m2+m3+m4+m5;

void show()

cout<<"\nRoll Number : "<<rollno;

cout<<"\nName : "<<name;

cout<<"\nMark1 : "<<m1;

cout<<"\nMark2 : "<<m2;

cout<<"\nMark3 : "<<m3;

cout<<"\nMark4 : "<<m4;

cout<<"\nMark5 : "<<m5;

cout<<"\nTotal : "<<total;

};

int main()
Asterisc Computer Institute C++
{

Stud s;

s.acceptStudDetail();

s.acceptExamDetail();

s.cal();

cout<<"\n\n************** STUDENT DETAIL ****************";

s.show();

return 0;

Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base class.

#include <iostream>

using namespace std;

class A

public:

int x;

void accept()

cout<<"\nEnter a number : ";

cin>>x;

};
Asterisc Computer Institute C++

class B:public A

public:

voidfindSquere()

cout<<"\nSquere : "<<x*x;

};

class C:public A

public:

voidfindCube()

cout<<"\nCube : "<<x*x*x;

};

int main()

B b;

C c;

cout<<"\nForSque re : ";
Asterisc Computer Institute C++
b.accept();

b.findSquere();

cout<<"\n\nFor Cube : ";

c.accept();

c.findCube();

return 0;

Hybrid (Virtual) Inheritance


Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance.

#include <iostream>

using namespace std;

class mm

{
protected:
introllno;
public:
void get_num(int a)
{ rollno = a; }
void put_num()
{ cout<< "Roll Number Is:"<<rollno<< "\n"; }
}; class marks : public mm
{
protected:
int sub1;
int sub2;
public:
void get_marks(intx,int y)
{
sub1 = x;
sub2 = y;
}
void put_marks(void)
{
cout<< "Subject 1:" << sub1 << "\n";
cout<< "Subject 2:" << sub2 << "\n";
}
Asterisc Computer Institute C++
};
class extra
{
protected:
float e;
public:
void get_extra(float s)
{e=s;}
void put_extra(void)
{ cout<< "Extra Score::" << e << "\n";}
};
class res : public marks, public extra{
protected:
float tot;
public:
void disp(void)
{
tot = sub1+sub2+e;
put_num();
put_marks();
put_extra();
cout<< "Total:"<< tot;
}
};
int main()
{
res std1;
std1.get_num(10);
std1.get_marks(10,20);
std1.get_extra(33.12);
std1.disp();
return 0;
}

You might also like