-
Notifications
You must be signed in to change notification settings - Fork 0
/
constructorandDeconstructor.cpp
77 lines (68 loc) · 1.91 KB
/
constructorandDeconstructor.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
using namespace std;
class Cars
{
string company;
string model;
string fuel_type;
float mileage;
double price;
public:
Cars()
{
cout << " DEFAULT CONSTRUCTOR " << endl;
}
Cars(string cname, string mname, string ftype, float m, double p)
{
cout << " PARAMETERIZED CONSTRUCTOR " << endl;
company = cname;
model = mname;
fuel_type = ftype;
mileage = m;
price = p;
}
Cars(Cars &obj)
{
cout << " copy CONSTRUCTOR " << endl;
company = obj.company;
model = obj.model;
fuel_type = obj.fuel_type;
mileage = obj.mileage;
price = obj.price;
}
void setData(string cname, string mname, string ftype, float m, double p)
{
company = cname;
model = mname;
fuel_type = ftype;
mileage = m;
price = p;
}
void displayData()
{
cout << "Car properties: " << endl;
cout << "BRAND NAME:- " << company << endl;
cout << "MODEL:- " << model << endl;
cout << "FUEL TYPE:- " << fuel_type << endl;
cout << "MILEAGE:- " << mileage << endl;
cout << "PRICE:- " << price << endl
<< endl;
}
~Cars()
{
cout << "DESTRUCTOR CALLED" << endl;
/*we will get this destructor called three times becuse it was clled for all the three objects.
and for dynamic memory allocation dalocation will happen in this part*/
}
};
int main()
{
Cars car1, car2("Bugatti", "Chhiron", "Petrol", 4.5, 499000);
car1.setData("Lamborghini", "Aventador", "Petrol", 5.5, 501000);
car1.displayData();
car2.displayData();
Cars car3 = car1;
car3.displayData();
return 0;
}