C++ Exercise 8

Mannan Ul Haq
0

Objective:

After completing this exercise, you will be able to:

  • Concept of Inheritance
  • Implement a base class and derived classes in C++
  • Understand the working of base class and derived class constructors/destructors
  • Understand the working and purpose of private and protected attributes of a base class


Activity:

Task 1:

Consider the following hierarchy as it exists in a university:
  • There are two types of persons in the university i.e. Student and Faculty
  • Every Person has some basic information that is common to all persons i.e. the first_name and last_name stored as private attributes and age which is a protected attribute.
  • A student can in turn be either an Undergraduate or a Graduate student, every student has a cgpa.
  • An undergraduate student has a fyp_name as his private attribute.
  • A graduate student has a thesis topic as his private attribute.
  • A faculty member has private attributes about the number of courses he is currently teaching, i.e. his course_count and a three digit telephone extension number. 

Implement the entire hierarchy of the Class i.e. define all the classes along with their attributes and their inheritance.

Task 2:

Add appropriate constructors and destructors to all the classes created in Exercise 2. For example the constructor for the Person class should take three inputs (for first_name, last_name and age). The student constructor should take four inputs, three for its parent class (i.e. Person) and one float value to be assigned to the cgpa attribute.

Task 3:

Add getters and setters function headings and implementation (resp) for all attributes in all the classes that you have defined.


Solution:

#include<iostream>
#include <string>
using namespace std;

class Person
{
private:
    string first_name;
    string last_name;
protected:
    int age;
public:
    Person(string fname, string lname, int a)
    {
        cout << "Person() called" << endl;
        first_name = fname;
        last_name = lname;
        age = a;
    }
    string get_first_name()
    {
        return first_name;
    }
    string get_last_name()
    {
        return last_name;
    }
    int get_age()
    {
        return age;
    }
    void set_first_name(string fname)
    {
        first_name = fname;
    }
    void set_last_name(string lname)
    {
        last_name = lname;
    }
    void set_age(int a)
    {
        age = a;
    }
    ~Person()
    {
        cout << "~Person() called" << endl;
    }
};

class Student : public Person
{
protected:
    float cgpa;
public:
    Student(string fname, string lname, int a, float c_gpa) : Person(fname, lname, a)
    {
        cout << "Student() called" << endl;
        cgpa = c_gpa;
    }
    float get_cgpa()
    {
        return cgpa;
    }
    void set_cgpa(float c_gpa)
    {
        cgpa = c_gpa;
    }
    ~Student()
    {
        cout << "~Student() called" << endl;
    }
};

class Undergraduate : public Student
{
private:
    string fyp_name;
public:
    Undergraduate(string fname, string lname, int a, float c_gpa, string fyp_n) : Student(fname, lname, a, c_gpa)
    {
        cout << "Undergraduate() called" << endl;
        fyp_name = fyp_n;
    }
    string get_fyp_name()
    {
        return fyp_name;
    }
    void set_fyp_name(string fyp_n)
    {
        fyp_name = fyp_n;
    }
    ~Undergraduate()
    {
        cout << "~Undergraduate() called" << endl;
    }
};

class Graduate : public Student
{
private:
    string thesis_topic;
public:
    Graduate(string fname, string lname, int a, float c_gpa, string thesistopic) : Student(fname, lname, a, c_gpa)
    {
        cout << "Graduate() called" << endl;
        thesis_topic = thesistopic;
    }
    string get_thesis_topic()
    {
        return thesis_topic;
    }
    void set_thesis_topic(string thesistopic)
    {
        thesis_topic = thesistopic;
    }
    ~Graduate()
    {
        cout << "~Graduate() called" << endl;
    }
};

class Faculty : public Person
{
private:
    int course_count;
    string extension_number;
public:
    Faculty(string fname, string lname, int a, int courseCount, string extensionNumber) : Person(fname, lname, a)
    {
        cout << "Faculty() called" << endl;
        course_count = courseCount;
        extension_number = extensionNumber;
    }
    int get_course_count()
    {
        return course_count;
    }
    void set_course_count(int courseCount)
    {
        course_count = courseCount;
    }
    string get_extension_number()
    {
        return extension_number;
    }
    void set_extension_number(string extensionNumber)
    {
        extension_number = extensionNumber;
    }
    ~Faculty()
    {
        cout << "~Faculty() called" << endl;
    }
};

int main()
{
    Undergraduate ted("Ted", "Thompson", 22, 3.91, "Some FYP Name");
    Faculty richard("Richard", "Karp", 45, 2, "420");
    cout << "Undergraduate: " << ted.get_first_name() << " " << ted.get_last_name() << " (" << ted.get_age() << " years old, CGPA: " << ted.get_cgpa() << ", FYP Name: " << ted.get_fyp_name() << ")" << endl;
    cout << "Faculty: " << richard.get_first_name() << " " << richard.get_last_name() << " (" << richard.get_age() << " years old, Teaching " << richard.get_course_count() << " courses this semester, Extension Number: " << richard.get_extension_number() << ")" << endl;

    return 0;
}

 

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !