Inheritance
Inheritance
1. Single Inheritance: - Single Inheritance is method in which a derived class has only
one base class.
#include <iostream.h>
class Value
{
protected:
int val;
public:
void set_values (int a)
{ val=a;}
In example shows a };
Base class value and a class Cube: public Value
Derived class cube {
class value contain public:
Protected member such int cube()
As val { return (val*val*val); }
Protected access };
Specifier we can use in int main ()
Derived class. {
Private member can not Cube cub;
Be Inheriting. cub.set_values (5);
cout << "The Cube of 5 is::" << cub.cube() << endl;
return 0;
}
Result:
The Cube of 5 is:: 125
Inheritance 2
2. Multiple Inheritance:- You can derive a class from any number of base classes.
Deriving a class from more than one direct base class is called multiple inheritance.
Multiple inheritance enables a derived class to inherit members from more than one
parent.
Deriving directly from more than one class is usually called multiple inheritance.
Syntax:-
Class C: Public A, Public B
{
Body of D
Example
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm; // sm = Sports mark
public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
Inheritance 3
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
};
void main()
Output:
Enter the Roll no: 100
{ Enter two marks
clrscr();
statement obj; 90
obj.get(); 80
obj.getsm();
Enter the Sports Mark: 90
obj.display(); Roll No: 100
getch(); Total : 260
} Average: 86.66
Ambiguity: - In multiple inheritance the ambiguity arises when same method name is
being used by two derived class and further derivation from these two base classes.
To resolve this ambiguity we are using scope resolution operator.
class M
{ class P:public M,public N
public: {
void display() public:
{ void display(void)
cout<<"vimal \n"; {
} M::display();
}; N::display();
class N
{ }
public:
void display() };
{ int main()
cout<<"Vaiwala\n"; {
} clrscr();
}; P p;
p.display();
getch();
}
Output: Vimal
Vaiwala
Inheritance 4
3. Hierarchical Inheritance: - It is the inheritance hierarchy wherein multiple subclasses
inherit from one base class
Hierarchical Inheritance is a method of inheritance where one or more derived classes
is derived from common base class.
It is the process of deriving two or more classes from single base class. And in turn
each of the derived classes can further be inherited in the same way.
The base class will include all the features that are common to the subclasses. A
subclass can be constructed by inheriting the properties of the base class. A subclass can
serve as a base class for the lower level classes and so on.
Example:- Hierarchical classification of students in a university
Student
Example
class A class C:public B
{ {
public:
protected:
void showC()
int x;
{
public:
showA();
void showA()
showB();
{
cout<<"x*y ="<<x*y;
cout<<"enter a value for x:"<<endl;
}
cin>>x;
};
}
void main()
}; {
class B:public A C ob1;
{ ob1.showc();
protected:
}
int y;
public:
void showB() Output
{ Enter value of x = 12
cout<<"enter value for y:"; Enter value of y = 3
cin>>y; X * Y =36
}
};
5. Hybrid Inheritance The inheritance hierarchy that reflects any legal combination of
other four types of inheritance.
There could be situations where we need to apply two or more types of inheritance to
design a program.
"Hybrid Inheritance" is a method where one or more types of inheritance are combined together
and used.
Inheritance 6
In figure base class A and class B and C are derived from class A.
So that path is called Hierarchical Inheritance.
Class B and C are the base class for class D. so that path is called
multiple inheritance. because there are more then one base class.
Example:-
class stu
{
protected:
int rno;
public:
void get_no(int a)
{
rno=a;
}
void put_no(void)
{
out<<"Roll no"<<rno<<"\n";
Inheritance 7
}
};
class test:public stu
{
protected:
float part1,part2;
public:
void get_mark(float x,float y)
{
part1=x;
part2=y;
}
void put_marks()
{
cout<<"Marks obtained:"<<"part1="<<part1<<"\n"<<"part2="<<part2<<"\n";
}
};
class sports
{
protected:
float score;
public:
void getscore(float s) int main()
{ {
score=s; clrscr();
} result stu;
void putscore(void) stu.get_no(123);
{ stu.get_mark(27.5,33.0);
cout<<"sports:"<<score<<"\n"; stu.getscore(6.0);
stu.display();
} return 0;
}; }
class result: public test, public sports
{
float total;
public:
void display(void); OUTPUT
};
void result::display(void) Roll no 123
{ Marks obtained : part1=27.5
total=part1+part2+score; Part2=33
put_no(); Sports=6
put_marks(); Total score = 66.5
Inheritance 8
putscore();
cout<<"Total Score="<<total<<"\n";
}
};
~derived()
{
cout << "Destroying derived class...:\n";
}
void showj() Output:-
{ Constructing base...:
cout << j << "\n"; i = 20
} Constructing derived class...:
}; j = 10
int main() 20
{ 10
clrscr(); Destroying derived class...:
derived ob(10, 20); Destroying base ...:
ob.showi();
Inheritance 10
ob.showj();
getch();
return 0;
}
} Student
Class B1 : virtual public A //parent 1 As Virtual base class As Virtual base class
{
}
Class B2 : public virtual A //parent 2 Test Sports
{
}
Class C : public B1, public B2 //child
{ Result
//Only one copy of A
//will be inherited
} Virtual Base Class
Note: - The Keyword virtual and public
may be use in either order.
Example
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number =a;
}
void put_number(void)
{
out<<"Roll no"<< roll_number <<"\n";
}
};
class test: virtual public student
{
protected:
float part1,part2;
public:
Inheritance 12
void get_mark(float x,float y)
{
part1=x;
part2=y;
}
void put_marks()
{
cout<<"Marks obtained:"<<"part1="<<part1<<"\n"<<"part2="<<part2<<"\n";
}
};
class sports:public virtual student
{
protected:
float score;
public:
void get_score(float s) int main()
{ {
score=s; clrscr();
} result stu;
void put_score(void) stu.get_number(123);
{ stu.get_mark(27.5,33.0);
cout<<"sports:"<<score<<"\n"; stu.get_score(6.0);
stu.display();
} return 0;
}; }
class result: public test, public sports
{
float total;
public:
void display(void); OUTPUT
};
void result::display(void) Roll no 123
{ Marks obtained : part1=27.5
total=part1+part2+score; Part2=33
put_number(); Sports=6
put_marks(); Total score = 66.5
put_score();
cout<<"Total Score="<<total<<"\n";
}
Q:-4 Explain This Pointer
C++ uses unique keyword called this to represent an object that invokes a member
function. this is a pointer that points to the object for which this function was called.
This unique pointer is automatically passed to a member function when it is called. The
pointer this acts as an implicit argument to all the member functions.
Inheritance 13
class abc
{ The Private variable ‘a’ can be used directly inside a member
int a; function, like a=123;
public: We can also use the following statement to do same job:
void display() thisa=123;
{ Since C++ permits the use of shorthand form a=123, we have not
this->a=123; been using pointer this explicitly so far. However, we have been
cout<<a; implicitly using the pointer this when overloading the operators using
} member function.
};
Output:=
main()
123
{
clrscr();
abc a;
a.display();
getch();
}
int main () {
clrscr();
CRectangle rect;
CTriangle trgl;
CPolygon * ppoly1 = ▭
CPolygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}
Q:-6 Explain Virtual Function With example
In a virtual function or virtual method is a whose behavior can be
When we use the same function name in both base and derived classes, the function in
base class is declared as virtual using keyword virtual preceding its normal declaration.
When a function is made virtual, C++ determines which function to use at run time
based by making the based on the type of object pointer to by base pointer, rather than the
type pointer. Thus, by making the base pointer to point to different objects, we can
execute different versions of the virtual function.
class Base
{ int main ()
public: {
void display() clrscr();
{ Base B;
cout<<"\n Display base"; Derived D;
} Base *bptr;
virtual void show() cout<<"\n bptr points to base \n";
{ bptr=&B;
cout<<"\n show base"; bptr -> display();//call base version
} bptr -> show();//call base version
}; cout<<"\n\n bptr points to derived \n";
class Derived: public Base bptr=&D;
Output
bptr -> display();//calls base version
bptrbptr
points
-> to base
show();//calls Derived version
return 0;
Display
} base
Show base
Inheritance 15
{
public:
void display()
{
cout<<"\n Display Derived";
}
void show()
{
cout<<"\n show derived";
}
};