You are on page 1of 4

Inheritance Example

class Person
{
public int age;
public string name;

public Person(int age, string name)


{
this.age = age;
this.name = name;
}
public void getname(int age, string name)
{

this.name = name;
this.age = age;
Console.WriteLine("The age of person is {0},
{1}", name, age);

}
class Employee:Person

{
public int baseLevel;
public int bonusLevel;
public int salary;
public Employee(int age,string name, int
baseLevel, int bonusLevel)
: base(age,name)
{
this.baseLevel = baseLevel;
this.bonusLevel = bonusLevel;
Console.WriteLine("This is child class
constructor {0},{1},{2},{3}",age,name, baseLevel,
bonusLevel);
}
public void TakeSalary()
{
salary = baseLevel + bonusLevel;
Console.WriteLine("The salary of
employee is {0}", salary);

} }}

class Program
{
static void Main(string[] args)
{

Employee emp = new


Employee(50,"Ahmed",500,200);

emp.getname(100,"qayoom");
emp.TakeSalary();
Console.ReadKey();
}

}
Program#2

class window
{
public int top;
public int left;
// constructor takes two integers to
// fix location on the console
public window( int top, int left )
{
this.top = top;
this.left = left;
}

// simulates drawing the window


public void DrawWindow( )
{
Console.WriteLine( "Drawing Window at {0},
{1}",top, left );
} }

class listbox:window
{
public string mListBoxContents;
// constructor adds a parameter
public listbox( int top, int left, string
theContents ):
base( top, left ) // call base constructor
{
mListBoxContents = theContents;
}

// a new version (note keyword) because in the


// derived method we change the behavior
public new void DrawWindow( )
{
base.DrawWindow( ); // invoke the base
method Console.WriteLine( "Writing string to the
listbox: {0}", mListBoxContents );
} }

class Program
{
static void Main(string[] args)
{
window w = new window(5, 10);
w.DrawWindow();

// create a derived instance


listbox lb = new listbox(20, 30, "Hello
world");
lb.DrawWindow();
Console.ReadLine();

}
}

You might also like