Inheritance
Inheritance
Title: Inheritance:
1. Create a base class item that stores the title and price of item. Create another base
class sale that holds sales figure of three months. Use the concept of multiple
inheritance to derive two classes hwitem and switem from both item and sales. Each
class has its own getdata() and displaydata() functions to input and output data
respectively.
2. C++ program to create a class result derived from marks which itself is derived
from student using Multilevel Inheritance.
3. C++ program to create Employee and Student inheriting from Person using
Hierarchical Inheritance.
4. Create a base class student that stores the name and PRN of student. Derive two
classes UT_Marks and sports from the class student. Use the concept of Hybrid
Inheritance to derive the class result from the class UT_Marks and class sports.
Description:
The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important feature of Object Oriented
Programming.
Sub Class: The class that inherits properties from another class is called Sub class or
Derived Class.
Super Class:The class whose properties are inherited by sub class is called Base Class or
Super class.
Implementing inheritance in C++: For creating a sub-class which is inherited from the base
class we have to follow the below syntax.
Syntax:
class subclass_name : access_mode base_class_name
{
//body of subclass
};
Types of Inheritance in C++
1. Single Inheritance: In single inheritance, a class is allowed to inherit from only one class.
i.e. one sub class is inherited by one base class only.
PP Lab PRN:24070122505
Syntax:
class subclass_name : access_mode base_class
{
//body of subclass
};
2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit
from more than one classes. i.e one sub class is inherited from more than one base classes.
Syntax:
class subclass_name : access_mode base_class1, access_mode base_class2, ....
{
//body of subclass
};
3. Multilevel Inheritance: In this type of inheritance, a derived class is created from another
derived class.
PP Lab PRN:24070122505
4. Hierarchical Inheritance: In this type of inheritance, more than one sub class is inherited
from a single base class. i.e. more than one derived class is created from a single base class.
Program Code:
1.#include <iostream>
#include <string>
using namespace std;
class Item {
protected:
string title;
float price;
public:
void getData() {
cout << "Enter item title: ";
cin.ignore();
getline(cin, title);
cout << "Enter item price: ";
cin >> price;
}
PP Lab PRN:24070122505
void displayData() {
cout << "Item Title: " << title << endl;
cout << "Item Price: $" << price << endl;
}
};
class Sales {
protected:
float salesFigures[3];
public:
void getData() {
for (int i = 0; i < 3; ++i) {
cout << "Enter sales figure for month " << (i + 1) << ": ";
cin >> salesFigures[i];
}
}
void displayData() {
cout << "Sales Figures for the last 3 months:" << endl;
for (int i = 0; i < 3; ++i) {
cout << "Month " << (i + 1) << ": $" << salesFigures[i] << endl;
}
}
};
void displayData() {
cout << "\n--- Hardware Item Details ---" << endl;
Item::displayData();
Sales::displayData();
PP Lab PRN:24070122505
}
};
void displayData() {
cout << "\n--- Software Item Details ---" << endl;
Item::displayData();
Sales::displayData();
}
};
int main() {
HWItem hardware;
SWItem software;
hardware.getData();
hardware.displayData();
software.getData();
software.displayData();
return 0;
}
2.#include <iostream>
#include <string>
using namespace std;
PP Lab PRN:24070122505
class Student {
protected:
string name;
int rollNumber;
public:
void getStudentData() {
cout << "Enter Student Name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Roll Number: ";
cin >> rollNumber;
}
void displayStudentData() {
cout << "Student Name: " << name << endl;
cout << "Roll Number: " << rollNumber << endl;
}
};
public:
void getMarks() {
cout << "Enter marks for Subject 1: ";
cin >> subject1;
cout << "Enter marks for Subject 2: ";
cin >> subject2;
cout << "Enter marks for Subject 3: ";
cin >> subject3;
}
void displayMarks() {
cout << "Marks in Subject 1: " << subject1 << endl;
cout << "Marks in Subject 2: " << subject2 << endl;
cout << "Marks in Subject 3: " << subject3 << endl;
}
PP Lab PRN:24070122505
};
int main() {
Result result;
result.getStudentData();
result.getMarks();
result.displayResult();
return 0;
}
3.#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
int age;
PP Lab PRN:24070122505
public:
void getData() {
cout << "Enter Name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Age: ";
cin >> age;
}
void displayData() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
public:
void getEmployeeData() {
getData();
cout << "Enter Department: ";
cin.ignore();
getline(cin, department);
cout << "Enter Salary: ";
cin >> salary;
}
void displayEmployeeData() {
cout << "\n--- Employee Details ---" << endl;
displayData();
cout << "Department: " << department << endl;
cout << "Salary: $" << salary << endl;
}
};
PP Lab PRN:24070122505
public:
void getStudentData() {
getData();
cout << "Enter Major: ";
cin.ignore();
getline(cin, major);
cout << "Enter GPA: ";
cin >> gpa;
}
void displayStudentData() {
cout << "\n--- Student Details ---" << endl;
displayData();
cout << "Major: " << major << endl;
cout << "GPA: " << gpa << endl;
}
};
int main() {
Employee employee;
Student student;
return 0;
4.#include <iostream>
PP Lab PRN:24070122505
#include <string>
using namespace std;
class Student {
protected:
string name;
string prn;
public:
public:
public:
void display_result() {
cout << "Name: " << UT_Marks::name << endl;
cout << "PRN: " << UT_Marks::prn << endl;
cout << "UT Marks: " << ut_marks << endl;
cout << "Sports Activity: " << sports_activity << endl;
}
}
int main() {
student_result.display_result();
return 0;
}
2.
PP Lab PRN:24070122505
3.
4.
PP Lab PRN:24070122505
Practice programs: C++ program to read and print employee information using multiple
inheritance.