-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoop2.cpp
92 lines (66 loc) · 1.97 KB
/
oop2.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
using namespace std;
int myPtrFunction(int i){
return i+61;
}
class Coordinates{
public:
int x,y;
void calculateCoordinate(int _x,int _y){
x = _x;
y = _y;
}
void output(){
cout<<"x = "<<x <<" y = "<< y << endl;
}
bool isOrigin(){
/*
if (x == 0 && y == 0)
return true;
else
return false; */
return x == 0 && y == 0; // :)
}
};
int main() {
Coordinates n;
n.x = 0;
n.y = 5;
n.calculateCoordinate(2,7);
n.output();
Coordinates m;
m.calculateCoordinate(10,20);
m.output();
if (n.isOrigin())
cout<<"n is the origin"<< endl;
else
cout<<"n is not the origin"<< endl;
Coordinates* ptr = &m;
cout<<"Class' output method's outputs with pointer"<<endl;
ptr-> output();
cout<<"Class' isOrigin method's outputs with pointer"<<endl<< ptr->isOrigin()<<endl;
cout<<"class' variables"<<endl<<ptr->x<<" and "<<ptr->y <<endl;
Coordinates* ptr2;
//dynamic memory management
ptr2 = new Coordinates();
cout<<"ptr2's outputs"<<endl;
ptr2->calculateCoordinate(61,61);
ptr2->output();
cout<<"ptr2's isOrigin method's output"<<endl<<ptr2->isOrigin()<<endl;
cout<<"--------------------------"<<endl;
Coordinates coordinateArray[10];
Coordinates* ptrArray;
ptrArray = new Coordinates[10];
ptrArray = coordinateArray;
for(int i = 0;i<10;i++)
{
coordinateArray[i].calculateCoordinate(i,i*10);
cout<<"Index: "<<i<<endl<<"Address: "<<(ptrArray+i)<<endl<<"Values x and y: "<<(ptrArray+i)->x<<" and "<<(ptrArray+i)->y
<<endl<<"-----------------"<<endl;
}
cout<<"Function's address: "<<myPtrFunction<<endl;
Coordinates* coorPtr = new Coordinates();
cout<<endl<<coorPtr<<endl;
delete coorPtr;
return 0;
}