OODP W6
OODP W6
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) {}
};
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;
};
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.
CODE:-
#include <iostream>
using namespace std;
class Person {
protected:
string name;
public:
void setName(string n) { name = n; }
void display() { cout << "Name: " << name << 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