-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructures.cpp
More file actions
64 lines (47 loc) · 1.63 KB
/
structures.cpp
File metadata and controls
64 lines (47 loc) · 1.63 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
60
61
62
63
64
/*
C++ structs, like classes, may contain access specifiers (public, private, protected), member functions, constructor,
destructor and support inheritance. The only difference for C++'s structs and classes is the struct's members default
to public access while class members default to private access, if no access specifier is used. Also, C++ structs default
to public-inheritance whereas classes default to private-inheritance.
*/
#include <iostream>
using namespace std;
struct Point {
int x, y;
};
struct Rectangle {
Point topLeft;
Point bottomRight;
Rectangle() {
//Calling constructor to initialize the elements to default values
topLeft.x = 0;
topLeft.y = 0;
bottomRight.x = 10;
bottomRight.y = 10;
}
int rectArea() {
return abs( (bottomRight.x - topLeft.x) * (bottomRight.y - topLeft.y) );
}
};
int main() {
Point p1, p2;
p1.x = 0; // p1 at (0, 3)
p1.y = 3;
p2.x = 4; // p2 at (4, 0)
p2.y = 0;
cout << "p1 at (" << p1.x << "," << p1.y << ")" << endl;
cout << "p2 at (" << p2.x << "," << p2.y << ")" << endl;
Rectangle rect;
cout << "Default rect at topLeft (" << rect.topLeft.x << "," << rect.topLeft.y << ")" << endl;
cout << "Default rect at bottomRight (" << rect.bottomRight.x << "," << rect.bottomRight.y << ")" << endl;
cout << "Default area = " << rect.rectArea()<<endl;
rect.topLeft = p1;
rect.bottomRight = p2;
cout << "Rectangle top-left at (" << rect.topLeft.x
<< "," << rect.topLeft.y << ")" << endl;
cout << "Rectangle bottom-right at (" << rect.bottomRight.x
<< "," << rect.bottomRight.y << ")" << endl;
cout << "New area = " << rect.rectArea() << endl;
system("pause");
return 0;
}