Inheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
Functions
(Reusability)
What Is Inheritance?
• Provides a way to create a new class from an existing class
• The new class is a specialized version of the existing class
• It makes it easier to create and maintain an application.
• It provides an opportunity to reuse the code functionality and fast
implementation time.
• Syntax:
int main()
OUTPUT:
{ Enter the Value of A=2
B b; Enter the Value of B=10
b.set_B(); Product of 2 * 10 = 20
b.cal_product();
return 0;
}
Forms of Inheritance
•Single Inheritance
•Multiple Inheritance
•Hierarchical Inheritance
•Multilevel Inheritance
•Hybrid Inheritance (also known as Virtual Inheritance)
Multilevel Inheritance
#include <iostream>
using namespace std;
class base //single base class
{
protected:
int x;
public:
void getdata()
{
cout << "Enter value of x= "; cin >> x;
}
};
Multilevel Inheritance
class derive1 : public base // derived class from base class
{
protected:
int y;
public:
void readdata()
{
cout << "\nEnter value of y= "; cin >> y;
}
};
Multilevel Inheritance
class derive2 : public derive1 // derived from class derive1
{
private:
int z;
public:
void indata()
{
cout << "\nEnter value of z= "; cin >> z;
}
void product()
{
cout << "\nProduct= " << x * y * z;
} };
Multilevel Inheritance
int main( )
OUTPUT:
{
Enter value of x= 2
derive2 a; //object of derived class
Enter value of y= 4
a.getdata();
a.readdata(); Enter value of z= 5
a.indata(); Product= 40
a.product();
return 0;
}
Hierarchical Inheritance
#include <iostream>
using namespace std;
class A // Base class
{
Protected:
int x, y; // data members
public:
void getdata() // to input x and y
{
cout<< "Enter value of x and y:\n";
cin>> x >> y;
}
};
Hierarchical Inheritance
class B : public A //B is derived from class base
{
public:
void product()
{
cout<< "\nProduct= " << x * y <<endl;
} };
class C : public A //C is also derived from class base
{
public:
void sum()
{
cout<< "\nSum= " << x + y; // Perform sum
} };
Hierarchical Inheritance
int main()
{
B obj1; //object of derived class B
C obj2; //object of derived class C
obj1.getdata(); OUTPUT:
Enter value of x and y:
obj1.product(); 24
obj2.getdata();
Product= 8
obj2.sum(); Enter value of x and y:
24
return 0;
} Sum= 6
Multiple Inheritance
#include <iostream>
using namespace std;
class stud {
protected:
int roll, m1, m2;
public:
void get()
{
cout << "Enter the Roll No.: ";
cin >> roll;
cout << "Enter the two highest marks: ";
cin >> m1 >> m2;
}
};
Multiple Inheritance
class extracurriculam {
protected:
int xm;
public:
void getsm()
{
cout << "\nEnter the mark for Extra Curriculam Activities:
"; cin >> xm;
}
};
Multiple Inheritance
class output : public stud, public extracurriculam {
int tot, avg;
public:
void display()
{
tot = (m1 + m2 + xm);
avg = tot / 3;
cout << "\n\n\tRoll No : " << roll << "\n\tTotal : " << tot;
cout << "\n\tAverage : " << avg;
} };
int main() OUTPUT:
{ Enter the Roll No.: 2
output O; Enter the two highest marks: 34 56
O.get();
Enter the mark for Extra Curriculam Activities: 5
O.getsm();
O.display(); } Roll No : 2
Total : 95
Average : 31
MultPath Inheritance
#include <iostream>
using namespace std;
class person {
protected:
char name[100];
int code;
public:
void input()
{
cout<<"\nEnter the name of the person : ";
cin>>name;
cout<<endl<<"Enter the code of the person : ";
cin>>code; }
void display()
{
cout<<endl<<"Name of the person : "<<name;
cout<<endl<<"Code of the person : "<<code; } };
MultiPath Inheritance
class account:virtual public person
{
protected:
float pay;
public:
void getpay()
{
cout<<endl<<"Enter the pay : ";
cin>>pay;
}
void display()
{
cout<<endl<<"Pay : "<<pay; } };
MultiPath Inheritance
class admin:virtual public person
{
Protected:
int experience;
public:
void getexp()
{
cout<<endl<<"Enter the experience : ";
cin>>experience;
}
void display()
{
cout<<endl<<"Experience : "<<experience; } };
MultiPath Inheritance
class master: public account, public admin
{
char n[100];
public:
void gettotal()
{
cout<<endl<<"Enter the company name : ";
cin>>n;
}
void display()
{
cout<<endl<<"Company name : "<<n;
}
};
MultiPath Inheritance
int main() OUTPUT:
{ Enter the name of the person : PPPP
return 0;
}
What to inherit?
output: A:default
B test(1); B
Constructors and Destructors in Base and
Derived Classes
#include<iostream>
using namespace std;
class BC
{
public:
BC()
{
cout<<"Base Class Constructor"<<endl;
}
~BC()
{
cout<<"Base Class Distructor"<<endl;
}
};
Constructors and Destructors in Base and
Derived Classes
class Derv:public BC
{
public:
Derv()
{
cout<<"Derived Class Constructor"<<endl;
}
~Derv()
{
cout<<"Derived Class Distructor"<<endl;
} };
int main()
{
Derv ob1;
cout<<"Program is ending"<<endl;
return 0;
}
Passing Arguments to Base Class Constructor
Square::Square(int side):Rectangle(side,side)
void Y();
DerivedClass D;
D.X();
Late Binding
#include<iostream>
using namespace std;
class base {
public:
virtual void print()
{
cout << "print base class\n";
}
void show()
{
cout << "show base class\n";
} };
Late Binding
class derived : public base {
public:
void print()
{
cout << "print derived class\n";
}
void show()
{
cout << "show derived class\n";
}
};
Late Binding
int main()
OUTPUT:
{ print derived class
show base class
base *bptr;
derived d;
bptr = &d;
#include<iostream>
using namespace std;
class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};
Pure Virtual Function
class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; }
};
int main(void)
{
Derived d;
d.fun();
return 0;
}
Abstract Class with Constructor
#include<iostream>
using namespace std;
// An abstract class with constructor
class Base
{
protected:
int x;
public:
virtual void fun() = 0;
Base(int i) {
x = i;
cout<<"Constructor of base called\n";
} };
Abstract Class with Constructor
Reference:
https://techvidvan.com/tutorials/cpp-templates/
Function Overloading
Non-type parameter
template <class T, int n, class U, class V>
int main()
{
X<float, 22> x1; //OK
const int n=44;
X<char, n> x2; //OK
int m=66;
X<short, m> x3; //error! – m must be a constant
}
Example Addition:
#include <iostream>
using namespace std; int main()
template<class T> {
class Add Add<int> data;
{ data.addition();
return 0;
T n1 = 6; }
T n2 = 1;
public:
void addition()
{
cout << "n1+n2: " << n1+n2<<endl;
}
};
Class Templates and Member Function
Instantiating Class templates
template<class T>
class X{ Member function of a
class Template
T square(T t) { return t*t; }
};
The square function in this example would be handled in the
same way.
Class X_short{
short square(short t) {return t*t;}
};
* Except that your compiler might
assign a different name for the class
X_short x;
other than X_short.
Class Templates Example:
For the Vector class - we may want Vectors of integer, float, Date, etc. Without
templates, we have to create different vectors for each of them, with templates we
can create a single generic vector:
operator[ ]
operator =
public:
Calculator(Temp num1, Temp num2)
{
n1 = num1;
n2 = num2;
}
Example Calculator:
void show()
{
cout << "Addition : " << n1 << "+" << n2 << "= " << addition() << endl;
cout << "Subtraction: " <<n1 << "-" << n2 << "= " << subtraction() <<
endl;
cout << "Product: " << n1 << "*" << n2 << "= " << multiplication() <<
endl;
cout << "Division: " << n1 << "/" << n2 << "= " << division() << endl;
}
Temp addition() { return (n1 + n2); }