C# Easy homework

profileMbahattab
assignment_6_-_fall2015.docx

COMP 2240-01 Homework 6

(Due 11/30/2015)

1. In this problem, there is no override behavior. It is a practice on inheritance concept and syntax.

Given the following class definition, complete the definition of the constructors for class car and class truck.

namespace Assignment_6

{

class vehicle

{

protected string make, model;

protected int year;

protected int wheels;

protected double weight;

public vehicle()

{

make = "Unknown";

model = "Unknown";

year = 0;

wheels = 0;

weight = 0.0;

}

public vehicle(string mk, string md, int yr, int wl, double wt)

{

make = mk;

model = md;

year = yr;

wheels= wl;

weight = wt;

}

public void DisplayVehicle()

{

Console.WriteLine("Make: {0}\nModel: {1}\nYear: {2}\nwheels: {3}\nWeight: {4}", make, model, year, wheels, weight);

}

}

class car : vehicle

{

private int nPassenger;

//Add a constructor for this car class that can initialize all the member //variables including those inherited members

public void DisplayVehicleCar()

{

DisplayVehicle();

Console.WriteLine("Passenger Load: {0}", nPassenger);

}

}

class truck : vehicle

{

private int nPassenger;

private int pay_load;

//Add a constructor for this truck class that can initialize all the member //variables including those inherited members

public void DisplayVehicleTruck()

{

DisplayVehicle();

Console.WriteLine("Passanger Load: {0}", nPassenger);

Console.WriteLine("Pay Load: {0}", pay_load);

}

}

class Program

{

static void Main(string[] args)

{

//Create an object of car with:

//Make: Ford; Model: Fusion; Year: 2014; Wheels: 4; weight: 2600;

//Passenger: 5

//Display all information about this car

//Create an object of truck with:

//Make: Chevy; Model: Silverado; Year: 2015; Wheels: 6; weight: 3600;

//Passenger: 4; Payload: 10000

//Display all information about this truck

}

}

}

2. Add two more classes, namely, minivan and SUV, both derived from vehicle class. Write code similar to class car and truck. Add corresponding constructors. Add DisplayVehicleMinivan(), and DisplayVehicleSUV() methods.

In the main, test your minivan and SUV class by creating an object of each (using your own data (make, model, year…etc). Display the two objects you created.

3. (This problem show the override behavior, aka. polymorphism)

Change the DisplayVehicle method to a vitual method in the vehicle class. Change all the DisplayVehicle*** methods defined in the derived classes to the same unique name DisplayVehicle() and make them “override” methods. Also delete the call to DisplayVehicl() from each overridden method DisplayVehicleMini***().

In the main, create an array of vehicle type. Fill in the array with some objects of car, truck, minivan and SUV. In a loop, display the info about each vehicle you filled into the array.