In an Application console Project, Give instructions that:
- Create a class "Person"
- Create two classes “Student” and “Teacher”, both inherit from the class "Person".
- The class "Student" will have a public method "GoToClasses", which will display on the screen "I’m going to class. ".
- The class "Person" must have a method "SetAge (int n)" which will indicate the value of their age (for example, 20 years old).
- The class "Student" will have a public method "DisplayAge" which will write on the screen "My age is: XX years old".
- You must create another test class called "Test" which will contain "static void Main()" .
- Create a Person object and make it say "Hello"
- Create a Student object, set its age to 20, make it say "Hello", “I'm going to class. And display his age. - Create a Teacher object, 50, ask him to say "Hello" and then begin the course.
using System;
using System.Collections.Generic;
namespace App
{
class Person
{
protected string name;
protected int age;
public Person(string name)
{
this.name = name;
}
public virtual void Greet() // Method for greeting
{
Console.WriteLine("Person " + name + " says hello.");
}
public void SetAge(int n) // Method for setting age
{
this.age = n;
}
}
class Student : Person
{
public Student(string name)
: base(name)
{
}
public void DisplayAge()
{
Console.WriteLine("My age is: " + age + " years old.");
}
public void GoToClasses()
{
Console.WriteLine("I'm going to class.");
}
public override void Greet() // Overridden method for greeting in class Student
{
Console.WriteLine("Student " + name + " says Hello.");
}
}
class Teacher : Person
{
public Teacher(string name)
: base(name)
{
}
public override void Greet() // Overridden method for greeting in class Teacher
{
Console.WriteLine("Teacher " + name + " says Hello.");
}
}
class Program
{
static void Main(string[] args)
{
Person person = new Person("Peter");
person.Greet();
Student student = new Student("Mary");
student.SetAge(20);
student.Greet();
student.DisplayAge();
Teacher teacher = new Teacher("Mike");
teacher.SetAge(30);
teacher.Greet();
Console.ReadLine();
}
}
}
Comments
Leave a comment