-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path62_this_pointer.cpp
More file actions
59 lines (49 loc) · 1.08 KB
/
62_this_pointer.cpp
File metadata and controls
59 lines (49 loc) · 1.08 KB
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
#include <iostream>
using namespace std;
class Shape {
public:
// Constructor definition
Shape(int l = 2, int w = 2)
{
length = l;
width = w;
}
double Area()
{
return length * width;
}
//Use 'this' to compare areas
int compareWithThis(Shape shape)
{
return this->Area() > shape.Area();
}
//'this' is not necessary to compare shapes
int compare(Shape shapeIn)
{
return Area() > shapeIn.Area();
}
private:
int length; // Length of a box
int width;
};
int main(void)
{
Shape sh1(4, 4); // Declare shape1
Shape sh2(2, 6); // Declare shape2
if(sh1.compare(sh2))
{
cout << "\nShape2 is smaller than Shape1" <<endl;
}
else
{
cout << "\nShape2 is equal to or larger than Shape1" <<endl;
}
if(sh1.compareWithThis(sh2)) {
cout << "\nShape2 is smaller than Shape1" <<endl;
}
else
{
cout << "Shape2 is equal to or larger than Shape1" <<endl;
}
return 0;
}