Inheritance
Inheritance
Introduction
Reusability--building new components by utilizing existing components-
is yet another important aspect of OO paradigm.
It is always good/ “productive” if we are able to reuse something that is
already exists rather than creating the same all over again.
This is achieve by creating new classes, reusing the properties of existing
classes.
It saves money , time, efforts etc.
Definition
• This mechanism of deriving a new class from existing/old
class is called “inheritance”.
• The old class is known as “base” class, “super” class or
“parent” class.
• The new class is known as “sub” class, “derived” class, or
“child” class.
• Example:
EMPLOYEE
• Syntax:
class DerivedClassName : access-level BaseClassName
where
– access-level specifies the type of derivation
• private by default, or
• Public
• Protected
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance
Examples
class ABC : private XYZ //private derivation
{
Members of ABC
};
Public
Private Private
Protected Protected
Public Public
Private
Protected
Public
Access Rights of Derived Classes
Type of Inheritance
C
Multilevel Inheritance
class test : public student
#include <iostream> {
using namespace std; protected:
class student float sub1,sub2;
{ public:
protected: void get_marks(float x, float y)
int roll_no; { sub1=x; sub2=y; }
public: void put_marks()
void get_no(int a) { cout<<“Marks in sub1”<<sub1<<endl;
{ roll_no=a; } cout<<“Marks in sub2”<<sub2<<endl;
}
void put_no()
};
{ class result:public test
cout<<“Roll number is”<<roll_no;
} { float total ;
}; public:
void 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 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
25
#include <iostream>
M N
P
class M class P:public M,public N
{ {
public: public:
void display() void show()
{
{
cout<<"in class M";
cout<<"in class P";
}
}; }
};
class N
{
public:
void display() int main()
{ {
cout<<"in class N"; P ob;
} ob.show();
}; ob.display();
return 0;
}
• Can be resolved
– By overriding the function in the derived class
int main()
{
P ob;
ob.M::display();
ob.N::display();
return 0;
}
• Ambiguity in Single Inheritance
int main()
class A {
{ B b;
public: b.display();
void display()
{ return 0;
cout<<“A\n”;
}
}
};
class B: public A
{
public:
void display()
{
cout<<“B\n”;
In this case, the function in
}
the derived class overrides
};
the inherited function.
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 Date
class student
{ Object of
{ Date class
private:
private:
int month, day, year;
int id_no;
public:
Constructor is Date dob;
Date(int m, int d, int y) called public:
{
student(int id, int d, int m, int y)
month = m; : dob(d,m,y)
day = d; {
year= y; id_no = id;
} }
}; };
Object Slicing
Constructor
Function of class A
called to perform
task
Object of class A
passed in constructor
Inheritance(egAggration concepts,
The const keyword)
• Const argument-
– Int student(const section *s);
• Const member function- the functions wont
change any value in the function. The compiler will
generate the error if functions tries to alter the data.
• Void mul(int, int) const; //declaration
• Void mul(int a, int v) const //definition
{
...;
}
class ABC
{
public:
int a; int main()
void func() {
{ ABC ob;
a=99; ob.func();
} cout<<ob.a;
void confunc() const ob.confunc();
{ cout<<ob.a;
a=100; //error cant modify a member. return 0;
} }
};
• Const object
syntax
const classname objectname;
eg: const student s1;
• Const object calls only const member functions.