Inheritance
Inheritance
lect#22
Introduction
M AM M ALS
DO G S C ATS H U M AN S
L IO N S T IG E R S LEO PAR DS
Types of Inheritance
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance
Defining Derived classes
• Syntax:
class DerivedClassName : access-level BaseClassName
where
– access-level specifies the type of derivation
• private by default, or
• Public
• Protected
d.b=20;
d.mul();
d.display();
return 0;
}
Private inheritance
{cout<<“a=“<<a<<“\n”; } }
};
};
int main()
{
D d;
//d.get_ab(); //won’t work
d.mul();
//d.show_a(); //won’t work
d.display();
#include<conio.h>
class a
protected:
int x;
public:
void getdata(int m)
x=m;
void putdata()
{ cout<<x<<"\t";
};
class b:protected a
{ protected:
int y;
public:
void getdata(int n)
{ y=n;
a::getdata(5);
} void putdata()
{ cout<<y<<"\t";
a::putdata(); }
void sum()
{
int z=x+y;
cout<<z<<"\t"; } };
void sum()
{
int z=x+y;
cout<<z<<"\t";
}
};
int main()
{
b b1;
b1.getdata(8);
b1.putdata();
b1.sum();
getch();
}
Class B
Private Not Inheritable
Protected
Public
Private Private
Protected Protected
Public Public
Private
Protected
Public
Access Rights of Derived Classes
Type of Inheritance
C
Multilevel Inheritance
class result:public test
#include <iostream.h>
class student
{ { float total ;
protected: public:
int roll_no;
public: void display() ;
void get_no(int a) };
{ roll_no=a; }
void put_no() void result:: display()
{ cout<<roll_no; {
} total= sub1+sub2;
};
put_no(); //function of
class test : public student
{ class student
protected: put_marks();
float sub1,sub2; //function of class test
public: cout<<“total = “<<total;
void get_marks(float x, float y) }
{ sub1=x; sub2=y; }
void put_marks(void) int main()
{ cout<<“Marks in sub1”<<sub1; {
cout<<“Marks in sub2”<<sub2; result student1;
} student1.get_no(102);
}; student1.get_marks(80.0,98.5);
student1.display();
void result:: display()
{
total= sub1+sub2;
put_no(); //function of class student
put_marks(); //function of class test
cout<<“total = “<<total;
}
int main()
{
result student1;
student1.get_no(102);
student1.get_marks(80.0,98.5);
student1.display();
return 0;
}
Overriding and Overloading
Parent Class Functions
• When any class member function is called, the
following steps take place:
1. The compiler looks for a matching function in the class of
the object using the function name
2. If no match is found in this class, the compiler looks for a
matching function in the parent class
3. If no match is found in the parent class, the compiler
continues up the inheritance hierarchy, looking at the
parent of the parent, until the base class is reached
4. If no match is found in any class, an error message is issued
34
Ambiguity in Multiple Inheritance
• Can arise when two base classes contain a function of the
same name
Example:
#include<iostream>
class base1
{
public:
void disp()
{
cout << "Base1" <<endl;
}
};
class base2
{
int main()
public:
{
void disp() derived Dvar;
{ //Ambiguous function call
cout<< "Base2"<<endl; Dvar.disp();
return 0;
} }
};
class derived : public base1,public
base2
{
//Empty class
};
• Can be resolved
– By overriding the function in the derived class
Void disp()
{
base1::disp();
base2::disp();
}
• Ambiguity in Single Inheritance
class A
{ int main()
public:
{
void display()
{ B b;
cout<<“A\n”; b.display(); // invokes
} display() in B
}; return 0;
class B: public A }
{ In this case, the function in
public: the derived class overrides
void display() the inherited function.
{
cout<<“B\n”;
}
};
Can be resolved:
– By using the scope resolution operator
int main()
{
B b;
b.A::display();
b.B::display();
}
Hybrid Inheritance
Record
Marks Avg
Result
Virtual base class
Student
Test score
result
};
class sports: public virtual void result::display(void)
student {
{ protected : total= part1+part2+score;
float score; put_no();
public: put_marks();
void get_score(float s) put_score();
{ score=s; } cout<<“total score: “<<total;
void put_score() }
{ cout<<“sports wt:”<<score; int main()
} }; {
class result : public test , public result student1;
sports student1.get_no(100);
{ float total; student1.get_marks(50.5,65.2);
public: student1.get_score(10.5);
void display(void); student1.display();
}; return 0;
}
Abstract Class
• An abstract class is one that is not used to create
objects.
• An abstract class is designed only to act as a base
class i.e. to be inherited by other classes.
• It is a design concept in program development and
provides a base upon which other classes may be
built.
Constructors and Inheritance
OUTPUT:
Beta constructed
Alpha constructed
Gamma constructed
Display Numbers:
x=4
p=2.5
q=5
u=2
v=4