INHERITANCE
INHERITANCE
INHERITANCE
1] SINGLE INHERITANCE:
###constructor call :
First constructor of base class is called and then the constructor of derived class is called.
If there are 2 functions of same name then the one in derived class will be called and not the base
one. But if we erase that function from derived class than the function from the base class will be
called. Because there wont exist any function of the same name in the derived class.
Example:
#include<iostream>
class Vehicle{
protected:
int chasis_no;
int dist;
public:
this->chasis_no=chasis_no;
this->dist=dist;
void display(){
};
public:
string type;
int no_of_seaters;
string name;
int rent;
public:
this->type=type;
this->no_of_seaters=no_of_seaters;
this->name=name;
this->rent=rent;
void display(){
};
int main(){
V1.display();
return 0;
2]MULTILEVEL:
Example-
#include<iostream>
class Vehicle{
protected:
int chasis_no;
int dist;
public:
void intro(){
}
};
public:
int no_of_seaters;
public:
void type(){
};
public:
string name;
int rent;
this-> chasis_no=chasis_no;
this->dist=dist;
this->no_of_seaters=no_of_seaters;
this->name=name;
this->rent=rent;
void display(){
};
int main(){
SUV S1(1234,123,4,"BMW",4000);
S1.display();
S1.intro();
S1.type();
return 0;
3] MULTIPLE INHERITENCE:
Example:
#include <iostream>
class Car{
public:
string name;
string number;
void car_details(){
//Car(){
// cout<<"car on rent!!!"<<endl;
//}
};
class Bike{
protected:
string bname;
int no;
void bike_details(){
/*Bike(){
cout<<"Bike on rent!!"<<endl;
}*/
};
class Customer: public Car, public Bike{
public:
string custname;
int rent;
this->custname=custname;
this->rent=rent;
this->name=name;
this->number=number;
this->bname=bname;
this->no=no;
void showcase(){
car_details();
bike_details();
cout<<"Customer approved!"<<endl;
};
int main(){
C1.showcase();
return 0;
Ex-
#include <iostream>
#include<cmath>
class Simplecalc{
int a;
int b;
public:
int getData(){
cin>>a;
cin>>b;
return a;
return b;
public:
int disp_res(){
cout<<"Subtraction: "<<a-b<<endl;
cout<<"Multiplication: "<<a*b<<endl;
cout<<"Division: "<<a/b<<endl;
return 0;
};
class Scienticalc{
int n1;
int n2;
public:
int getData(){
cin>>n1;
cin>>n2;
return 0;
public:
int display(){
cout<<"cos(n1): "<<cos(n1)<<endl;
cout<<"sin(n2): "<<sin(n1)<<endl;
cout<<"tan(n1): "<<tan(n1)<<endl;
return n1;
return n2;
};
};
int main(){
Hybridcalc H1;
H1.getData();
H1.display();
WE GET ERROR AFTER RUNNING BECAUSE THERE ARE TWO FUNCTIONS OF SAME NAME !!!
4]VIRTUAL FUNCTIONS:
EX
#include<iostream>
class Shape{
protected:
int x;
int y;
public:
void get_data(){
cin>>x;
cin>>y;
};
public:
void get_data(){
cin>>x;
cin>>y;
int area_t=0.5*x*y;
};
public:
void get_data(){
cin>>y;
int area_r=x*y;
};
int main(){
Rectangle R;
R.get_data();
R.display_area();
Triangle T;
T.get_data();
T.display_area();
Certainly! Here are some potential problem statements on inheritance in C++ that would be suitable
for SY (Second Year) Engineering students. These problems range in complexity and cover various
aspects of inheritance, polymorphism, and class design.
Create a class hierarchy for geometric shapes. Define a base class `Shape` with a pure virtual
function `area()`. Derive classes `Circle`, `Rectangle`, and `Triangle`, each implementing the `area()`
function. Write a program to demonstrate polymorphism by storing different shapes in an array and
calculating their areas.
2. **Employee Management System**:
Design a base class `Employee` with derived classes `FullTimeEmployee`, `PartTimeEmployee`, and
`Intern`. Each derived class should have its own method to calculate the salary based on different
criteria. Implement a program to create objects of these classes and display their details and salaries.
Create a base class `BankAccount` with derived classes `SavingsAccount` and `CurrentAccount`.
Implement functions for deposit, withdrawal, and calculating interest (for SavingsAccount). Write a
program to manage these accounts and demonstrate the use of inheritance and polymorphism.
Design a class hierarchy for a library system. Create a base class `Book` with derived classes
`Textbook`, `Magazine`, and `ReferenceBook`. Implement methods for displaying details and
availability status. Create a simple menu-driven program to manage library operations.
Create a base class `Vehicle` and derive classes `Car`, `Bike`, and `Truck`. Each derived class should
implement a method to display vehicle information including type, make, model, and any specific
attributes. Write a program to create and manage a list of vehicles.
Design a base class `Character` with derived classes `Warrior`, `Mage`, and `Archer`. Each class
should have attributes like health, strength, and a method to display their abilities. Implement a
game simulation that showcases the characters fighting each other, demonstrating polymorphism in
action.
7. **Animal Classification**:
Create a base class `Animal` with derived classes `Mammal`, `Bird`, and `Reptile`. Implement a
method in each class to display unique characteristics. Write a program to create instances of these
classes and demonstrate their properties.
Design a base class `Loan` with derived classes `PersonalLoan`, `HomeLoan`, and `CarLoan`. Each
derived class should have methods to calculate the monthly payment based on different interest
rates and loan durations. Implement a user interface to input loan details and display the calculated
payments.
9. **Online Course Management**:
Create a class hierarchy for an online course system. Define a base class `Course` with derived
classes `TechnicalCourse`, `ManagementCourse`, and `ArtCourse`. Each course should have specific
attributes and methods to display course details and enroll students.
Develop a class hierarchy for a furniture store. Create a base class `Furniture` and derived classes
`Chair`, `Table`, and `Sofa`. Implement methods to calculate the price based on dimensions and
material. Write a program that allows users to select furniture items and calculate the total cost.
- Each problem can be expanded to include features like file handling, exception management, and
user input validation for added complexity.
- Encourage students to think about code reusability, maintainability, and the benefits of using
inheritance and polymorphism in their designs.
- You can also include scenarios that require overriding base class methods and the implications of
using virtual vs. non-virtual functions.
Feel free to modify these statements according to the specific needs of your students!
Q.MULTIPATH:
#include<iostream>
class Human{
protected:
string name;
public:
void display(){
cout<<"I am "<<name<<endl;
};
public:
int subscribers;
public:
void work(){
};
public:
string specialisation;
public:
void special(){
};
public:
int salary;
Teacher(){
};
this->name=name;
this->subscribers=subscribers;
this->specialisation=specialisation;
this->salary=salary;
void sal(){
};
int main(){
Teacher T("Sanika",5000,"Tech",4000);
T.display();
T.work();
T.special();
T.sal();