Chapter 5
Chapter 5
Single Inheritance:
In single inheritance, a derived class inherits from only
one base class.
In simple inheritance, there is only one super class and
one sub class.
Syntax:
class Base
{ };
class Derived : public Base
{ };
Multiple Inheritance:
In multiple inheritance, a class can inherit from more than
one base class.
In multiple inheritance ,a derived class can inherit from
more than one base class.
Syntax:
class Base1 {};
class Base2 {};
class Derived : public Base1, public Base2
{ };
2. Explain various types of inheritance with the help of a block diagram.
Multilevel Inheritance:
In multilevel inheritance, a class inherits from another
class, and then another class inherits from the derived
class. This forms a chain of inheritance.
In multilevel inheritance, a derived class becomes a base
class for another class.
Syntax:
class Base { // class members };
class Derived1 : public Base
{};
class Derived2 : public Derived1
{ };
Hierarchical Inheritance:
Hierarchical Inheritance: In hierarchical inheritance,
multiple classes inherit from a single base class. Each of
these derived classes can have its own additional
members.
In hierarchical inheritance, multiple derived classes inherit
from a single base class.
Syntax:
class Base { };
class Derived1 : public Base
{ };
class Derived2 : public Base
{ };
Single Inheritance:
In single inheritance, a derived class inherits from only
one base class.
In simple inheritance, there is only one super class and
one sub class.
Syntax:
class Base
{ };
class Derived : public Base
{ };
public:
void accept() {
Employee::accept();
cout << "Enter years of experience: ";
cin >> year_of_experience;
cout << "Enter salary: ";
cin >> salary;
}
int main() {
int n;
cout << "Enter the number of managers: ";
cin >> n;
Manager m[n];
for (int i = 0; i < n; i++) {
m[i].accept();
}
cout << "\nDisplaying details of all
managers:" << endl;
for (int i = 0; i < n; ++i) {
m[i].display();
}
return 0;
}
->>>advantages of inheritance
1)improves code reusesabilty
2)runtime polymorphism
Multilevel Inheritance:
In multilevel inheritance, a class inherits from another
class, and then another class inherits from the derived
class. This forms a chain of inheritance.
In multilevel inheritance, a derived class becomes a base
class for another class.
Syntax:
class Base { // class members };
class Derived1 : public Base
{};
class Derived2 : public Derived1
{ };
class Flight {
protected:
string fno,fname;
public:
Flight(const string& f, const string& n)
: fno(f), fname(n) {}
void Flightdetails(){
cout << "Flight Number: " << fno <<
endl;
cout << "Flight Name: " <<fname <<
endl;
}
};
class Route : public Flight {
protected:
string src,dest;
public:
Route(const string& fno, const
string& n, const string& s, const
string& d)
: Flight(fno, n), src(s), dest(d) {}
void Routedetails() {
Flightdetails();
cout << "Source: " << src<<
endl;
cout << "Destination: " <<dest
<< endl;
}
};
public:
Reservation(const string& fno, const string& n,
const string& s, const string& d,int seat, const
string& c, float amt, const string& dates)
: Route(fno, n, s, d), no_seats(seat),cls(c),
fare(amt), date(dates) {}
void display() {
Routedetails();
cout << "Number of Seats: " << no_seats
<< endl;
cout << "Class: " << cls << endl;
cout << "Fare: " << fare << endl;
cout << "Travel Date: " << date << endl;
}
};
int main()
{
Reservation r1("F1000", "AIR", "Pune",
"Beng", 2, "A", 3000.0, "30-03-2024");
r1.display();
Reservation r2("F2000", "India", "Goa",
"pune", 4, "B", 5000.0, "20-02-2024");
r2.display();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Employee {
protected:
string name;
string designation;
public:
Employee(const string& _name,
const string& _designation)
: name(_name),
designation(_designation) {}
void display() const {
cout << "Employee Name: " <<
name << ", Designation: " <<
designation << endl;
}
};
class Project {
protected:
int projectId;
string title;
public:
Project(int pid, const string& t)
: projectId(pid), title(t) {}
void display() const {
cout << "Project ID: " <<
projectId << ", Title: " << title
<< endl;
}
};
7. What is Abstract class and Pure virtual function? Write a C++ program to create
a class shape
with function to find area and display name of shape and other essential components
of the
class. Create derived class circle, square, rectangle each having overridden
function area and
display. Write a suitable program which illustrate virtual function.
A pure virtual function doesn't have the function body and it must end
with = 0
#include <iostream>
#include <cmath>
const double PI = 3.142;
class Shape {
public:
virtual double calculateArea() const = 0;
};
int main() {
Circle circle(5.0);
Triangle triangle(4.0, 7.0);
cout << "Area of Circle: " << circle.calculateArea() << "\n";
cout << "Area of Triangle:"<< triangle.calculateArea() << "\n";
return 0;
}
#include <iostream>
#include <cmath>
const double PI = 3.142;
class Shape {
public:
virtual double calculateArea() const = 0;
};
int main() {
Circle circle(5.0);
Triangle triangle(4.0, 7.0);
cout << "Area of Circle: " << circle.calculateArea() << "\n";
cout << "Area of Triangle:"<< triangle.calculateArea() << "\n";
return 0;
}