INHERITANCE

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

INHERITANCE: Not to write the same code again in other classes i.

e we inherit those lines of codes


into other class.(and can add extra properties )

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>

using namespace std;

class Vehicle{

protected:

int chasis_no;

int dist;

public:

Vehicle(int chasis_no,int dist){

this->chasis_no=chasis_no;

this->dist=dist;

void display(){

cout <<"Chasis number: "<<chasis_no<<"Travelling distance: "<<dist<<endl;

};

class Car:public Vehicle{

public:

string type;

int no_of_seaters;

string name;

int rent;
public:

Car(int chasis_no,int dist,string type,int no_of_seaters,string name,int rent):Vehicle(chasis_no,dist)


{

this->type=type;

this->no_of_seaters=no_of_seaters;

this->name=name;

this->rent=rent;

void display(){

cout<<" "<<chasis_no<<"\n km"<<dist<<"\n"<<type<<"\n"<<no_of_seaters<<"\n"<<name<<"\


n"<<rent<<endl;

};

int main(){

Car V1(12345,120,"SUV",5,"Mercedes-Benz EQ",500);

V1.display();

return 0;

2]MULTILEVEL:

Example-

#include<iostream>

using namespace std;

class Vehicle{

protected:

int chasis_no;

int dist;

public:

void intro(){

cout <<"Chasis number: "<<chasis_no<<"\nTravelling distance: "<<dist<<endl;

}
};

class Car:public Vehicle{

public:

int no_of_seaters;

public:

void type(){

cout<<"the number of ppl: "<<no_of_seaters<<endl;

};

class SUV: public Car{

public:

string name;

int rent;

SUV(int chasis_no,int dist,int no_of_seaters,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(){

cout<<" "<<name<<"\n "<<rent<<endl;

};

int main(){

SUV S1(1234,123,4,"BMW",4000);

S1.display();

S1.intro();
S1.type();

return 0;

3] MULTIPLE INHERITENCE:

Example:

#include <iostream>

using namespace std;

class Car{

public:

string name;

string number;

void car_details(){

cout<<"Nmae: "<<name<<"\n Number: "<<number<<endl;

//Car(){

// cout<<"car on rent!!!"<<endl;

//}

};

class Bike{

protected:

string bname;

int no;

void bike_details(){

cout<<"Name: "<<bname<<"\n Number: "<<no<<endl;

/*Bike(){

cout<<"Bike on rent!!"<<endl;

}*/

};
class Customer: public Car, public Bike{

public:

string custname;

int rent;

Customer(string custname,int rent,string name,string number,string bname,int no){

this->custname=custname;

this->rent=rent;

this->name=name;

this->number=number;

this->bname=bname;

this->no=no;

void showcase(){

cout<<"Customer Name: "<<custname<<"\n Rent (in rupees): "<<rent<<endl;

car_details();

bike_details();

cout<<"Customer approved!"<<endl;

};

int main(){

Customer C1("Sanika",1200,"Hyundai AURA","MH-09GA2226","Access 125",5898);

C1.showcase();

return 0;

AMBIGUITY IN MULTIPLE INHERITACE:

Ex-

#include <iostream>

#include<cmath>

using namespace std;

class Simplecalc{
int a;

int b;

public:

int getData(){

cout<<"Enter first number: "<<endl;

cin>>a;

cout<<"Enter the second number: "<<endl;

cin>>b;

return a;

return b;

public:

int disp_res(){

cout<<"Addition of two nos: "<<a+b<<endl;

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(){

cout<<"Enter first number: "<<endl;

cin>>n1;

cout<<"Enter the second number: "<<endl;

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;

};

class Hybridcalc:public Simplecalc,public Scienticalc{

};

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>

using namespace std;

class Shape{
protected:

int x;

int y;

public:

void get_data(){

cout<<"Enter the side1: "<<endl;

cin>>x;

cout<<"Enter the side2: "<<endl;

cin>>y;

virtual void display_area()=0;

};

class Triangle:public Shape{

public:

void get_data(){

cout<<"Enter the side1 of triangle: "<<endl;

cin>>x;

cout<<"Enter the side2 of triange: "<<endl;

cin>>y;

virtual void display_area() override{

int area_t=0.5*x*y;

cout<<"The area of triangle is: "<<area_t<<endl;

};

class Rectangle:public Shape{

public:

void get_data(){

cout<<"Enter the side1 of rectangle: "<<endl;


cin>>x;

cout<<"Enter the side2 of rectangle: "<<endl;

cin>>y;

virtual void display_area() override{

int area_r=x*y;

cout<<"Area of rectangle: "<<area_r<<endl;

};

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.

### Problem Statements

1. **Basic Shape Hierarchy**:

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.

3. **Bank Account System**:

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.

4. **Library Management System**:

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.

5. **Vehicle Registration System**:

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.

6. **Game Character Hierarchy**:

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.

8. **Bank Loan System**:

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.

10. **Furniture Store**:

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.

### Additional Considerations

- 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>

using namespace std;

class Human{

protected:

string name;

public:

void display(){

cout<<"I am "<<name<<endl;

};

class Youtuber:public virtual Human{

public:
int subscribers;

public:

void work(){

cout<<"I have no. of subscribers: "<<subscribers<<endl;

};

class Engineer:public virtual Human{

public:

string specialisation;

public:

void special(){

cout<<"i have specialisation in "<<specialisation<<endl;

};

class Teacher: public Youtuber,public Engineer{

public:

int salary;

Teacher(){

};

Teacher(string name,int subscribers,string specialisation,int salary){

this->name=name;

this->subscribers=subscribers;

this->specialisation=specialisation;

this->salary=salary;

void sal(){

cout<<"my salary is: "<<salary;

};
int main(){

Teacher T("Sanika",5000,"Tech",4000);

T.display();

T.work();

T.special();

T.sal();

You might also like