-
Notifications
You must be signed in to change notification settings - Fork 0
/
Untitled1.cpp
53 lines (52 loc) · 1.03 KB
/
Untitled1.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
#include<iostream>
using namespace std;
class car//class named car
{
private://access specifier
float petrol,distance;//instance variable:made for objects
public://access specifier
car(); //Default contructor
// defining inside class {
// petrol=70;//initializing
// distance=0;
// }
car(float a)//single parameterized
{
petrol=a;
distance=0;
}
car(float a,float b)//multiple parameterized contructor or constructor overloading)
{
petrol=a;
distance=b;
}
car(car &n)//copy constructor &n is reference to another object of same type
{
petrol=n.petrol;
distance=n.distance;
}
~car()
{
printf("An Object have been destroyed!");
}
void show();
};
car::car()//defining outside class
{
petrol=0;//initializing
distance=0;
}
void car::show()
{
cout<<"Petrol= "<<petrol<<" litres"<<endl;
cout<<"Distance Traveled"<<distance<<" km"<<endl;
}
int main()
{
car hondacity(5,10),civic,swift(1),bmw(hondacity);//objects
hondacity.show();
civic.show();
swift.show();
bmw.show();
return 0;
}