221: // In Task 2, we discussed multilevel inheritance with parameterized constructors for Student, UndergraduateStudent, and GraduateStudent classes in a university management system. Can you explain the advantages of using multilevel inheritance with specific details about the functions and data members in these classes? How were the parameterized constructors (e.g., setting student name, age, and ID) used to ensure that each class in the hierarchy correctly initializes its properties, such as creating an UndergraduateStudent named "John," aged 20, with a student ID of 12345
// In Task 2, we discussed multilevel inheritance with parameterized constructors for Student,
UndergraduateStudent, and GraduateStudent classes in a university management system.
Can you explain the advantages of using multilevel inheritance with specific
details about the functions and data members in these classes? How were the parameterized
constructors (e.g., setting student name, age, and ID) used to ensure that each
class in the hierarchy correctly initializes its properties.
#include <iostream>
using namespace std;
class Student
{
private:
string name, ID;
int age;
public:
Student() { name = "", ID = "", age = 0; }
Student(string n, string i, int a) { name = n, ID = i, age = a; }
void setName(string n) { name = n; }
void setAge(int a) { age = a; }
void setID(string i) { ID = i; }
string getName() { return name; }
string getID() { return ID; }
int getAge() { return age; }
};
class UndergraduateStudent : protected Student
{
private:
string status;
public:
UndergraduateStudent() : Student("", "", 0) { status = "Undergraduate"; }
UndergraduateStudent(string n, string i, int a) : Student(n, i, a) { status = "Undergraduate"; }
void print()
{
cout << "Name: " << getName() << endl
<< "ID: " << getID() << endl
<< "Age: " << getAge() << endl
<< "Status: " << status << endl
<< endl;
}
};
class GraduatedStudent : protected Student
{
private:
string status;
public:
GraduatedStudent() : Student("", "", 0) { status = "Graduated"; }
GraduatedStudent(string n, string i, int a) : Student(n, i, a) { status = "Graduated"; }
void print()
{
cout << "Name: " << getName() << endl
<< "ID: " << getID() << endl
<< "Age: " << getAge() << endl
<< "Status: " << status << endl
<< endl;
}
};
int main()
{
UndergraduateStudent ug1("Khatak", "34534642", 19);
GraduatedStudent g1("Ehtisham", "93209409", 27);
ug1.print();
g1.print();
return 0;
}
Comments
Post a Comment