0% found this document useful (0 votes)
53 views

Public Int Public String Public Int String This This Public Void Int String This This

The document contains two examples of inheritance in C#: 1) An Employee class inherits from a Person class, allowing employees to have attributes like a name and age from the Person class. The Employee class adds attributes for base salary level and bonus level. 2) A listbox class inherits from a window class, allowing listboxes to have the same location attributes as windows. The listbox adds a string attribute to hold contents. It overrides the DrawWindow method to invoke the parent method and then write the listbox contents.

Uploaded by

binghazi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Public Int Public String Public Int String This This Public Void Int String This This

The document contains two examples of inheritance in C#: 1) An Employee class inherits from a Person class, allowing employees to have attributes like a name and age from the Person class. The Employee class adds attributes for base salary level and bonus level. 2) A listbox class inherits from a window class, allowing listboxes to have the same location attributes as windows. The listbox adds a string attribute to hold contents. It overrides the DrawWindow method to invoke the parent method and then write the listbox contents.

Uploaded by

binghazi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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