Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2370 views
1
//An example class with overloaded methods
2
#include <iostream>
3
4
using namespace std;
5
6
class Point {
7
public:
8
//overloaded constructors!
9
Point()
10
{
11
_x = 0;
12
_y = 0;
13
}
14
15
Point(double _x, double _y)
16
{
17
this->_x = _x;
18
this->_y = _y;
19
}
20
21
//overloaded methods for accessing x and y
22
double x()
23
{
24
return _x;
25
}
26
27
void x(double val)
28
{
29
this->_x = val;
30
}
31
32
double y()
33
{
34
return _y;
35
}
36
37
void y(double val)
38
{
39
this->_y = val;
40
}
41
private:
42
//private attributes of the class
43
double _x;
44
double _y;
45
};
46
47
48
int main()
49
{
50
Point *p1 = new Point;
51
Point *p2 = new Point(2.0, 3.0);
52
53
cout << "(" << p2->x() << ", " << p2->y() << ")" << endl;
54
p1->x(4);
55
cout << "(" << p1->x() << ", " << p1->y() << ")" << endl;
56
}
57
58