Oop Unit Iii
Oop Unit Iii
Types of inheritance:
1.Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
2. What is multilevel inheritance? Develop a C++ program for multilevel 4 S-24
inheritance.
Multilevel Inheritance:
The inheritance in which a class can be derived from another derived class is
known as Multilevel Inheritance. Suppose there are three classes A, B, and C. A is
the base class. B is the derived class of A. and C is the class that is derived from
class B.
3. Describe the concept of virtual base class with suitable example. 4 S-24
Illustrate the concept of virtual base class with suitable example. 4 S-23
Consider a hybrid inheritance as shown in the above diagram. The child class has
two direct base classes, Parent1 and Parent2 which themselves have a common
base class as Grandparent. The child inherits the members of Grandparent via two
separate paths. All the public and protected members of Grandparent are inherited
into Child twice, first via Parent1 and again via Parent2. This leads to duplicate
sets of the inherited members of Grandparent inside Child class. The duplication of
inherited members can be avoided by making the common base class as virtual
base class while declaring the direct or intermediate base classes as shown below.
class Grandparent
{
};
class Parent1: virtual public Grandparent
{
};
class Parent2: public virtual Grandparent
{
};
class Child: public Parent1, public Parent2
{
};
Example:
#include<iostream>
using namespace std;
class student
{
int rno;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
RSCOEP Department of Computer Engineering Prof.S.U.Puri 77
}
void putnumber()
{
cout<<"\n\n\t Roll No:"<<rno<<"\n";
}
};
class test: virtual public student
{
public:
int part1,part2;
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Part1:";
cin>>part1; cout<<"Part2:";
cin>>part2;
}
void putmarks()
{
cout<<"\t Marks Obtained\n";
cout<<"\n\t Part1:"<<part1;
cout<<"\n\tPart2:"<<part2;
}
};
class sports: public virtual student
{
public:
int score;
void getscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
void putscore()
{
cout<<"\n\t Sports Score is:"<<score;
}
};
class result: public test, public sports
{
int total;
public:
void display()
{
total=part1+part2+score;
putnumber();
putmarks();
RSCOEP Department of Computer Engineering Prof.S.U.Puri 78
putscore();
cout<<"\n\t Total Score:"<<total;
}
};
int main()
{
result obj;
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
}
4. Describe a C++ program to declare a class college with name and code.Derive 4 S-24
new class a student with member as name.Accept and display details of one
students along with college data
#include<iostream>
using namespace std;
class college
{
char name[10];
int code;
public:
void accept_college()
{
cout<<"Enter College Name:";
cin>>name;
cout<<"Enter Code:";
cin>>code;
}
void display_college()
{
cout<<endl<<"College Name:"<<name;
cout<<endl<<"College Code:"<<code;
}
};
class student:public college
{
char sname[10];
public:
void accept_student()
{
cout<<"Enter student Name:";
cin>>sname;
}
void display_student()
{
cout<<endl<<"Student Name:"<<sname;
State and describe visibility modes and its effects used in inheritance. 4 S-19
Visibility modes:
private
protected
public
Private:
o Therefore, the public and protected members of the base class can only be
accessed by the member functions of derived class but, cannot be accessed by the
objects of the derived class.
};
Public:
o Therefore the public members of the base class can be accessed by both the
member functions of derived class as well as the objects of the derived class.
Syntax:
};
Protected:
o Therefore the public and protected members of the base class can be accessed by
the member functions of derived class as well as the member functions of
immediate derived class of it but they cannot be accessed by the objects of derived
class
Syntax:
};
RSCOEP Department of Computer Engineering Prof.S.U.Puri 81
6. Write a program to implement inheritance shown in fig. 6 S-24
#include <iostream>
using namespace std;
// Base class Teacher
class Teacher {
public:
string Name;
int emp_id;
void setTeacherDetails()
{
cout<<"Enter Employee Name:";
cin>>Name;
cout<<"Enter Employee ID:";
cin>>emp_id;
}
void displayTeacherDetails()
cout << "Employee Name: " << Name << endl;
cout << "Employee ID: " << emp_id << endl;
}
};
// Base class Student
class Student {
public:
string S_Name;
int Roll_No;
void setStudentDetails()
{
cout<<"Enter Student Name:";
cin>>S_Name;
cout<<"Enter Roll Number ID:";
cin>>Roll_No;
}
void displayStudentDetails() {
cout << "Student Name: " << S_Name << endl;
cout << "Roll Number: " << Roll_No << endl;
}
RSCOEP Department of Computer Engineering Prof.S.U.Puri 82
};
// Derived class Info inheriting from both Teacher and Student
class Info : public Teacher, public Student {
public:
void displayInfo() {
displayTeacherDetails();
displayStudentDetails();
}
};
int main() {
Info info;
info.setTeacherDetails();
info.setStudentDetails();
cout<<endl;
info.displayInfo();
}
OR
#include <iostream>
using namespace std;
// Base class Teacher
class Teacher {
public:
string Name;
int emp_id;
void displayTeacherDetails() {
cout << "Teacher Name: " << Name << endl;
cout << "Employee ID: " << emp_id << endl;
}
};
// Base class Student
class Student {
public:
string S_Name;
int Roll_No;
void displayStudentDetails() {
cout << "Student Name: " << S_Name << endl;
cout << "Roll Number: " << Roll_No << endl;
}
RSCOEP Department of Computer Engineering Prof.S.U.Puri 83
};
// Derived class Info inheriting from both Teacher and Student
class Info : public Teacher, public Student {
public:
void displayInfo() {
displayTeacherDetails();
displayStudentDetails();
}
};
int main() {
Info info;
info.setTeacherDetails("Alice", 123);
info.setStudentDetails("Bob", 456);
info.displayInfo();
}
7. Explain the access specifier in C++. 4 W-23
1. Private
2. Public
3. protected
Types of Inheritance
Single Inheritance:
Multiple Inheritance:
This type of inheritance can lead to the "diamond problem" (ambiguity when a
RSCOEP Department of Computer Engineering Prof.S.U.Puri 84
class inherits from two base classes with the same member).
Multilevel Inheritance:
A derived class inherits from a base class, which itself is derived from another base
class.It forms a chain of inheritance.
Hierarchical Inheritance:
Multiple derived classes inherit from a single base class.It forms a tree-like
structure.
Hybrid Inheritance:
Example: A class inheriting from a base class and also from a class that itself uses
multiple inheritance.
Example:
Vehicle is the base class with properties like number of wheels, color, etc.
Car is a derived class of Vehicle, inheriting its properties and adding specific car
features like model, engine type, etc.
and Car, and adding specific sedan features like number of doors, boot space, etc.
#include <iostream>
class Vehicle {
public:
string color;
void displayVehicleDetails() {
};
public:
string model;
string engine_type;
void displayCarDetails() {
};
public:
int num_doors;
int boot_space;
void displaySedanDetails() {
};
int main() {
sedan.num_wheels = 4;
sedan.color = "Red";
sedan.model = "Corolla";
sedan.engine_type = "Petrol";
sedan.num_doors = 4;
sedan.boot_space = 500;
sedan.displayVehicleDetails();
sedan.displayCarDetails();
sedan.displaySedanDetails();
return 0;
10. Write a C++ program to implement multiple inheritance as shown in Figure 6 W-23
No. 1. Accept and display data of test marks and sport’s marks using object of
class ‘result’
#include <iostream>
class Test {
public:
void getTestMarks() {
void displayTestMarks() {
};
class Sports {
public:
int sportMarks;
void getSportMarks() {
void displaySportMarks() {
};
public:
int total;
void displayResult() {
displayTestMarks();
displaySportMarks();
};
int main() {
Result result;
result.getTestMarks();
result.getSportMarks();
result.calculateTotal();
result.displayResult();
return 0;
#include <iostream>
class Animal {
public:
void eat() {
};
public:
void bark() {
};
int main() {
Dog d;
d.bark();
return 0;
#include <iostream>
class A {
public:
void displayA() {
};
class B {
public:
void displayB() {
class C : public A {
public:
void displayC() {
};
public:
void displayD() {
};
int main() {
D obj;
obj.displayA();
obj.displayB();
obj.displayC();
obj.displayD();
return 0;
Cannot be instantiated.
#include <iostream>
class Shape {
public:
};
public:
double radius;
Circle(double r) : radius(r) {}
};
public:
};
int main() {
Circle c(5);
return 0;
void displayStudentDetails() {
cout << "Student Name: " << name << endl;
}
};
int main() {
Student student;
string collegeName, collegeCode, studentName;
cout << "Enter College Name: ";
getline(cin, collegeName);
cout << "Enter College Code: ";
getline(cin, collegeCode);
cout << "Enter Student Name: ";
getline(cin, studentName);
student.setCollegeDetails(collegeName, collegeCode);
student.setStudentDetails(studentName);
cout << endl << "College Details:" << endl;
student.displayCollegeDetails();
cout << endl << "Student Details:" << endl;
student.displayStudentDetails();
#include <iostream>
class Science {
protected:
public:
phy_marks = p;
chy_marks = c;
};
class Maths {
protected:
public:
geo_marks = g;
};
private:
int total;
public:
void calculateTotal() {
void displayTotal() {
};
int main() {
Result result;
result.setMathsMarks(alg, geo);
result.calculateTotal();
result.displayTotal();
return 0;
18. Write a program to implement inheritance as shown in figure No. 2. Assume 6 S-22
suitable member function
#include <iostream>
#include <string>
class Staff {
protected:
string code;
public:
code = c;
};
protected:
string subject;
public:
subject = s;
};
protected:
string grade;
public:
grade = g;
};
int main() {
Officer officer;
teacher.setCode(teacherCode);
teacher.setSubject(teacherSubject);
officer.setCode(officerCode);
officer.setGrade(officerGrade);
teacher.displayCode();
teacher.displaySubject();
officer.displayCode();
officer.displayGrade();
19. What is multilevel inheritance? Draw the diagram to show multilevel 2 W-19
inheritance. using classes with data member and member function.
#include <iostream>
class Grandfather {
protected:
string name;
public:
void setGrandfatherName(string n) {
name = n;
void displayGrandfatherName() {
};
protected:
public:
void setFatherOccupation(string o) {
occupation = o;
void displayFatherOccupation() {
};
public:
void displayInfo() {
displayGrandfatherName();
displayFatherOccupation();
};
int main() {
Son son;
son.setGrandfatherName("John Doe");
son.setFatherOccupation("Engineer");
son.displayInfo();
return 0;
20. Write a program to implement single inheritance from the following Refer 4 W-19
Figure No. 1.
#include <string>
class Employee {
public:
int emp_id;
string name;
emp_id = id;
name = n;
void displayEmployeeDetails() {
};
public:
float basic_salary;
basic_salary = salary;
void displayEmployeeInfo() {
};
int main() {
emp_info employee;
employee.setBasicSalary(50000.0);
employee.displayEmployeeInfo();
return 0;
21. Write a program to implement the following hierarchy using suitable member 6 W-19
functions. Refer Figure No. 2.
class Student {
public:
int roll_no;
string name;
roll_no = r;
name = n;
void displayStudentDetails() {
public:
marks1 = m1;
marks2 = m2;
void displayTestMarks() {
};
public:
int score;
void setSportsScore(int s) {
score = s;
void displaySportsScore() {
};
public:
int total;
void displayResult() {
displayStudentDetails();
displayTestMarks();
};
int main() {
Result result;
result.setStudentDetails(123, "Alice");
result.setTestMarks(85, 90);
result.calculateTotal();
result.displayResult();
return 0;
A derived class (also known as a subclass or child class) is a class that inherits
properties and methods from another class called the base class (or parent class or
superclass). It's a fundamental concept in object-oriented programming that
promotes code reusability and hierarchical relationships between classes.
#include <iostream>
class Shape {
protected:
string color;
public:
color = c;
string getColor() {
return color;
};
private:
double radius;
public:
void setRadius(double r) {
radius = r;
double getArea() {
};
private:
public:
width = w;
height = h;
};
int main() {
Circle circle;
circle.setColor("Red");
circle.setRadius(5);
Rectangle rectangle;
rectangle.setColor("Blue");
rectangle.setDimensions(4, 6);
cout << "Circle color: " << circle.getColor() << ", Area: " << circle.getArea() <<
endl;
cout << "Rectangle color: " << rectangle.getColor() << ", Area: " <<
rectangle.getArea() << endl;
return 0;
23. Write a C++ program to declare a class COLLEGE with members as college 4 S-19
code. Derive a new class as STUDENT with members as studid. Accept and
display details of student along with college for one object of student.
#include <iostream>
#include <string>
class COLLEGE {
protected:
string college_code;
public:
void displayCollegeCode() {
};
private:
int studid;
public:
studid = id;
void displayStudentId() {
void displayDetails() {
displayCollegeCode();
displayStudentId();
};
int main() {
STUDENT student;
string college_code;
int studid;
student.setCollegeCode(college_code);
student.setStudentId(studid);
student.displayDetails();
return 0;
24. Describe with examples, passing parameters to base class constructor and 4 S-19
derived class constructor by creating object of derived class.
When creating an object of a derived class, the constructor of the base class is
called before the constructor of the derived class. This allows you to pass
parameters to both constructors to initialize members of both classes.
To pass parameters to the base class constructor, you use the member initialization
list in the derived class constructor.
#include <iostream>
class Base {
public:
int x;
cout << "Base constructor called with value " << x << endl;
};
int y;
cout << "Derived constructor called with value " << y << endl;
};
int main() {
return 0;
#include <iostream>
class Subject1 {
public:
int m1;
void setM1(int m) {
m1 = m;
RSCOEP Department of Computer Engineering Prof.S.U.Puri 113
}
void displayM1() {
};
class Subject2 {
public:
int m2;
void setM2(int m) {
m2 = m;
void displayM2() {
};
public:
int total;
void calculateTotal() {
total = m1 + m2;
void displayResult() {
displayM1();
displayM2();
int main() {
Result result;
result.setM1(m1);
result.setM2(m2);
result.calculateTotal();
result.displayResult();
return 0;
26. Write a C++ program to implement following inheritance. Refer Figure No. 2. 6 S-19
Accept and display data for one object of class result (Hint : use virtual base
# include <iostream.h>
#include<conio.h>
class College_Student
int student_id;
char College_code[5];
public:
void read_collegeStud_Data()
cin>>college_code>>student_id;
void display_collegeStud_Data()
cout<<college_code<<”\t”<<student_id<<”\n”;
};
float percentage;
public:
void read_test()
void display_test()
};
char grade[5];
public:
void read_sportsData()
cin>> grade;
void display_sportsData()
};
public:
void read_result()
read_test()
read_sportsData();
void display_result()
display_collegeStud_Data() ;
display_test()
display_sportsData();
};
void main()
result r;
clrscr();
r.read_result();
r.display_result();
27. Describe use of protected access specifier used in the class. 2 W-18
};
29. Write a C++ program to declare a class ‘College’ with data members as name 4 W-18
and college code. Derive a new class ‘student’ from the class college with data
RSCOEP Department of Computer Engineering Prof.S.U.Puri 118
members as sname and roll no. Accept and display details of one student with
college data.
#include <iostream>
#include <string>
class College {
public:
string name;
string code;
name = n;
code = c;
void displayCollegeDetails() {
};
public:
string sname;
int roll_no;
sname = sn;
roll_no = rno;
void displayStudentDetails() {
void displayAllDetails() {
displayCollegeDetails();
displayStudentDetails();
};
int main() {
Student student;
int roll_no;
getline(cin, college_name);
getline(cin, student_name);
student.setCollegeDetails(college_name, college_code);
student.setStudentDetails(student_name, roll_no);
student.displayAllDetails();
return 0;
Accept and display data of one teacher and one student using object of class
‘Info’.
#include <iostream>
#include <string>
class Info {
public:
string name;
void setName(string n) {
name = n;
void displayInfo() {
};
public:
int empid;
empid = id;
displayInfo();
};
public:
int rollno;
rollno = rno;
void displayStudentDetails() {
displayInfo();
};
int main() {
Teacher teacher;
Student student;
// Teacher details
teacher.setName("Mr. Smith");
teacher.setEmpId(12345);
// Student details
student.setName("Alice Johnson");
student.setRollNo(23456);
// Display details
teacher.displayTeacherDetails();
student.displayStudentDetails();
return 0;
Accept and display data for one programmer and one manager. Make display
function virtual.
#include <iostream>
#include <string>
class Employee {
protected:
int empid;
string empcode;
public:
empid = id;
};
private:
string skill;
public:
void setSkill(string s) {
skill = s;
};
private:
string department;
public:
department = dept;
};
int main() {
Programmer programmer;
Manager manager;
programmer.setEmpid(101);
programmer.setEmpcode("P001");
programmer.setSkill("C++");
manager.setEmpid(201);
manager.setEmpcode("M001");
manager.setDepartment("HR");
programmer.display();
manager.display();
return 0;
#include <iostream>
#include <string>
class CarManufacturer {
public:
string name;
void setManufacturerName(string n) {
name = n;
void displayManufacturerName() {
};
public:
string modelName;
int modelNo;
modelNo = mno;
void displayModelDetails() {
};
public:
int carNo;
string color;
carNo = cn;
color = c;
void displayCarDetails() {
void displayAllDetails() {
displayManufacturerName();
displayModelDetails();
displayCarDetails();
};
Car car;
getline(cin, manufacturerName);
getline(cin, modelName);
getline(cin, car.color);
car.setManufacturerName(manufacturerName);
car.setModelDetails(modelName, modelNo);
car.setCarDetails(carNo, car.color);
car.displayAllDetails();
return 0;