This week's tasks are:
Practice 1
Run the following code and find any statements, which may break the class's encapsulation feature. As the default, the member variable "data" is in the private space, so that if objects are not instances of the class "MyClass", they should not be able to directly access it.
#include <iostream>
using namespace std;
class MyClass {
int data; // mydata
public:
MyClass(int n) : data(n) {}
int getData() {
return data;
}
int &getRefData() {
return data;
}
int *getPointerData() {
return &data;
}
int addValue(int n) {
data += n;
return data;
}
int subValue(int n) {
data -= n;
return data;
}
};
int main() {
MyClass myclass(2998);
cout << myclass.getData() << endl;
myclass.getRefData() = 489;
cout << myclass.getData() << endl;
*myclass.getPointerData() = 322;
cout << myclass.getData() << endl;
myclass.addValue(350);
cout << myclass.getData() << endl;
myclass.subValue(450);
cout << myclass.getData() << endl;
return 0;
}
Practice 2
Run the following code and if there is any error, discuss why you've got the error and how to fix it.
"pp1 = new Point;" will create a new instance in the heap memory space and "delete pp1 (delete pp2)" will delete the instance from the heap memory space.
#include <cmath>
using namespace std;
class Point {
int x, y;
public:
Point(int x = 0; int y = 0) : x(x), y(y) {}
Point(Point &p) : x(p.getX()), y(p.getY()) {}
int getX() {return x;}
int getY() {return y;}
void setX(int xval) {x = xval;}
void setY(int yval) {y = yval;}
Point add(Point p) {
Point tmp(x, y);
tmp.x += p.getX();
tmp.y += p.getY();
return tmp;
}
double distance(Point p) {
return fabs(sqrt(pow(abs(x = p.getX()), 2) + pow(abs(y - p.getY()), 2)));
}
};
int main() {
Point *pp1, *pp2;
try {
pp1 = new Point;
if (pp1 == NULL)
throw pp1;
pp2 = new Point;
if (pp2 == NULL)
throw pp2;
} catch (...) {
cerr << "Memory error" << endl;
exit(1);
}
for (int i = 0; i < 10; i++) {
pp1->setX(i), pp1->setY(i);
pp2->setX(i + i * 2), pp2->setY(i + i * 2);
cout << "The distance between ";
cout << "pp1(" << pp1->getX() << ", " << pp1->getY() << ") and ";
cout << "pp2(" << pp2->getX() << ", " << pp2->getY() << ") : ";
cout << pp1->distance(*pp2) << endl;
delete pp1, delete pp2;
}
return 0;
}
Practice 3
Compile and run the following code. If it does not compile or run as you expected, discuss why and how to fix it. You need to consider the relationships between "constructors" and "class inheritance".
#include <iostream>
using namespace std;
struct Object {
int value;
Object(int val = 0) : value(val) {}
int getValue() {return value;}
void setValue(int val) {value = val;)
virtual const char *getName() {return "Object";}
};
class MyObject : Object {
public:
bool operator<(const MyObject &robj) {
return (value < robj.value);
}
bool operator<=(const MyObject &robj) {
return (value <= robj.value);
}
bool operator>(const MyObject &robj) {
return (value > robj.value);
}
bool operator >=(const MyObject &robj) {
return (value >= robj.value);
}
bool operator==(const MyObject &robj) {
return (value == robj.value);
}
const char *getName() {return "MyObject";}
};
int main() {
cout << boolalpha
<< (MyObject(100) < MyObject(200)) << ', '
<< (MyObject(100) <= MyObject(200)) << ', '
<< (MyObject(100) > MyObject(200)) << ', '
<< (MyObject(100) >= MyObject(200)) << ', '
<< (MyObject(100) == MyObject(200)) << endl;
return 0;
}