Tuesday, September 26, 2017

6. DEMONSTRATE USE OF VIRTUAL AND OVERRIDE KEY WORDS IN C# WITH A SIMPLE PROGRAM

using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
namespace VirtualKey
{
class person
{
protected string fname; protected string lname;
public person(string fn, string ln)
{
fname = fn; lname = ln;
}
public virtual void display()
{
Console.WriteLine("Person  :" + fname + " " + lname);
}
}
class emp : person
{
public ushort year;
public emp(string fn, string ln, ushort yr) : base(fn, ln)
{
year = yr;
}
public override void display()
{
Console.WriteLine("Employee :"+fname+" "+lname+" "+year);
}
}
class worker : person
{
public String company;
public worker(string fn, string ln, string c):base(fn, ln)
{
company=c;
}
public override void display()
{
Console.WriteLine("Worker :" + fname + " " + lname + " " +company);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\n\n*** VIRTUAL AND OVERRIDE KEYWORDS DEMO ***");
person p1 = new person("RAVI", "KUMAR"); person p2 = new emp("RAVI", "KUMAR",2012);
person p3 = new worker("RAVI", "KUMAR","XYZ SOLUTIONS"); p1.display();
p2.display();
p3.display();
Console.ReadLine();
}
}
}

No comments:

Post a Comment