C# Exercise 7

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


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:

using System;

class Person
{
    private string firstName;
    private string lastName;
    protected int age;

    public Person(string fname, string lname, int age)
    {
        Console.WriteLine("Person() called");
        this.firstName = fname;
        this.lastName = lname;
        this.age = age;
    }

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    public int Age
    {
        get { return age; }
        set { age = value; }
    }
}

class Student : Person
{
    protected float cgpa;

    public Student(string fname, string lname, int age, float c_gpa) : base(fname, lname, age)
    {
        Console.WriteLine("Student() called");
        this.cgpa = c_gpa;
    }

    public float CGPA
    {
        get { return cgpa; }
        set { cgpa = value; }
    }
}

class Undergraduate : Student
{
    private string fypName;

    public Undergraduate(string fname, string lname, int age, float c_gpa, string fyp_n) : base(fname, lname, age, c_gpa)
    {
        Console.WriteLine("Undergraduate() called");
        this.fypName = fyp_n;
    }

    public string FYPName
    {
        get { return fypName; }
        set { fypName = value; }
    }
}

class Graduate : Student
{
    private string thesisTopic;

    public Graduate(string fname, string lname, int age, float c_gpa, string thesistopic) : base(fname, lname, age, c_gpa)
    {
        Console.WriteLine("Graduate() called");
        this.thesisTopic = thesistopic;
    }

    public string ThesisTopic
    {
        get { return thesisTopic; }
        set { thesisTopic = value; }
    }
}

class Faculty : Person
{
    private int courseCount;
    private string extensionNumber;

    public Faculty(string fname, string lname, int age, int courseCount, string extensionNumber) : base(fname, lname, age)
    {
        Console.WriteLine("Faculty() called");
        this.courseCount = courseCount;
        this.extensionNumber = extensionNumber;
    }

    public int CourseCount
    {
        get { return courseCount; }
        set { courseCount = value; }
    }

    public string ExtensionNumber
    {
        get { return extensionNumber; }
        set { extensionNumber = value; }
    }
}

class Program
{
    static void Main()
    {
        Undergraduate ted = new Undergraduate("Ted", "Thompson", 22, 3.91f, "Some FYP Name");
        Faculty richard = new Faculty("Richard", "Karp", 45, 2, "420");

        Console.WriteLine($"Undergraduate: {ted.FirstName} {ted.LastName} ({ted.Age} years old, CGPA: {ted.CGPA}, FYP Name: {ted.FYPName})");
        Console.WriteLine($"Faculty: {richard.FirstName} {richard.LastName} ({richard.Age} years old, Teaching {richard.CourseCount} courses this semester, Extension Number: {richard.ExtensionNumber})");
    }
}

 

Post a Comment

0Comments

Post a Comment (0)

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

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