---
title: "Abstract Student class program"
subject: "Object Oriented Programming"
chapter: 7
key: "semester_3/object_oriented_programming:student-abstract-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/student-abstract-program
site: BCT Engineering
---

# Abstract Student class program

Object Oriented Programming · Chapter 7: Polymorphism and Dynamic Binding

- Asked in: 2070 Asar, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/student-abstract-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-7

## Question

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.

## 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.

## Answer

`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.

```cpp
#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:**

```text
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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What makes Student an abstract class in the program?
   A. It has protected data members
   B. It has pure virtual functions getdata() and display()
   C. It has a virtual destructor
   D. It is a base class
   Answer: B. Pure virtual functions (= 0) make the class abstract so new Student is not allowed. (Key point: Why Student is abstract)

2. Why does s[i]->display() call the correct version for each student?
   A. display() is virtual and the base pointer holds the actual derived object
   B. The loop index selects the class
   C. Each class has a different function name
   D. Pointers always call the derived version
   Answer: A. Virtual functions are resolved by the actual object type at run time. (Key point: Run-time binding)

3. Why does Student have a virtual destructor?
   A. To make Student abstract
   B. So deleting derived objects through Student pointers cleans up correctly
   C. To allow creation of Student objects
   D. To read input
   Answer: B. A virtual destructor ensures the derived destructor runs when deleting via a base pointer. (Key point: Virtual destructor role)

## Related in Object Oriented Programming

- [Polygon abstract class with Area()](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apolygon-abstract-area) — Object Oriented Programming, chapter 7
- [Pure virtual function and abstract class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apure-virtual-abstract-class) — Object Oriented Programming, chapter 7, asked 5×
- [Access overridden members from derived class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aaccess-overridden-members) — Object Oriented Programming, chapter 6, asked 2×
- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Abstract class with example](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aabstract-class-example) — Object Oriented Programming, chapter 7
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6

## Related in other subjects

- [Student records with two functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-student-read-display-functions) — Computer Programming, chapter 8
- [Create classes from design class diagrams](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aclasses-from-design-class-diagram) — Object Oriented Analysis and Design, chapter 4, asked 2×
- [Student course department class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Astudent-course-diagram-type) — Object Oriented Analysis and Design, chapter 2
- [OS as virtual machine; functions](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Aos-virtual-machine-functions) — Operating System, chapter 1
