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

Lab1.11 Constructor

dotnet labreport

Uploaded by

karantestingdemo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab1.11 Constructor

dotnet labreport

Uploaded by

karantestingdemo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab1
{
class Car
{
public string Make;
public string Model;
public int Year;
public string Color;

// Default constructor
// This is automatically provided by C# if no constructor is defined

// Parameterless constructor
public Car()
{
Make = "Unknown";
Model = "Generic";
Year = DateTime.Now.Year;
Color = "White";
}

// Parameterized constructor
public Car(string make, string model, int year, string color)
{
Make = make;
Model = model;
Year = year;
Color = color;
}

public void DisplayInfo()


{
Console.WriteLine($"Car: {Year} {Make} {Model}, Color: {Color}");
}
}

class Program
{
static void Main(string[] args)
{
// Using default constructor
Car car1 = new Car();
Console.WriteLine("Car 1 (Default Constructor):");
car1.DisplayInfo();

// Using parameterless constructor


Car car2 = new Car();
Console.WriteLine("\nCar 2 (Parameterless Constructor):");
car2.DisplayInfo();

// Using parameterized constructor


Car car3 = new Car("Toyota", "Camry", 2022, "Blue");
Console.WriteLine("\nCar 3 (Parameterized Constructor):");
car3.DisplayInfo();
}
}
}

You might also like