0
0
C Sharp (C#)programming~30 mins

Base class and derived class in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Base class and derived class
📖 Scenario: You are creating a simple program to represent vehicles. Each vehicle has a brand name. Cars are a special type of vehicle that also have a model name.
🎯 Goal: Build a base class called Vehicle with a brand field, then create a derived class called Car that adds a model field. Finally, display the brand and model of a car.
📋 What You'll Learn
Create a base class named Vehicle with a public string field brand
Create a derived class named Car that inherits from Vehicle
Add a public string field model to the Car class
Create an instance of Car with brand "Toyota" and model "Corolla"
Print the brand and model of the car in the format: "Brand: Toyota, Model: Corolla"
💡 Why This Matters
🌍 Real World
Understanding base and derived classes helps organize code when working with related objects, like different types of vehicles.
💼 Career
Inheritance is a key concept in object-oriented programming used in many software development jobs to create reusable and organized code.
Progress0 / 4 steps
1
Create the base class Vehicle
Create a public class called Vehicle with a public string field named brand.
C Sharp (C#)
Need a hint?

Think of Vehicle as a general type that holds the brand name.

2
Create the derived class Car
Create a public class called Car that inherits from Vehicle. Add a public string field named model to the Car class.
C Sharp (C#)
Need a hint?

Use : to inherit from the base class Vehicle.

3
Create an instance of Car and set fields
Create a new Car object named myCar. Set its brand field to "Toyota" and its model field to "Corolla".
C Sharp (C#)
Need a hint?

Remember to create the object with new Car() and then set the fields.

4
Print the brand and model of the car
Write a Console.WriteLine statement to print the brand and model of myCar in the format: "Brand: Toyota, Model: Corolla".
C Sharp (C#)
Need a hint?

Use Console.WriteLine with string interpolation to show the values.