0% found this document useful (0 votes)
9 views

Inheritance

The document contains examples of C++ programs using inheritance. It includes 11 exercises demonstrating inheritance concepts like deriving classes from base classes, accessing base class members, and multi-level inheritance. The exercises cover inheritance with classes like Vehicle, Shape, Person and more.

Uploaded by

Md Shorifuzzaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Inheritance

The document contains examples of C++ programs using inheritance. It includes 11 exercises demonstrating inheritance concepts like deriving classes from base classes, accessing base class members, and multi-level inheritance. The exercises cover inheritance with classes like Vehicle, Shape, Person and more.

Uploaded by

Md Shorifuzzaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Inheritance

Name: Mahfuz Ahmed


ID: 22203029
Exercise-1:
#include<iostream>
using namespace std;
class Person
{

public:
string name;
void get()
{
cout<<"Enter the person name:";
getline(cin,name);
}
};
class Student:public Person
{
int ID;
public:
void take()
{
cout<<"Enter the Student ID:";
cin>>ID;
}
void show()
{
cout<<"Student Name:"<<name<<endl;
cout<<"Student Id:"<<ID<<endl;
}
};
int main()
{
Student s;
s.get();
s.take();
s.show();
return 0;
}

Exercise-2:
#include<iostream>
using namespace std;
class Shape
{

public:
int w,l;
void get()
{
cout<<"Enter the Width and Height:";
cin>>w>>l;
}
};
class Rectangle:public Shape
{
public:

int area()
{
return w*l;
}
};
int main()
{
Rectangle r;
r.get();
cout<<"Area of this Rectangle:"<<r.area()<<endl;
return 0;

}
Exercise-3:
#include<iostream>
using namespace std;
class Vehicle
{

public:
int wheel;
float top_speed;
void get()
{
cout<<"Enter the Vehicle Wheels:";
cin>>wheel;
cout<<"Enter the Vehicle Top Speed:";
cin>>top_speed;
}
};
class Car:public Vehicle
{
int door;
public:
void take()
{
cout<<"Enter the Vehicle Door:";
cin>>door;
}
void show()
{
cout<<"Car No of Wheels:"<<wheel<<endl;
cout<<"Car Top Speed:"<<top_speed<<endl;
cout<<"Car No of Doors:"<<door<<endl;
}
};
int main()
{
Car c;
c.get();
c.take();
c.show();
return 0;
}
Exercise-4:
#include<iostream>
using namespace std;
class Animal
{

public:
string name;
};
class Cat:public Animal
{
int no_lives_cat;
public:
void get()
{
cout<<"Enter the name of Animal:";
cin>>name;
cout<<"Enter the No of Cat Lives:";
cin>>no_lives_cat;
}
void show()
{
cout<<"Animal Name:"<<name<<endl;
cout<<"NO of Cat Lives:"<<no_lives_cat<<endl;
}
};
int main()
{
Cat s;
s.get();
s.show();
return 0;
}
Exercise-5:
#include<iostream>
using namespace std;
class BankAccount
{

public:
double balance;
};
class SavingsAccount:public BankAccount
{
int interest;
public:
void get()
{
cout<<"Enter the Balance:";
cin>>balance;
cout<<"Enter the Interest rate:";
cin>>interest;
}
void show()
{
cout<<"Balance is:"<<balance<<endl;
cout<<"Interest rate:"<<interest<<"%"<<endl;
}
};
int main()
{
SavingsAccount s;
s.get();
s.show();
return 0;

Exercise-6:
#include<iostream>
using namespace std;
class Person
{

public:
string name;
int age;
};
class Student
{
public:
int ID;
float GPA;

};
class StudentAthlet:public Person, public Student
{
string sport;
public:
void get()
{
cout<<"Enter the Student name:";
getline(cin,name);
cout<<"Enter the Student Age:";
cin>>age;
cout<<"Enter the Student ID:";
cin>>ID;
cout<<"Enter the Student GPA:";
cin>>GPA;
cout<<"Enter the Sport name:";
cin>>sport;
}
void show()
{
cout<<"Student Name:"<<name<<endl;
cout<<"Student Age:"<<age<<endl;
cout<<"Student Id:"<<ID<<endl;
cout<<"Student GPA:"<<GPA<<endl;
cout<<"Sport Name:"<<sport<<endl;
}
};
int main()
{
StudentAthlet s;
s.get();
s.show();
return 0;

Exercise-7:
#include<iostream>
using namespace std;
class Shape
{

public:
int w,h;
};
class Color
{
public:
string colour;

};
class ColorShape:public Shape, public Color
{
public:
void get()
{
cout<<"Enter the Shape Width:";
cin>>w;
cout<<"Enter the Shape Height:";
cin>>h;
cout<<"Enter the Shape Color:";
cin>>colour;
}
void show()
{
cout<<"shape Width:"<<w<<endl;
cout<<"shape Height:"<<h<<endl;
cout<<"shape Color:"<<colour<<endl;
}
};
int main()
{
ColorShape c;
c.get();
c.show();
return 0;

Exercise-8:
#include<iostream>
using namespace std;
class Vehicle
{

public:
int wheel;
float speed;
};
class Engine
{
public:
int hpower;

};
class Car:public Vehicle, public Engine
{
public:
int door;
void get()
{
cout<<"Enter the Wheels:";
cin>>wheel;
cout<<"Enter the Top Speed:";
cin>>speed;
cout<<"Enter the Horsepower of the Engine:";
cin>>hpower;
cout<<"Enter the No of Doors:";
cin>>door;
}
void show()
{
cout<<"No of Wheels:"<<wheel<<endl;
cout<<"Top Speed:"<<speed<<endl;
cout<<"Horsepower of the Engine:"<<speed<<"cc"<<endl;
cout<<"Car No of Doors:"<<door<<endl;
}
};
int main()
{
Car c;
c.get();
c.show();
return 0;

}
Exercise-9:
#include<iostream>
using namespace std;
class Animal
{

public:
string name;
int age;

};
class Pet
{
public:
string owner;
};
class PetAnimal:public Animal, public Pet
{
public:
void get()
{
cout<<"Enter the Animal name:";
cin>>name;
cout<<"Enter the Animal age:";
cin>>age;
cout<<"Enter the owner name:";
cin.ignore();
getline(cin,owner);
}
void show()
{
cout<<"Animal name:"<<name<<endl;
cout<<"Animal age:"<<age<<endl;
cout<<"Owner name:"<<owner<<endl;
}
};
int main()
{
PetAnimal p;
p.get();
p.show();
return 0;

Exercise-10:
#include<iostream>
using namespace std;
class Mammals
{
public:
void show()
{
cout<<"I am a Mammal"<<endl;;
}
};

class MarineAnimals
{
public:
void output()
{
cout<<"I am a Marine Animal"<<endl;
}
};

class BlueWhale:public Mammals,public MarineAnimals


{
public:
void display()
{
cout<<"I belong to both the categories: Mammal as well as Marine Animal."<<endl;
}
};

int main()
{
Mammals mammal;
MarineAnimals marine;
BlueWhale blue;

mammal.show();
marine.output();
blue.display();
return 0;
}
Exercise-11:
#include<iostream>
using namespace std;
class Fruit
{

public:
int no_fruits;
void get()
{
cout<<"Enter the total number of Fruits:";
cin>>no_fruits;
}

};
class Apple:public Fruit
{
public:
int apple;
void take()
{
cout<<"Enter the number of Apple:";
cin>>apple;
}
};
class Mangoes:public Apple
{
public:
int mango;
void show()
{
mango=no_fruits-apple;
cout<<"Total Fruits="<<no_fruits<<endl;
cout<<"No of Apple="<<apple<<endl;
cout<<"No of Mango="<<mango<<endl;

}
};
int main()
{
Mangoes m;
m.get();
m.take();
m.show();
return 0;
}
Exercise-12:
#include<iostream>
using namespace std;
class Marks
{
public:
int roll, mark;
string name;
};
class Physics:public Marks
{
public:

void get(string s, int r)


{
name=s;
roll=r;
cout<<"Enter the PHY marks:";
cin>>mark;
}
};
class Chemistry:public Marks
{
public:

void get(string s, int r)


{
name=s;
roll=r;
cout<<"Enter the CHY marks:";
cin>>mark;
}
};
class Math:public Marks
{
public:

void get(string s, int r)


{
name=s;
roll=r;
cout<<"Enter the MATH marks:";
cin>>mark;
}
};
int main()
{
int n,totali=0,total=0;
float avg;
string Name;
cout<<"Enter the Number of student:";
cin>>n;
Physics P[n];
Chemistry C[n];
Math M[n];
for(int i=0;i<n;i++){
cout<<"Name:";
cin>>Name;
P[i].get(Name,i+1);
C[i].get(Name,i+1);
M[i].get(Name,i+1);
}
for(int i=0;i<n;i++)
{
cout<<"Name:"<<P[i].name<<endl;
cout<<"ROLL:"<<P[i].roll<<endl;
totali=P[i].mark+C[i].mark+M[i].mark;
cout<<"Total Mark:"<<totali<<endl;
total=total+totali;
}
avg=(float) total / (float) n;
cout<<"Average Marks of the class:"<<avg<<endl;
return 0;
}

Exercise-13:
#include<iostream>
using namespace std;
class Vehicle
{
public:
float mileage;
int price;
};
class Car: public Vehicle
{
public:
int owner_cost, warranty, seating_cap;
string fuel;
};
class Bike: public Vehicle
{
public:
int num_cylinder, num_gear;
string cooling, wheel;
float tank;
};
class Audi:public Car
{
public:
string model_ty;
void get()
{
cout<<"Enter the Details for AUDI Car:"<<endl;
cout<<"Enter Model Type:";
cin>>model_ty;
cout<<"Enter Ownership Cost:";
cin>>owner_cost;
cout<<"Enter Warranty year:";
cin>>warranty;
cout<<"Enter Seating Capacity:";
cin>>seating_cap;
cout<<"Enter Fuel Type:";
cin>>fuel;
cout<<"Enter the Mileage:";
cin>>mileage;
cout<<"Enter the Price:";
cin>>price;

}
void show()
{
cout<<"DEtails For AUDI Car:"<<endl;
cout<<"Model Type:"<<model_ty<<endl;
cout<<"Enter Ownership Cost:"<<owner_cost<<endl;
cout<<"Warranty year:"<<warranty<<endl;
cout<<"Seating Capacity:"<<seating_cap<<endl;
cout<<"Fuel Type:"<<fuel<<endl;
cout<<"Mileage:"<<mileage<<endl;
cout<<"Price:"<<price<<endl;

}
};

class Ford:public Car


{
public:
string model_ty;
void get()
{
cout<<"Enter the Details for FORD Car:"<<endl;
cout<<"Enter Model Type:";
cin>>model_ty;
cout<<"Enter Ownership Cost:";
cin>>owner_cost;
cout<<"Enter Warranty year:";
cin>>warranty;
cout<<"Enter Seating Capacity:";
cin>>seating_cap;
cout<<"Enter Fuel Type:";
cin>>fuel;
cout<<"Enter the Mileage:";
cin>>mileage;
cout<<"Enter the Price:";
cin>>price;

}
void show()
{
cout<<"DEtails For FORD Car:"<<endl;
cout<<"Model Type:"<<model_ty<<endl;
cout<<"Enter Ownership Cost:"<<owner_cost<<endl;
cout<<"Warranty year:"<<warranty<<endl;
cout<<"Seating Capacity:"<<seating_cap<<endl;
cout<<"Fuel Type:"<<fuel<<endl;
cout<<"Mileage:"<<mileage<<endl;
cout<<"Price:"<<price<<endl;

}
};

class Bajaj:public Bike


{
public:
string make_type;
void get()
{
cout<<"Enter the Details for BAJAJ Bike:"<<endl;
cout<<"Enter Make Type:";
cin>>make_type;
cout<<"Enter Number of Cylinders:";
cin>>num_cylinder;
cout<<"Enter The Number of Gear:";
cin>>num_gear;
cout<<"Enter Cooling Type:";
cin>>cooling;
cout<<"Enter the Wheel Type:";
cin>>wheel;
cout<<"Enter the Fuel Tank Size in inches:";
cin>>tank;
cout<<"Enter the Mileage:";
cin>>mileage;
cout<<"Enter the Price:";
cin>>price;

}
void show()
{
cout<<"DEtails For BAJAJ Bike:"<<endl;
cout<<"Make Type:"<<make_type<<endl;
cout<<"Number of Cylinder:"<<num_cylinder<<endl;
cout<<"Number of Gear:"<<num_gear<<endl;
cout<<"Cooling Type:"<<cooling<<endl;
cout<<"Wheel Type:"<<wheel<<endl;
cout<<"Tank Size:"<<tank<<endl;
cout<<"Mileage:"<<mileage<<endl;
cout<<"Price:"<<price<<endl;

}
};
class TVS:public Bike
{
public:
string make_type;
void get()
{
cout<<"Enter the Details for BAJAJ Bike:"<<endl;
cout<<"Enter Make Type:";
cin>>make_type;
cout<<"Enter Number of Cylinders:";
cin>>num_cylinder;
cout<<"Enter The Number of Gear:";
cin>>num_gear;
cout<<"Enter Cooling Type:";
cin>>cooling;
cout<<"Enter the Wheel Type:";
cin>>wheel;
cout<<"Enter the Fuel Tank Size in inches:";
cin>>tank;
cout<<"Enter the Mileage:";
cin>>mileage;
cout<<"Enter the Price:";
cin>>price;

}
void show()
{
cout<<"DEtails For BAJAJ Bike:"<<endl;
cout<<"Make Type:"<<make_type<<endl;
cout<<"Number of Cylinder:"<<num_cylinder<<endl;
cout<<"Number of Gear:"<<num_gear<<endl;
cout<<"Cooling Type:"<<cooling<<endl;
cout<<"Wheel Type:"<<wheel<<endl;
cout<<"Tank Size:"<<tank<<endl;
cout<<"Mileage:"<<mileage<<endl;
cout<<"Price:"<<price<<endl;
}
};
int main()
{
Audi A;
Ford F;
Bajaj B;
TVS T;

A.get();
F.get();
B.get();
T.get();
A.show();
F.show();
B.show();
T.show();

return 0;
}
Exercise-14:
#include<iostream>
using namespace std;
class Shape
{
public:
void show()
{
cout<<"This is a Shape"<<endl;
}
};
class Polygon:public Shape
{
public:
void show()
{
cout<<"Polygon is a Shape"<<endl;
}
};
class Rectangle:public Polygon
{
public:

void show()
{
cout<<"Rectangle is a Polygon"<<endl;
}
};
class Triangle:public Polygon
{
public:

void show()
{
cout<<"Triangle is a Polygon"<<endl;
}
};
class Square:public Rectangle
{
public:

void show()
{
cout<<"Square is a Rectangle"<<endl;
}
};
int main()
{
Shape S;
Polygon P;
Rectangle R;
Triangle T;
Square SQ;

S.show();
P.show();
R.show();
T.show();
SQ.show();

return 0;
}
Exercise-15:
#include<iostream>
#include<cmath>
using namespace std;
class Number
{
protected:
int num, sqr, cube;
public:
Number()
{
cout<<"Enter the number:";
cin>>num;
}
};
class Square:public Number
{
public:
Square()
{
sqr=pow(num,2);
}
void display()
{
cout<<"Square of the number is:"<<sqr<<endl;
}
};
class Cube:public Number
{
public:
Cube()
{
sqr=pow(num,3);
}
void print()
{
cout<<"Cube of the number is:"<<sqr<<endl;
}
};
int main()
{
Square S;
S.display();
Cube C;
C.print();

return 0;
}

You might also like