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

Chapter 5

The document discusses different types of inheritance in C++ including single, multiple, multilevel and hierarchical inheritance. Code examples are provided to illustrate single, multiple and multilevel inheritance. Key advantages of inheritance like code reusability and runtime polymorphism are also mentioned.

Uploaded by

wednesday262626
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Chapter 5

The document discusses different types of inheritance in C++ including single, multiple, multilevel and hierarchical inheritance. Code examples are provided to illustrate single, multiple and multilevel inheritance. Key advantages of inheritance like code reusability and runtime polymorphism are also mentioned.

Uploaded by

wednesday262626
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Chapter 5:

1. Explain different forms of inheritance with the help of syntax in c++.

Inheritance is a mechanism that allows a class (derived or child class) to inherit


properties and behaviours from another class (base or parent class).
 Advantage /Use of Inheritance:
 Reusability is achieved through Inheritance,
 Runtime polymorphism achieved through inheritance.

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
{ };

3. What is inheritance? Explain single inheritance with an example.

Inheritance is a mechanism that allows a class (derived or child class) to inherit


properties and behaviours from another class (base or parent class).
 Advantage /Use of Inheritance:
 Reusability is achieved through Inheritance,
 Runtime polymorphism achieved through inheritance.

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
{ };

#include <iostream> #include <string> using namespace std;


class Employee {
protected:
string eid;
string ename;
string desg;
public:
void accept() {
cout << "Enter Employee id: ";
cin >> eid;
cout << "Enter name: ";
cin >> ename;
cout << "Enter designation: ";
cin >> desg;
}
void display() const {
cout << "Eid: " << eid << endl;
cout << "Name: " << ename << endl;
cout << "Designation: " << desg << endl;
}
};

class Manager : public Employee {


private:
int year_of_experience;
double salary;

public:
void accept() {
Employee::accept();
cout << "Enter years of experience: ";
cin >> year_of_experience;
cout << "Enter salary: ";
cin >> salary;
}

void display() const {


Employee::display();
cout << "Years of Experience: " <<
year_of_experience << endl;
cout << "Salary: " << salary << endl;
}
};

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

4. Give two advantages of Inheritance? Explain multilevel inheritance with an


example.

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

class Reservation : public Route {


protected:
int no_seats;
string cls;
float fare;
string date;

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

5. What is inheritance? Explain multiple inheritance with an example.


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
{ };

#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;
}
};

class Emp_Proj : public Employee, public Project {


private:
int duration;
public:
Emp_Proj(const string& nm, const string& desg, int pid, const string& tt, int dur)
: Employee(nm, desg), Project(pid,tt), duration(dur) {}
void display() const {
Employee::display();
Project::display();
cout << "Duration: " << duration << " months" << endl;
}
};
int main() {
Emp_Proj ep("John Doe", "Manager", 101, "New Product Launch", 12);
ep.display();
return 0;
}

6. What is hybrid inheritance? What is inheritance? Explain hierarchical


inheritance with an
example.
->>>Hybrid inheritance is a combination of two or more types of
inheritance. It can involve any combination of single, multiple,
multilevel, or hierarchical inheritance.
Syntax:
class Base { // class members };
class Derived1 : public Base
{ };
class Derived2 : public Base
{ };
class FinalDerived : public Derived1, public Derived2
{ };

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
{ };

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.

->>>>>Pure Virtual Function


 A pure virtual function is a virtual function that has no definition within
the class.
 A pure virtual function is a "do nothing" function. Here "do nothing"
means that it just provides the template, and derived class implements
the function.
Syntax: virtual return_type fun_name(Parameters)=0;
Example:
class Shape {
public:

virtual void calculateArea() = 0;


};

A pure virtual function doesn't have the function body and it must end
with = 0

A class that contains at least one pure virtual


function is known as an abstract class.
A class containing one or more pure virtual
functions is termed an abstract class, and you
cannot create instances (objects) of an
abstract class.

#include <iostream>
#include <cmath>
const double PI = 3.142;
class Shape {
public:
virtual double calculateArea() const = 0;
};

class Circle : public Shape {


private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea()
{ return PI * radius * radius; }
};

class Triangle : public Shape {


private:
double base, height;
public:
Triangle(double b, double h) : base(b), height(h) {}
double calculateArea()
{ return 0.5 * base * height; }
};

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

8. Explain virtual Base class concept with example.


-->>>
A pure virtual function is a virtual function that has no definition within
the class.
 A pure virtual function is a "do nothing" function. Here "do nothing"
means that it just provides the template, and derived class implements
the function.
Syntax: virtual return_type fun_name(Parameters)=0;

#include <iostream>
#include <cmath>
const double PI = 3.142;
class Shape {
public:
virtual double calculateArea() const = 0;
};

class Circle : public Shape {


private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea()
{ return PI * radius * radius; }
};
class Triangle : public Shape {
private:
double base, height;
public:
Triangle(double b, double h) : base(b), height(h) {}
double calculateArea()
{ return 0.5 * base * height; }
};

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

You might also like