0% found this document useful (0 votes)
26 views6 pages

ThucHanh2 Interface

Uploaded by

lmht312113
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

ThucHanh2 Interface

Uploaded by

lmht312113
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Write a program to demonstrate Inheritance.

Define a base class Vehicle having


properties like type, color, speed, brand and methods Run() and Display(). The Run()
method should display a message “I am running” and the type of the vehicle. The
Display() method should display the various properties of the vehicle. Derive a class Car
and initialized the derived attributes of base class Vehicle. Finally, in the Main() method
execute Run() and Display()using an object of the derived class Car.

Solution:

The output of the program is as shown in the Figure 2.4.

1
Figure 2.4: Output of Vehicle.cs

2. Consider the previous question. Override the Run() method in the derived class to display a
message “The CAR is running”.

Solution:

The output of the program is as shown in the Figure 2.5.

Figure 2.5: Output of Vehicle.cs

3. Write a program to display the value of two integers using parameterized constructor and a
method. Create two objects of this and pass the sets of values (10, 20) and (30, 40)

2
respectively. Overload the ‘+’ operator to add the two x values and two y values of these
objects to form a new object (x, y). The answer should be (10+30, 20+40) = (40, 60).

Solution:

The output of the program is as shown in the Figure 2.6.

Figure 2.6: Output of OperatorOvrldDemo.cs

4. Write a program to demonstrate the use of abstract class. Create two derived classes Blue
and Green based on a generic Color class. Color class should define a template for a
method Fill(string colorname). This method should be implemented by the classes
Blue and Green. The Fill method should display a message “Fill me up with” and then
the color name.

Solution:

3
using System;

abstract class Color


{
public abstract void Fill(string strColor);
}
class Blue : Color
{
public override void Fill(string strColor)
{
Console.WriteLine("Fill me up with " + strColor);
}
}
class Green : Color
{
public override void Fill(string strColor)
{
Console.WriteLine("Fill me up with " + strColor);
}
}

class ColorDemo
{
static void Main()
{
Blue b = new Blue();
b.Fill("Blue");

Green g = new Green();


g.Fill("Green");
}
}

The output of the above program is as shown in the Figure 3.1.

Figure 3.1: Output of Color.cs

5. Write a program to demonstrate polymorphism and the use of keyword base. Declare a class
AppWindow having a virtual method CreateWindow(), displaying message “Window:
drawing Window at top, left” where top and left are integer variables initialized in the
constructor. Derive a class ListBox from AppWindow and initialize three parameters in its
constructor; top, left and a string variable listBoxContents. Override
CreateWindow() with the message “Writing string to the listbox: listBoxContents”

4
and also display the virtual method of the base class. Derive another class Button from
AppWindow and override the virtual method CreateWindow() with the message
“Drawing a button at top, left”.

Execute CreateWindow() method for all the three classes.

Solution:

using System;

public class AppWindow


{
//Protected members
protected int top;
protected int left;

// constructor takes two integers to fix location on the


console

public AppWindow(int top, int left)


{
this.top = top;
this.left = left;
}
// simulates drawing the AppWindow
public virtual void CreateWindow()
{
Console.WriteLine("Window: drawing Window at {0}, {1}",
top, left);
}

}
// ListBox derives from AppWindow
public class ListBox : AppWindow
{
private string listBoxContents;

// constructor adds a parameter and also call base constructor

public ListBox( int top, int left, string contents):base(top,


left)
{
listBoxContents = contents;
}

// Overriding CreateWindow
public override void CreateWindow()
{
base.CreateWindow(); // invoking base method
Console.WriteLine ("Writing string to the listbox: {0}",
listBoxContents);

5
}

}
// Button derives from AppWindow
public class Button : AppWindow
{
public Button(int top, int left): base(top, left)
{
}

// Overriding CreateWindow
public override void CreateWindow( )
{
Console.WriteLine("Drawing a button at {0}, {1}\n", top,
left);
}
}
public class Tester
{
public static void Main()
{
AppWindow win = new AppWindow(-110,-0);
win.CreateWindow( );
win = new ListBox(3,4,"This is a list box");
win.CreateWindow();
win = new Button(5,6);
win.CreateWindow();
}
}

The output of the program is as shown in the Figure 3.2.

Figure 3.2: Output of AppWindow.cs

You might also like