0% found this document useful (0 votes)
8 views2 pages

Constructor Overloading in C++ Car Class

The document presents a C++ program that demonstrates constructor overloading in a 'Car' class. It includes a default constructor, a parameterized constructor with one argument, and another with two arguments to initialize car details. The program creates three car objects using different constructors and displays their details.

Uploaded by

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

Constructor Overloading in C++ Car Class

The document presents a C++ program that demonstrates constructor overloading in a 'Car' class. It includes a default constructor, a parameterized constructor with one argument, and another with two arguments to initialize car details. The program creates three car objects using different constructors and displays their details.

Uploaded by

Dhruv Garg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

/* Write a Program using Constructor Overloading*/

#include <iostream>
using namespace std;

class Car {
private:
string brand;
int year;

public:
// Default Constructor
Car() {
brand = "Unknown";
year = 0;
}

// Parameterized Constructor (1 argument)


Car(string b) {
brand = b;
year = 2024; // Default year
}

// Parameterized Constructor (2 arguments)


Car(string b, int y) {
brand = b;
year = y;
}

// Function to display car details


void display() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};

int main() {
// Using different constructors
Car car1; // Calls default constructor
Car car2("Honda"); // Calls constructor with one parameter
Car car3("Ford", 2020); // Calls constructor with two parameters

// Displaying details
cout << "Car 1: "; [Link]();
cout << "Car 2: "; [Link]();
cout << "Car 3: "; [Link]();

return 0;
}
*********************************OUTPUT***********************************
Car 1: Brand: Unknown, Year: 0
Car 2: Brand: Honda, Year: 2024
Car 3: Brand: Ford, Year: 2020

You might also like