- 2070 Asar · 6 marks
Student, teaching and non-teaching staff from person
Short answer
The program uses hierarchical inheritance: a base class Person with name, age, address; derived Student (oll, program), TeachingStaff (epartment, subject), and NonTeachingStaff (epartment, post). Each defines getdata() and display() to input and output details, demonstrating reuse.
Write a program to create classes to represent student, teaching staffs and non-teaching staffs from the base class person. Use proper members in the classes to make your program meaningful.
Design: a base class Person holds common data (name, age, address). Three classes derive from it publicly: Student (roll number, programme), TeachingStaff (department, subject) and NonTeachingStaff (department, post). Each class has getdata() and display().
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name, address;
int age;
public:
void getPerson() {
cout << "Name: "; cin >> name;
cout << "Age: "; cin >> age;
cout << "Address: "; cin >> address;
}
void showPerson() {
cout << "Name: " << name << ", Age: " << age
<< ", Address: " << address << endl;
}
};
class Student : public Person {
int roll;
string program;
public:
void getdata() {
getPerson();
cout << "Roll no: "; cin >> roll;
cout << "Programme: "; cin >> program;
}
void display() {
cout << "--- Student ---" << endl;
showPerson();
cout << "Roll: " << roll << ", Programme: " << program << endl;
}
};
class TeachingStaff : public Person {
string department, subject;
public:
void getdata() {
getPerson();
cout << "Department: "; cin >> department;
cout << "Subject: "; cin >> subject;
}
void display() {
cout << "--- Teaching Staff ---" << endl;
showPerson();
cout << "Dept: " << department << ", Subject: " << subject << endl;
}
};
class NonTeachingStaff : public Person {
string department, post;
public:
void getdata() {
getPerson();
cout << "Department: "; cin >> department;
cout << "Post: "; cin >> post;
}
void display() {
cout << "--- Non-teaching Staff ---" << endl;
showPerson();
cout << "Dept: " << department << ", Post: " << post << endl;
}
};
int main() {
Student s;
TeachingStaff t;
NonTeachingStaff n;
cout << "Enter student details" << endl; s.getdata();
cout << "Enter teaching staff details" << endl; t.getdata();
cout << "Enter non-teaching staff details" << endl; n.getdata();
s.display();
t.display();
n.display();
return 0;
}
This is hierarchical inheritance: one base class (Person) with three derived classes. The common data is written once in Person and reused, which shows the reusability benefit of inheritance. Members are protected so derived classes can access them but outside code cannot.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Abstract Student class program · Object Oriented Programming, chapter 7
- Manager derived from person and employee · Object Oriented Programming, chapter 6
- Protected specifier; student/test/result program · Object Oriented Programming, chapter 6
- File stream; student binary file program · Object Oriented Programming, chapter 8
- Access overridden members from derived class · Object Oriented Programming, chapter 6 · asked 2×
- Effect of derivation types on class members · Object Oriented Programming, chapter 6
- Create classes from design class diagrams · Object Oriented Analysis and Design, chapter 4 · asked 2×
- Identify conceptual classes; create domain model · Object Oriented Analysis and Design, chapter 2
- Student course department class diagram · Object Oriented Analysis and Design, chapter 2
- Interpret: teaching values to professionals · Communication English, chapter 1 · asked 2×