forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path85-this.cpp
63 lines (53 loc) · 1.37 KB
/
85-this.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
#include <iostream>
#include <stdexcept>
#include <set>
class Point; //Class declaration
void print_point(Point const &pt);
class Point
{
public:
Point(double x, double y)
// : x{x}, y{y}
{
//Task: Below this line, initialize the two members of this object
// to be the provided values of x and y. You aren't allowed to
// change the names of the parameters in the signature on line 17,
// and don't try any funny business either (just write some assignment statements).
this->x = x;
this->y = y;
}
Point() : Point{0, 0}
{ //The default constructor defers to the two-argument constructor.
}
double get_x() const
{
return x;
}
double get_y() const
{
return y;
}
void print() const
{
//Task: Instead of printing anything here, call the print_point function
// with the current Point instance to print it out.
print_point(*this);
}
private:
double x{};
double y{};
};
void print_point(Point const &pt)
{
std::cout << "print_point: (" << pt.get_x() << ", " << pt.get_y() << ")" << std::endl;
}
int main()
{
Point P{6, 17};
Point Q{10, 187};
std::cout << "Calling print_point on P" << std::endl;
print_point(P);
std::cout << "Calling Q.print()" << std::endl;
Q.print();
return 0;
}