Ex . No.
6 Programs using types of inheritance
Program 1: Multi-Level Inheritance for Student Percentage
Calculation
Write a C++ program to calculate the percentage of a student using
multi-level inheritance. Accept the marks of three subjects in base
class. A class will derive from the above mentioned class which
includes a function to find the total marks obtained and another
class derived from this class which calculates and displays the
percentage of student.
Aim:
To create a C++ program using multi-level inheritance to calculate the
percentage of a student. The base class stores marks of three subjects, the
intermediate class calculates total marks, and the derived class computes and
displays the percentage.
Algorithm:
Step 1: Start
Step 2: Create a base class Student with three subject marks as data
members.
Step 3: Create a derived class Marks that inherits from Student and
includes a function to calculate total marks.
Step 4: Create another derived class Result (from Marks) to compute and
display the percentage.
Step 5: Accept user input for marks, compute total and percentage, and
display the result.
Step 6: Stop
Program :
#include <iostream>
using namespace std;
// Base Class: Student (Stores marks of 3 subjects)
class Student {
protected:
float mark1, mark2, mark3;
public:
void getMarks() {
cout << "Enter marks for 3 subjects: ";
cin >> mark1 >> mark2 >> mark3;
}
};
// Derived Class: Marks (Calculates total marks)
class Marks : public Student {
protected:
float total;
public:
void calculateTotal() {
total = mark1 + mark2 + mark3;
}
};
// Derived Class: Result (Calculates and displays percentage)
class Result : public Marks {
public:
void displayPercentage() {
float percentage = (total / 300) * 100;
cout << "Total Marks: " << total << endl;
cout << "Percentage: " << percentage << "%" << endl;
}
};
// Main function
int main() {
Result student;
student.getMarks();
student.calculateTotal();
student.displayPercentage();
return 0;
}
Output :
Enter marks for 3 subjects: 78 85 90
Total Marks: 253
Percentage: 84.33%
Program 2: Multiple Inheritance with Employee Information
Write a C++ program to implement the following and give a
brief description about the identified inheritance:
Aim:
To implement multiple inheritance in C++ where an Employee
class derives from BasicInfo and DepartmentInfo to store and display employee
details.
Algorithm:
Step 1: Start
Step 2: Create a base class BasicInfo with attributes for name and age
and a function to accept and display them.
Step 3: Create another base class DepartmentInfo with attributes for
department and designation and a function to accept and display them.
Step 4: Create a derived class Employee that inherits from both BasicInfo
and DepartmentInfo.
Step 5: The Employee class will have a function to display all employee
details.
Step 6: In the main function, create an object of Employee, take input,
and display the details.
Step 7: Stop
Program :
#include <iostream>
using namespace std;
// Base Class: BasicInfo
class BasicInfo {
protected:
string name;
int age;
public:
void getBasicInfo() {
cout << "Enter Employee Name: ";
cin >> name;
cout << "Enter Employee Age: ";
cin >> age;
}
void displayBasicInfo() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
// Base Class: DepartmentInfo
class DepartmentInfo {
protected:
string department, designation;
public:
void getDepartmentInfo() {
cout << "Enter Department: ";
cin >> department;
cout << "Enter Designation: ";
cin >> designation;
}
void displayDepartmentInfo() {
cout << "Department: " << department << endl;
cout << "Designation: " << designation << endl;
}
};
// Derived Class: Employee (inherits from BasicInfo and DepartmentInfo)
class Employee : public BasicInfo, public DepartmentInfo {
public:
void displayEmployeeDetails() {
cout << "\n--- Employee Details ---\n";
displayBasicInfo();
displayDepartmentInfo();
}
};
// Main function
int main() {
Employee emp;
emp.getBasicInfo();
emp.getDepartmentInfo();
emp.displayEmployeeDetails();
return 0;
}
Output :
Enter Employee Name: John
Enter Employee Age: 30
Enter Department: IT
Enter Designation: Manager
--- Employee Details ---
Name: John
Age: 30
Department: IT
Designation: Manager
Program 3: Hybrid Inheritance with Grandfather, Father, Mother,
and Son Classes
Write a c++ program for the following scenario - Create four
classes: grandfather, father, mother, and son to achieve the
combination of multilevel and hierarchical inheritance (as hybrid
inheritance). Hint: create functions like house(), land(), gold(), car()
in the classes and object for derived class son and access those
functions.
Aim:
To implement hybrid inheritance in C++ with classes Grandfather,
Father, Mother, and Son, demonstrating multiple and multilevel inheritance.
Algorithm:
Step 1: Start
Step 2: Create a base class Grandfather with a function house().
Step 3: Create a derived class Father inheriting from Grandfather, with
an additional function land().
Step 4: Create another class Mother with a function gold().
Step 5: Create a derived class Son that inherits from both Father and
Mother, adding a function car().
Step 6: Create an object of Son and call all functions.
Step 7: Stop
Program:
#include <iostream>
using namespace std;
// Base Class: Grandfather
class Grandfather {
public:
void house() {
cout << "Grandfather owns a house." << endl;
}
};
// Derived Class: Father (inherits from Grandfather)
class Father : public Grandfather {
public:
void land() {
cout << "Father owns agricultural land." << endl;
}
};
// Another Base Class: Mother
class Mother {
public:
void gold() {
cout << "Mother has gold jewelry." << endl;
}
};
// Derived Class: Son (inherits from Father and Mother - Hybrid Inheritance)
class Son : public Father, public Mother {
public:
void car() {
cout << "Son owns a sports car." << endl;
}
};
// Main function
int main() {
Son obj;
obj.house();
obj.land();
obj.gold();
obj.car();
return 0;
}
Output:
Grandfather owns a house.
Father owns agricultural land.
Mother has gold jewelry.
Son owns a sports car.
S.NO Particulars Max Marks
marks Obtained
1 Inference 10
2 presentation 3
3 On-time submission 2
TOTAL : 15
Result :
The above three C++ programs successfully demonstrate different types of
inheritance. Program 1 implements Multi-Level Inheritance, where the Result
class inherits from Marks, which in turn inherits from Student, to calculate the
total marks and percentage of a student. Program 2 demonstrates Hybrid
Inheritance, where the Son class inherits from both Father (who inherits from
Grandfather) and Mother, representing a family hierarchy and asset
ownership. Program 3 showcases Multiple Inheritance, where the Employee
class inherits from both BasicInfo and DepartmentInfo to manage and display
employee details. These programs effectively validate the implementation of
different inheritance types in C++.