- 2070 Asar · 6 marks
Abstract Student class program
Short answer
An abstract Student class declares pure virtual functions getdata() and display(). Derived Engineering, Science, Medical classes implement them. A base pointer array calls each object's overridden methods at run time, demonstrating polymorphism and code reuse.
Write a program having student as an abstract class and create derived class such as Engineering, Science and Medical. Show the use of virtual functions in this program.
Student is an abstract class with pure virtual functions getdata() and display(). Concrete classes Engineering, Science and Medical derive from it and override these functions. A base pointer array is used to call the correct version at run time.
#include <iostream>
#include <string>
using namespace std;
class Student { // abstract class
protected:
string name;
int roll;
public:
virtual void getdata() = 0; // pure virtual functions
virtual void display() = 0;
virtual ~Student() {}
};
class Engineering : public Student {
string branch;
public:
void getdata() {
cout << "Engineering - name, roll, branch: ";
cin >> name >> roll >> branch;
}
void display() {
cout << "[Engineering] " << name << ", Roll " << roll
<< ", Branch " << branch << endl;
}
};
class Science : public Student {
string subject;
public:
void getdata() {
cout << "Science - name, roll, major subject: ";
cin >> name >> roll >> subject;
}
void display() {
cout << "[Science] " << name << ", Roll " << roll
<< ", Major " << subject << endl;
}
};
class Medical : public Student {
string specialization;
public:
void getdata() {
cout << "Medical - name, roll, specialization: ";
cin >> name >> roll >> specialization;
}
void display() {
cout << "[Medical] " << name << ", Roll " << roll
<< ", Specialization " << specialization << endl;
}
};
int main() {
Student *s[3];
s[0] = new Engineering;
s[1] = new Science;
s[2] = new Medical;
for (int i = 0; i < 3; i++) s[i]->getdata(); // run-time binding
cout << "\nStudent records" << endl;
for (int i = 0; i < 3; i++) s[i]->display();
for (int i = 0; i < 3; i++) delete s[i];
return 0;
}
Sample run:
Engineering - name, roll, branch: Ram 1 Computer
Science - name, roll, major subject: Sita 2 Physics
Medical - name, roll, specialization: Hari 3 Surgery
Student records
[Engineering] Ram, Roll 1, Branch Computer
[Science] Sita, Roll 2, Major Physics
[Medical] Hari, Roll 3, Specialization Surgery
new Student is not allowed because the class is abstract. The virtual functions make s[i]->display() call the version of the actual object. The virtual destructor ensures correct clean-up through base pointers.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Polygon abstract class with Area() · Object Oriented Programming, chapter 7
- Pure virtual function and abstract class · Object Oriented Programming, chapter 7 · asked 5×
- Access overridden members from derived class · Object Oriented Programming, chapter 6 · asked 2×
- File stream; student binary file program · Object Oriented Programming, chapter 8
- Abstract class with example · Object Oriented Programming, chapter 7
- Protected specifier; student/test/result program · Object Oriented Programming, chapter 6
- Student records with two functions · Computer Programming, chapter 8
- Create classes from design class diagrams · Object Oriented Analysis and Design, chapter 4 · asked 2×
- Student course department class diagram · Object Oriented Analysis and Design, chapter 2
- OS as virtual machine; functions · Operating System, chapter 1