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

OODP W6

The document contains tutorial questions and code examples related to object-oriented programming concepts such as inheritance in C++. It includes examples of single, multiple, hierarchical, multilevel, and hybrid inheritance, demonstrating how to create classes and derive them in C++. Additionally, it explains multilevel inheritance using an academic hierarchy scenario with classes for Person, Teacher, and Professor.

Uploaded by

Aarushi Sarkar
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)
4 views

OODP W6

The document contains tutorial questions and code examples related to object-oriented programming concepts such as inheritance in C++. It includes examples of single, multiple, hierarchical, multilevel, and hybrid inheritance, demonstrating how to create classes and derive them in C++. Additionally, it explains multilevel inheritance using an academic hierarchy scenario with classes for Person, Teacher, and Professor.

Uploaded by

Aarushi Sarkar
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/ 6

21CSC201T – Object Oriented Design

and Programming Week - 6 – Tutorial


Questions
Inheritance: Single, Multiple, Hierarchical,
Multilevel and Hybrid

CODE:-

#include <iostream>
using namespace std;

class Student {
protected:
string name;
int rollNumber;
public:
Student(string n, int roll) : name(n), rollNumber(roll) {}
};
class SportsPlayer {
protected:
string sportName;
int ranking;
public:
SportsPlayer(string sport, int rank) : sportName(sport), ranking(rank) {}
};

class SportStudent : public Student, public SportsPlayer {


public:
SportStudent(string n, int roll, string sport, int rank)
: Student(n, roll), SportsPlayer(sport, rank) {}
void displayDetails() {
cout << "Name: " << name << "\nRoll Number: " << rollNumber
<< "\nSport: " << sportName << "\nRanking: " << ranking << endl;
}
};

int main() {
SportStudent student1("Rahul", 101, "Basketball", 2);
student1.displayDetails();
}

OUTPUT:-
Name: Rahul
Roll Number: 101
Sport: Basketball
Ranking: 2
CODE:-

#include <iostream>
using namespace std;

class Shape {
public:
virtual void getData() = 0;
virtual double calculateArea() = 0;
};

class Rectangle : public Shape {


double length, width;
public:
void getData() { cin >> length >> width; }
double calculateArea() { return length * width; }
};

class Circle : public Shape {


double radius;
public:
void getData() { cin >> radius; }
double calculateArea() { return 3.14159 * radius * radius; }
};

int main() {
Rectangle rect; Circle circ;
rect.getData(); cout << "Rectangle Area: " << rect.calculateArea() << endl;
circ.getData(); cout << "Circle Area: " << circ.calculateArea() << endl;
}

OUTPUT:-
Rectangle Area: 50
Circle Area: 153.938

3. Academic Hierarchy:
 In an academic institution, different roles have hierarchical relationships.
Implement this scenario using multilevel inheritance in C++.
1. Create a base class Person that contains:
 A name attribute.
 A function to set and display the name.
2. Create a derived class Teacher that inherits from Person and includes:
 A subject attribute.
 A function to set and display the subject.
3. Create a further derived class Professor that inherits from Teacher and includes:
 A department attribute.
 A function to set and display the department.
4. Demonstrate multilevel inheritance by:
 Creating an object of Professor.
 Setting values for name, subject, and department.
 Displaying the details of the professor.

Explanation of Multilevel Inheritance:


 Multilevel inheritance is when a derived class inherits from another derived class,
forming a chain of inheritance.
 Here, Professor inherits from Teacher, which in turn inherits from Person.
 This means Professor gets access to all attributes and functions of both Teacher and
Person.

CODE:-
#include <iostream>
using namespace std;

class Person {
protected:
string name;
public:
void setName(string n) { name = n; }
void display() { cout << "Name: " << name << endl; }
};

class Teacher : public Person {


protected:
string subject;
public:
void setSubject(string s) { subject = s; }
void display() { Person::display(); cout << "Subject: " << subject << endl; }
};

class Professor : public Teacher {


string department;
public:
void setDepartment(string d) { department = d; }
void display() { Teacher::display(); cout << "Department: " << department << endl; }
};

int main() {
Professor prof;
prof.setName("Dr. Sharma"); prof.setSubject("CS"); prof.setDepartment("AI");
prof.display();
}

OUTPUT:-
Name: Dr. Sharma
Subject: CS
Department: AI

You might also like