The document discusses different types of inheritance in C++ including single, multiple, hierarchical, multilevel and hybrid inheritance. It provides examples of code implementing each type of inheritance with classes like Student, Marks, Sports etc. Key points covered are the syntax for declaring derived classes, accessing members of base classes, calling constructors and displaying output.
4. Base class to derived as
the derived class.
A
B C
D
(v) Hybrid inheritance
Class derived-class name : visibility base
class-name
{
----------------------------------
Member function
----------------------------------
}
5. The public data member and three public member
function.
B base class and D derived class.
The class D contains one private data member and two
public member function.
EXAMPLE PROGRAM:
#include<iostream.h >
#include<conio.h>
class student
{
public:
int rno;
6. char name[20];
void getdata()
}
Cout<<“Enter RollNo : -t”;
Cin>>rno;
Cout<<“enter name :-t”
Cin>>name;
}
};
class marks : public student
{
Public:
int m1,m2,m3,tot;
float per;
Void getmarks()
{
Getdata();
Cout<<“enter mark1:-t”;
Cin>>m1;
Cout<<“enter mark2:-t”;
Cin>>m2;
Cout<<“enter mark3:-t”;
Cin>>m3;
}
Void display()
{
Getmarks();
7. Cout<<“RollNo t namet
mark1 t mark2 t mark3 t
total t percentage”;
Cout<<rno<<“t”;<<name<<“
t”<<m1<<“t”<<m2<<“t”<<
m3<<“t”<<tot<<“t”<<per;
}
};
Void main()
{
Student std;
Clrscr();
Std.getmarks();
Std.display();
Getch();
}
The mechanism of the
derived class D.
Private derivation the
public members of the
base class become private
members of the derived
class.
8. The private data need from inherited by a derived
class, the private member making it public.
A member declared as protected is accessible by the
member function with
private : //optional.
--------------//visible to member function.
--------------//with in its class.
Protected:
---------------//write member function.
---------------//of its own and derived class.
Public:
---------------//write to all functions.
---------------//with in the program.
10. The access control to the
private and protected
member of a class. They
could be,
A functions that is a friend
of a class.
A member function of a
class that is a friend of a
class.
A member function of a
derived class.
The class A serve as a
base class for the
derived class B.
The class B is known
as intermediate base
class provide a link for
the inheritance
between A and C.
11. All users
Derived class
Member
function
Own member function
Friend function
class
public
protected
private
A
B
C
Base class Grand father
Intermediate base class father
Derived class child
12. #include<iostream.h>
#include<conio.h>
Class student {
Protected:
int rno,m1,m2;
Public:
void get() {
cout<<“enter the roll no:”;
Cin>>rno;
Cout<<“enter the mark1:”:
Cin>>m1;
Cout<<“enter the mark2:”;
Cin>>m2;
} };
Class sports {
Protected:
Int sm;
Public:
Void getsm() {
Cout<<“enter the sports mark:”;
Cin>>sm;
} };
Class statement: public student,
public sports {
Int tot,avg;
Public:
Void display() {
Tot =(m1+m2+sm);
Avg=tot/3;
14. A class can inherit the
attributes of two or more
classes.
This known as multiple
inheritance.
Syntax:
EXAMPLE PROGRAM:
#include<iostream.h>
using namespace std;
class A
{
public:
B-1 B-2 B-n
D
Class D: visibility B-1,visibility B-2 …..
{
--------------------------
-----(Body of D)-----
---------------------------
}
15. A() {
Cout<<“A ‘s constructor
called”<<end1; } };
Class B {
Public:
B() {
Cout<<“B ‘s constructor
called”<<end1; } };
Class C:public B,public A
{ public:
C() {
Cout<<“C ‘s constructor
called”<<end1; } };
Int main()
{
C c;
return 0;
}
Output:
B’s constructor called
A’s constructor called
C’s constructor called
16. One class may be
inherited by more than
one class.
This process is known as
hierarchical inheritance.
When more than one
classes are derived from
a single base class , such
inheritance is known as
hierarchical inheritance,
where feature that are
common in lower level are
included in parent class.
student
arts
medical
engineering
mech elec civil
17. Hybrid inheritance is also
called as virtual base
class.
A class is derived from
classes as in multiple
inheritance.
One of the parent
classes is not a base
class .It is a derived
class.
Multiple or multilevel type
of inheritance are used
to derive a class
.
EXAMPLE PROGRAM:
#include<bits/stdc++.h>
using namespace std;
class parent
{ public:
Int id_p; };
Class child : public parent
{
public :
int id_c;
};
Int main()