Create a person class and instantiate an object student based on this Class. The class can set an age, say hello, display her age on the screen and start the explanation.
Example of the output
My age is 21 years old
I'm studying
Hello!
I'm explaining
using System;
using System.Collections.Generic;
namespace App
{
class Person
{
protected int age;
public Person()
{
}
public virtual void Greet() // Method for greeting
{
Console.WriteLine("Hello!\nI'm explaining");
}
public void SetAge(int n) // Method for setting age
{
this.age = n;
}
}
class Student : Person
{
public Student()
{
}
public void DisplayAge()
{
Console.WriteLine("My age is " + age + " years old.");
}
public void Studying()
{
Console.WriteLine("I'm studying.");
}
}
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.SetAge(21);
student.DisplayAge();
student.Studying();
student.Greet();
Console.ReadLine();
}
}
}
Comments
Leave a comment