Create a base class Person with member variables for name and age, and a method displayInfo(). Derive a class Student from Person with an additional member variable for the student ID and override the displayInfo() method to include the student ID
#include <iostream>
#include <string>
using namespace std;
// Base class Person
class Person {
protected:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
virtual void displayInfo() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
// Derived class Student from Person
class Student : public Person {
private:
int studentID;
public:
Student(string n, int a, int id) : Person(n, a), studentID(id) {}
void displayInfo() override {
cout << "Name: " << name << ", Age: " << age << ", Student ID: " << studentID << endl;
}
};
int main() {
Person person("Alice", 30);
Student student("Bob", 20, 12345);
cout << "Person Info:" << endl;
person.displayInfo();
cout << "Student Info:" << endl;
student.displayInfo();
return 0;
}