0% found this document useful (0 votes)
12 views15 pages

Inheritance

Uploaded by

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

Inheritance

Uploaded by

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

PP Lab PRN:24070122505

Practical No-8,9,10,11 Date: / /2024

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.

5. Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining more


than one type of inheritance. For example: Combining Hierarchical inheritance and
Multiple Inheritance.
Below image shows the combination of hierarchical and multiple inheritance:
PP Lab PRN:24070122505

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

class HWItem : public Item, public Sales {


public:
void getData() {
cout << "\n--- Enter Hardware Item Details ---" << endl;
Item::getData();
Sales::getData();
}

void displayData() {
cout << "\n--- Hardware Item Details ---" << endl;
Item::displayData();
Sales::displayData();
PP Lab PRN:24070122505

}
};

class SWItem : public Item, public Sales {


public:
void getData() {
cout << "\n--- Enter Software Item Details ---" << endl;
Item::getData();
Sales::getData();
}

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

class Marks : public Student {


protected:
float subject1, subject2, subject3;

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

};

class Result : public Marks {


public:
void displayResult() {
displayStudentData();
displayMarks();

float total = subject1 + subject2 + subject3;


float average = total / 3;

cout << "Total Marks: " << total << endl;


cout << "Average Marks: " << average << endl;
}
};

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

class Employee : public Person {


private:
string department;
float salary;

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

class Student : public Person {


private:
string major;
float gpa;

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;

cout << "Enter Employee Details:" << endl;


employee.getEmployeeData();
employee.displayEmployeeData();

cout << "\nEnter Student Details:" << endl;


student.getStudentData();
student.displayStudentData();

return 0;

4.#include <iostream>
PP Lab PRN:24070122505

#include <string>
using namespace std;

class Student {
protected:
string name;
string prn;

public:

Student(string n, string p) : name(n), prn(p) {}


};

class UT_Marks : public Student {


protected:
float ut_marks;

public:

UT_Marks(string n, string p, float marks) : Student(n, p), ut_marks(marks) {}


};

class Sports : public Student {


protected:
string sports_activity;

public:

Sports(string n, string p, string activity) : Student(n, p), sports_activity(activity) {}


};

class Result : public UT_Marks, public Sports {


public:

Result(string n, string p, float marks, string activity)


: UT_Marks(n, p, marks), Sports(n, p, activity) {}
PP Lab PRN:24070122505

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() {

Result student_result("John Doe", "PRN123456", 85.0, "Football");

student_result.display_result();

return 0;
}

Input and Output


1.
PP Lab PRN:24070122505

2.
PP Lab PRN:24070122505

3.

4.
PP Lab PRN:24070122505

Conclusion: Thus we have implemented the concept of inheritance in C++

Practice programs: C++ program to read and print employee information using multiple
inheritance.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy