-
Notifications
You must be signed in to change notification settings - Fork 170
/
plus operator overloading
88 lines (82 loc) · 1.89 KB
/
plus operator overloading
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
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
using namespace std;
class box
{
private:
double length,breadth,height;
public:
double getvolume()
{
return length*breadth*height;
}
void setlength(double l)
{
length=l;
}
void setbreadth(double b)
{
breadth=b;
}
void setheight(double h)
{
height=h;
}
box operator+(const box&b)
{
box box;
box.length=this->length+b.length;
box.breadth=this->breadth+b.breadth;
box.height=this->height+b.height;
return box;
}
box operator-(const box&m)
{
box box;
box.length=this->length-m.length;
box.breadth=this->breadth-m.breadth;
box.height=this->height-m.height;
return box;
}
};
int main()
{
double vol1,vol2,vol3,vol4,vol5;
box box1;
box box2;
box box3;
box box4;
box box5;
box1.setlength(2.5);
box1.setbreadth(5.5);
box1.setheight(7.5);
box2.setlength(12.5);
box2.setbreadth(15.5);
box2.setheight(17.5);
box4.setlength(2.5);
box4.setbreadth(1.5);
box4.setheight(1.5);
vol1=box1.getvolume();
vol2=box2.getvolume();
vol4=box4.getvolume();
cout<<endl<<"box1 volume is "<<vol1<<endl;
cout<<"box2 volume is "<<vol2<<endl;
cout<<"box4 volume is "<<vol4<<endl;
box3=box1+box2+box4;
vol3=box3.getvolume();
box5=box2-box1;
vol5=box5.getvolume();
cout<<endl<<"overloads unuary + operator:"<<endl;
cout<<"box3 volume is "<<vol3<<endl; //box3=box1+box2+box4
cout<<endl<<"overloads unuary - operator:"<<endl;
cout<<"box5 volume is "<<vol5<<endl; //box5=box2-box1
cout<<endl<<"overloads [] operator:"<<endl;
int intArray[1024];
for (int i = 0, j = 0; i < 1024; i++)
{
intArray[i] = j++;
}
// 512
cout << intArray[512] << endl;
// 257
cout << 257 [intArray] << endl;
}