-
Notifications
You must be signed in to change notification settings - Fork 1
/
32Constructors.cpp
30 lines (27 loc) · 969 Bytes
/
32Constructors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include<conio.h>
#include<string.h>
using namespace std;
//A constructor in C++ is a special method that is automatically called when an object of a class is created.
// The constructor has the same name as the class, it is always public, and it does not have any return value.
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
brand = x;
model = y;
year = z;
}
};
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
//Navjot Singh Prince