Python Exercise 6

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 Python
  • Understand the working of base class and derived class initializers
  • Understand the working and purpose of private attributes of a base class


Activity:

Task 1:

- The university contains two categories of individuals: `Student` and `Faculty`.

- Each `Person` has fundamental attributes like `first_name` and `last_name`, which are private (denoted in Python with double underscores). The attribute `age` is protected (denoted in Python with a single underscore).

- A `Student` could be categorized as an `Undergraduate` or a `Graduate` student. Each student has an attribute `cgpa`.

  - An `Undergraduate` student possesses a private attribute `fyp_name`.

  - A `Graduate` student possesses a private attribute `thesis_topic`.

- A `Faculty` member has private details regarding the number of courses they're teaching, captured by `course_count`, and a three-digit telephone extension, `extension_number`.


Your goal is to implement the entire hierarchy. This means defining all classes, their attributes, and relationships (inheritance).


Task 2:

Introduce appropriate constructors to every class. For instance, the `Person` class's constructor should expect three parameters: `first_name`, `last_name`, and `age`. The `Student` constructor should take an additional float value for the `cgpa` apart from the parameters of its parent class (`Person`).

Task 3:

Integrate getter and setter methods for all attributes across all the defined classes.


Solution:

class Person:
    def __init__(self, first_name, last_name, age):
        self.__first_name = first_name
        self.__last_name = last_name
        self._age = age
        print("Person() called")

    def get_first_name(self):
        return self.__first_name

    def get_last_name(self):
        return self.__last_name

    def get_age(self):
        return self._age

    def set_first_name(self, first_name):
        self.__first_name = first_name

    def set_last_name(self, last_name):
        self.__last_name = last_name

    def set_age(self, age):
        self._age = age

    def __del__(self):
        print("~Person() called")


class Student(Person):
    def __init__(self, first_name, last_name, age, cgpa):
        super().__init__(first_name, last_name, age)
        self._cgpa = cgpa
        print("Student() called")

    def get_cgpa(self):
        return self._cgpa

    def set_cgpa(self, cgpa):
        self._cgpa = cgpa

    def __del__(self):
        print("~Student() called")


class Undergraduate(Student):
    def __init__(self, first_name, last_name, age, cgpa, fyp_name):
        super().__init__(first_name, last_name, age, cgpa)
        self.__fyp_name = fyp_name
        print("Undergraduate() called")

    def get_fyp_name(self):
        return self.__fyp_name

    def set_fyp_name(self, fyp_name):
        self.__fyp_name = fyp_name

    def __del__(self):
        print("~Undergraduate() called")


class Graduate(Student):
    def __init__(self, first_name, last_name, age, cgpa, thesis_topic):
        super().__init__(first_name, last_name, age, cgpa)
        self.__thesis_topic = thesis_topic
        print("Graduate() called")

    def get_thesis_topic(self):
        return self.__thesis_topic

    def set_thesis_topic(self, thesis_topic):
        self.__thesis_topic = thesis_topic

    def __del__(self):
        print("~Graduate() called")


class Faculty(Person):
    def __init__(self, first_name, last_name, age, course_count, extension_number):
        super().__init__(first_name, last_name, age)
        self.__course_count = course_count
        self.__extension_number = extension_number
        print("Faculty() called")

    def get_course_count(self):
        return self.__course_count

    def set_course_count(self, course_count):
        self.__course_count = course_count

    def get_extension_number(self):
        return self.__extension_number

    def set_extension_number(self, extension_number):
        self.__extension_number = extension_number

    def __del__(self):
        print("~Faculty() called")


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


main()

 

Post a Comment

0Comments

Post a Comment (0)

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

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