Lab 06
Lab 06
class Point2D
{
private:
int x;
int y;
static const double limit = 10.0;
static double value; // indicates that all object’s value are the same
public:
Point2D();
void assignPoint2D(int x, int y);
void displayPoint2D();
static void setValue(double v);
// only static member function can access static member
};
Point2D::Point2D()
{
x = 0;
y = 0;
}
void Point2D::displayPoint2D()
{
std::cout << "(" << x << "," << y << ") = ";
std::cout << value << std::endl;
}
1/7
National Chiao Tung University Laboratory Manual 06
Department of Electrical and Computer Engineering April 9, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
void Point2D::setValue(double v)
{
if (v < limit)
value = v;
else
value = limit;
}
int main()
{
Point2D ptArray[10];
ptArray[0].setValue(1.1);
// modify the static member by static member fuction
return 0;
}
Remark the line double Point2D::value = 0.0; and compiler the program again. Try to
explain the error message.
Remove static in static const double limit = 10.0; and compiler the program again.
Remove const in static const double limit = 10.0; and compiler the program again.
Try to modify ptArray[0].setValue(1.1); as ptArray[0].setValue(30.1); and
execute the program again.
int main()
{
const Point2D pt1;
Point2D pt2;
2/7
National Chiao Tung University Laboratory Manual 06
Department of Electrical and Computer Engineering April 9, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
pt1.displayPoint2D();
pt2.displayPoint2D();
return 0;
}
// lab6-2-2.cpp
int main()
{
const Point2D pt1;
Point2D pt2;
pt1.displayPoint2D();
pt2.displayPoint2D();
return 0;
}
Please identify the difference between mutable data member and non-mutable data
member.
3/7
National Chiao Tung University Laboratory Manual 06
Department of Electrical and Computer Engineering April 9, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
this->value = 0.0;
this->coord = new int [num];
for (int i=0;i<num;i++) this->coord[i] = 0;
}
this pointer includes the address of the object, so it can be used to compare the addresses
between different objects.
// lab6-3-2.cpp
#include <iostream>
/* class PointND declares and defines in lab 5-2 with copy constructor*/
/* add declaration of member function: copyPoint2D() to class PointND */
int main()
{
int *vec = new int [num];
for (int i=0;i<num;i++) vec[i] = i;
PointND pt1;
pt1.assignValue(4.3);
pt1.assignCoord(vec,num);
pt1.displayPointND();
PointND pt2;
pt2.copyPointND(pt1);
pt2.displayPointND();
PointND pt3;
pt3.copyPointND(pt3);
pt3.displayPointND();
delete []vec;
return 0;
}
4/7
National Chiao Tung University Laboratory Manual 06
Department of Electrical and Computer Engineering April 9, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
class Vec
{
public:
Vec(){len =0;}
Vec(int n);
~Vec();
private:
class Items // nested class Items for Vec
{ // all members in Items are private
friend class Vec; // make Vec can access member in Items
Items(){value = 0;}
Items(int v){value = v;}
int value;
};
int len;
Items *vec;
};
Vec::Vec(int n)
{
len = n;
vec = new Items [len];
}
Vec::~Vec()
{
if (len > 0)
delete []vec;
}
5/7
National Chiao Tung University Laboratory Manual 06
Department of Electrical and Computer Engineering April 9, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
int main()
{
Vec vector(5);
vector.printVec();
Items n;
return 0;
}
There is a compiler error in this example because the nested class cannot be used in global
scope. Try to modify the program and make Items be accessed in global scope.
6/7
National Chiao Tung University Laboratory Manual 06
Department of Electrical and Computer Engineering April 9, 2012
Computer Intelligence on Automation(C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
quadrangle q1(quadrangle::vertex(0,0),
quadrangle::vertex(3,2),
quadrangle::vertex(10,7),
quadrangle::vertex(8,10));
quadrangle q2(quadrangle::vertex(3,6), quadrangle::vertex(10,9));
cout << "q1 information" << endl;
q1.draw();
cout << endl;
cout << "q2 information" << endl;
q2.draw();
cout << endl;
cout << "q1 move to (5,5) " << endl;
q1.move(quadrangle::vertex(5,5));
cout << "q1 information" << endl;
q1.draw();
cout << endl;
quadrangle::origin = quadrangle::vertex(-5,3);
cout << "q2 move to (-1,2) " << endl;
q2.move(quadrangle::vertex(-1,2));
cout << "q2 information" << endl;
q2.draw();
cout << endl;
return 0;
} // end main
q1 information
v1: (0,0) v2: (3,2) v3: (10,7) v4: (8,10)
q2 information
v1: (3,6) v2: (10,6) v3: (10,9) v4: (3,9) area: 21
q1 move to (5,5)
Distance: 7.07107
q1 information
v1: (5,5) v2: (8,7) v3: (15,12) v4: (13,15)
q2 move to (-1,2)
Distance: 4.12311
q2 information
v1: (-1,2) v2: (6,2) v3: (6,5) v4: (-1,5) area: 21
7/7