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

Popular posts from this blog

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_