Question: In C sharp You need to write a parent class and 2 child sub classes. After creating 4 child objects of each type and putting
In C sharp
You need to write a parent class and 2 child sub classes.
After creating 4 child objects of each type and putting them into an array, you will write a foreach loop and call the objects Print method.
=================================================================
Start a new Visual Studio console project
- Add a new class called Vehicle (You can add this class in the same file as program.cs, or on its own file in the same project, or you can do it right and add a new project, a ClassLibrary project. We have done it all 3 ways so you can look at prior projects to make sure you get this correct.)
>> The Vehicle class should have a string property called Color.
>> The Vehicle class should have an int property called Year. (We will just use an int for years such as 2021, or 1987, etc. Not really dates, just a year.)
>> The Vehicle class should have a constructor that takes in 2 values and sets the values of the 2 properties.
>> Add a virtual Print method, which takes no parameter, and writes The color of this vehicle is color and its model year is Year Where the value of color comes from its Color prop and the value of Year comes from the Year property. So for example, it might write out: The color of this vehicle is blue and its model year is 2015
- Now add another new class, called Car, it should inherit from the Vehicle class.
>> The Car class should have a constructor that takes in 2 values (for color and year) and passes those values into the parents constructor to set the values of the 2 properties.
>> Give it a Print method, which overrides the parents Print method. It should do the same thing as the parents method, except it should write the text out in green letters.
- Now add another new class, called Truck, it should inherit from the Vehicle class.
>> The Car class should have a constructor that takes in 2 values (for color and year) and passes those values into the parents constructor to set the values of the 2 properties.
>> Give it a Print method, which overrides the parents Print method. It should do the same thing as the parents method, except it should write the text out in red letters.
- Now in the Main method of your program.cs, instantiate 4 objects (2 objects of type Car and 2 objects of type Truck) and pass in values to the constructors for Color and Year, use any values you like.
- Create a new array of type Vehicle and size 4, and add your 4 objects to the array.
- Using a foreach loop, call the Print method on each array element Your output should look something like:
The color of this vehicle is red and its model year is 2015
The color of this vehicle is blue and its model year is 2021
The color of this vehicle is black and its model year is 2019
The color of this vehicle is gray and its model year is 2003
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
