#include <iostream>
using namespace std;
// Base class
class Person {
protected:
string name;
int age;
public:
// Parameterized constructor
Person(string n, int a) {
name = n;
age = a;
cout << "Person constructor called with name = " << name << " and age = " << age << endl;
}
};
// Derived class
class Student : public Person {
private:
string studentID;
public:
// COnstructor for Student that intializes base class (Person) and studentID
Student(string n, int a, string id) : Person(n, a), // Call the base class constructor with name and age
studentID(id) {
// Constructor body (can be empty if no additional initialization is needed)
}
// Method to display student info
void displayStudentInfo() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Student ID: " << studentID << endl;
}
};
int main() {
Student student("Rahul", 20, "50342324");
student.displayStudentInfo();
return 0;
}
No comments:
Post a Comment