C# Exercise: 134 Classes Student + Teacher

In this exercise in C#, you will need to write a program that includes the Person class you just created. From this class, you will create two additional classes: Student and Teacher, both descendants of Person. The Student class will have a public method named "GoToClasses", which will print "I’m going to class" on the screen. The Teacher class will have a public method named "Explain", which will display "Explanation begins" on the screen. Also, the Teacher class will have a private attribute called "subject" of type string. The Person class must include a method called "SetAge(int n)" to set the age of the person. The student will have a public method called "ShowAge", which will print "My age is: 20 years old" on the screen. You will also need to create a test class called "StudentAndTeacherTest", which will contain the Main method and will: create a person and make them greet, create a student, set their age to 21, make them greet and display their age, and create a teacher aged 30, make them greet and then explain. This exercise will help you deepen your understanding of creating and inheriting classes, using public and private methods, and organizing tests within a main class in C#.

This exercise will help improve your skills in object-oriented programming (OOP) and allow you to better manage relationships between classes and instances in your C# projects.



 Exercise

Classes Student + Teacher

 Objetive

Write a C# program that include in it the class Person that you just created.

Create a class "Student" and another class "Teacher", both descendants of "Person".

The class "Student" will have a public method "GoToClasses", which will write on screen "I’m going to class."

The class "Teacher" will have a public method "Explain", which will show on screen "Explanation begins". Also, it will have a private attribute "subject", a string.

The class Person must have a method "SetAge (int n)" which will indicate the value of their age (eg, 20 years old).

The student will have a public method "ShowAge" which will write on the screen "My age is: 20 years old" (or the corresponding number).

You must create another test class called "StudentAndTeacherTest" that will contain "Main" and:
Create a Person and make it say hello
Create a student, set his age to 21, tell him to Greet and display his age
Create a teacher, 30 years old, ask him to say hello and then explain.

 Example Code

// Import the System namespace for basic functionality
using System;

// Define the base class Person
public class Person
{
    // Declare a public integer field for age
    public int age;

    // Define a public method to set the age of the person
    public void SetAge(int n)
    {
        // Assign the value of n to the age field
        age = n;
    }

    // Define a public method for greeting
    public void Greet()
    {
        // Print a greeting message to the console
        Console.WriteLine("Hello!");
    }
}

// Define the derived class Student, inheriting from Person
public class Student : Person
{
    // Define a public method that indicates the student is going to classes
    public void GoToClasses()
    {
        // Print a message to indicate going to class
        Console.WriteLine("I'm going to class.");
    }

    // Define a public method to show the student's age
    public void ShowAge()
    {
        // Print the age message with the current age
        Console.WriteLine("My age is: " + age + " years old.");
    }
}

// Define the derived class Teacher, inheriting from Person
public class Teacher : Person
{
    // Declare a private string field for the subject
    private string subject;

    // Define a public method that indicates the teacher is explaining
    public void Explain()
    {
        // Print a message to indicate that an explanation is beginning
        Console.WriteLine("Explanation begins.");
    }

    // Define a method to set the teacher's subject
    public void SetSubject(string subjectName)
    {
        // Assign the value of subjectName to the subject field
        subject = subjectName;
    }
}

// Define a test class to execute the program
public class StudentAndTeacherTest
{
    // Main method to run the program
    public static void Main()
    {
        // Create a Person object and make it say hello
        Person person = new Person();
        person.Greet();

        // Create a Student object, set its age, greet, and show its age
        Student student = new Student();
        student.SetAge(21);
        student.Greet();
        student.ShowAge();
        student.GoToClasses();

        // Create a Teacher object, set its age, greet, and explain
        Teacher teacher = new Teacher();
        teacher.SetAge(30);
        teacher.Greet();
        teacher.Explain();
    }
}