Untitled Document

三、编程实现
1. 参考程序如下：
#include <iostream.h>

class C
{
private:
int private_data;
void private_method()
{
cout<<"I am private method"<<endl;
}
protected:
int protected_data;
void protected_method()
{
cout<<"I am protected method"<<endl;
}
public:
int public_data;
void public_method()
{
cout<<"I am public method"<<endl;
}
C()
{
private_data = 0;
protected_data = 1;
public_data = 2;
}

friend void friendFunction(C c);
};
void friendFunction(C c)
{
c.private_method();
c.protected_method();
cout<<"c.private_data = "<<c.private_data<<endl;
cout<<"c.protected_data = "<<c.protected_data<<endl;
}
void main()
{
C c;
// Error: c.private_method();
// Error: c.protected_method;
// Error: cout<<c.private_data<<endl;
// Error: cout<<c.protected_data<<endl;
friendFunction(c);
}

说明：上述main（）函数中注释起来的语句是普通函数访问对象c的private成员和proctected成员，它们，不能通过编译器。例如，Visual C＋＋6.0 编译器将产生这样的出错信息：
cannot access private member declared in class 'C'
或者
cannot access protected member declared in class 'C'
然而， 利用声明为类C的友元的friendFunction()函数，就可以实现对于私有成员的访问。
程序输出结果如下：
I am private method
I am protected method
c.private_data = 0
c.protected_data = 1

2．参考程序如下：
#include <iostream.h>
class C
{
public:
//C(char a){}
void Display()
{
cout<<"hello world!"<<endl;
}
};

void main()
{
C c;
c.Display();
}
说明：上述程序可以编译通过，程序输出结果如下：
hello world!
说明main（）函数中C c;代码行调用了类C的缺省调用函数。
但是，如果我们将注释行的注释去掉，实际上就是给类C加入一个带有一个参数的构造函数，则编译不能通过，Visual C＋＋ 6.0编译器显示下列出错信息：
no appropriate default constructor available
因为我们已经建立了一个构造函数，所以编译器不再建立缺省的构造函数，C c;代码行也就不能再通过编译了。
3．参考程序如下：
#include <iostream.h>

class C
{
private:
int i;
public:

C():i(0){}
~C(){}

int getI()const
{
return this->i;
}

void setI(int i)
{
this->i = i;
}
};

void main()
{
C noConstC;
const C constC;

cout<<noConstC.getI()<<endl;
noConstC.setI(1);
cout<<noConstC.getI()<<endl;

cout<<constC.getI()<<endl;
// Error: constC.setI(1);
cout<<constC.getI()<<endl;
}
说明：上述代码编译可以通过，如果加入注释行，则程序不能编译通过， Visual C＋＋ 6.0编译器显示下面的信息：
cannot convert 'this' pointer from 'const class C' to 'class C &'
这是由于新加入的代码涉及到常量对象调用非cosnt成员函数引起的。
下表显示了变量和函数的调用关系：

| const成员函数 | 非const成员函数
const对象 | 可以调用 | 不可以调用
非const对象 | 可以调用 | 可以调用

4．参考程序如下：
#include <iostream.h>

class ObjectCounter
{
public:
ObjectCounter(char ch)
{
counter++;
name = ch;
cout<<"Object "<<name<<" comes into being"<<endl;
}
~ObjectCounter()
{
counter--;
cout<<"Object "<<name<<" dies out"<<endl;
}
static int counter;
void printCounter()
{
cout<<name<<" say: Now CLASS ObjectCounter has "<<counter<<" objects altogether!"<<endl;
}
private:
char name;
};
int ObjectCounter::counter = 0;
void main()
{
ObjectCounter* oc1;
ObjectCounter* oc2;
ObjectCounter* oc3;

oc1 = new ObjectCounter('1');
oc1->printCounter();
oc2 = new ObjectCounter('2');
oc1->printCounter();
oc2->printCounter();
oc3 = new ObjectCounter('3');
oc1->printCounter();
oc2->printCounter();
oc3->printCounter();

delete oc3;
oc1->printCounter();
oc2->printCounter();
delete oc2;
oc1->printCounter();
delete oc1;
}
说明：程序输出结果如下：
Object 1 comes into being
1 say: Now CLASS ObjectCounter has 1 objects altogether!
Object 2 comes into being
1 say: Now CLASS ObjectCounter has 2 objects altogether!
2 say: Now CLASS ObjectCounter has 2 objects altogether!
Object 3 comes into being
1 say: Now CLASS ObjectCounter has 3 objects altogether!
2 say: Now CLASS ObjectCounter has 3 objects altogether!
3 say: Now CLASS ObjectCounter has 3 objects altogether!
Object 3 dies out
1 say: Now CLASS ObjectCounter has 2 objects altogether!
2 say: Now CLASS ObjectCounter has 2 objects altogether!
Object 2 dies out
1 say: Now CLASS ObjectCounter has 1 objects altogether!
Object 1 dies out
5．解答：
构造函数被调用通常发生在以下三种情况，第一种情况就是用一个对象初始化另一个对象时，比如c2 = c1；第二种情况是当对象作函数参数，实参传给形参时；第三种情况是程序运行过程中创建其它临时对象时。
6．参考程序如下：
#include <iostream.h>

class Point
{
private:
int X;
int Y;
public:
Point(int X, int Y)
{
this->X = X;
this->Y = Y;
}
Point()
{
this->X = 0;
this->Y = 0;
}
~Point(){}
int GetX()const
{
return this->X;
}
int GetY()const
{
return this->Y;
}
void SetX(int X)
{
this->X = X;
}
void SetY(int Y)
{
this->Y = Y;
}
void MoveTo(int X, int Y)
{
SetX(X);
SetY(Y);
}
void Display()const
{
cout<<"X: "<<GetX()<<endl;
cout<<"Y: "<<GetY()<<endl;
}
};

void main()
{
Point p(100,100);
p.Display();
p.MoveTo(200,200);

cout<<"after moving"<<endl;
p.Display();
}

程序输出结果如下：
X: 100
Y: 100
after moving
X: 200
Y: 200
7．参考程序如下：
#include <iostream.h>

class Point
{
private:
int X;
int Y;
public:
Point(int X, int Y)
{
this->X = X;
this->Y = Y;
}
Point()
{
this->X = 0;
this->Y = 0;
}
~Point(){}
int GetX()const
{
return this->X;
}
int GetY()const
{
return this->Y;
}
void SetX(int X)
{
this->X = X;
}
void SetY(int Y)
{
this->Y = Y;
}
void MoveTo(int X, int Y)
{
SetX(X);
SetY(Y);
}
void Display()const
{
cout<<"X: "<<GetX()<<endl;
cout<<"Y: "<<GetY()<<endl;
}
};

class Circle
{
private:
Point O;
int R;
public:
Circle(Point& p, int R)
{
O.MoveTo(p.GetX(),p.GetY());

this->R = R;
}
~Circle(){}

Point GetO()const
{
return this->O;
}
int GetR()const
{
return this->R;
}

void MoveTo(Point& p)
{
O.MoveTo(p.GetX(), p.GetY());
}
void SetR(int R)
{
this->R = R;
}

void Display()
{
cout<<"Circle: ("<<O.GetX()<<","<<O.GetY()<<"),"<<R<<endl;
}
};

void main()
{
Point p(100,100);
Point p2(200,200);
Circle c(p, 100);
c.Display();

c.MoveTo(p2);
cout<<"after moving"<<endl;
c.Display();

c.SetR(200);
cout<<"after altering r"<<endl;
c.Display();
}
程序输出结果如下：
Circle: (100,100),100
after moving
Circle: (200,200),100
after altering r
Circle: (200,200),200
8．参考程序如下：
#include <iostream.h>

class Complex
{
double R;
double I;
public:
Complex(double R = 0.0, double I = 0.0)
{
this->R = R;
this->I = I;
}
~Complex(){}

/* copy constructor function */
Complex(const Complex& c)
{
this->R = c.R;
this->I = c.I;
}

/* (+ - * /) */
friend const Complex operator+(const Complex& a, const Complex& b);
friend const Complex operator-(const Complex& a, const Complex& b);
friend const Complex operator*(const Complex& a, const Complex& b);
friend const Complex operator/(const Complex& a, const Complex& b);

/* - */
friend const Complex operator-(const Complex& a);

/* == != */
friend bool operator==(const Complex& a, const Complex& b);
friend bool operator!=(const Complex& a, const Complex& b);

friend ostream& operator<<(ostream& out, const Complex& c);
};

/* + overloading */
const Complex operator +(const Complex& a, const Complex& b)
{
Complex c;

c.R = a.R + b.R;
c.I = a.I + b.I;

return c;
}

/* - overloading */
const Complex operator -(const Complex& a, const Complex& b)
{
Complex c;

c.R = a.R - b.R;
c.I = a.I - b.I;

return c;
}
/* * overloading */
const Complex operator *(const Complex& a, const Complex& b)
{
Complex c;

c.R = (a.R * b.R) - (a.I * b.I);
c.I = (a.R * b.I) + (a.I * b.R);

return c;
}
/* / overloading */
const Complex operator /(const Complex& a, const Complex& b)
{
Complex c;
double temp;

temp = b.R*b.R + b.I*b.I;

if(temp == 0)
{
c.R = 0; c.I = 0;
return c;
}

c.R = (a.R*b.R + a.I*b.I) / temp;
c.I = (a.I*b.R - a.R*b.I) / temp;

return c;
}

/* - overloading */
const Complex operator -(const Complex& a)
{
Complex c;

c.R = - a.R;
c.I = - a.I;

return c;
}

/* == overloading */
bool operator==(const Complex& a, const Complex& b)
{
if((a.I == b.I) && (a.R == b.R))
{
return true;
}else
{
return false;
}
}

/* != overloading */
bool operator!=(const Complex& a, const Complex& b)
{
if((a.I == b.I) && (a.R == b.R))
{
return false;
}else
{
return true;
}
}

/* ostream overloading */
ostream& operator<<(ostream& out, const Complex& c)
{
out<<"("<<c.R<<")"<<"+"<<"("<<c.I<<")"<<"i";
return out;
}

void main()
{
Complex a(1);
Complex b(1, 2);
Complex c;
double da = 100.0;
int ia = 99;

cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"da = "<<da<<endl;
cout<<"ia = "<<ia<<endl;

c = a + b;
cout<<"c = a + b = "<<c<<endl;

c = a - b;
cout<<"c = a - b = "<<c<<endl;

c = a * b;
cout<<"c = a * b = "<<c<<endl;

c = a / b;
cout<<"c = a / b = "<<c<<endl;

c = -a;
cout<<"c = -a = "<<c<<endl;

c = da;
cout<<"c = da = "<<c<<endl;

c = ia;
cout<<"c = ia = "<<c<<endl;

c = a * da;
cout<<"c = a * da = "<<c<<endl;

c = a * ia;
cout<<"c = a * ia = "<<c<<endl;

c = a / da;
cout<<"c = a / da = "<<c<<endl;

c = a / ia;
cout<<"c = a / ia = "<<c<<endl;

c = a + da;
cout<<"c = a + da = "<<c<<endl;

c = a + ia;
cout<<"c = a + ia = "<<c<<endl;

c = a - da;
cout<<"c = a - da = "<<c<<endl;

c = a - ia;
cout<<"c = a - ia = "<<c<<endl;

if(c == a)
{
cout<<"c == a"<<endl;
}
if(c != a)
{
cout<<"c != a"<<endl;
}

if(c == da)
{
cout<<"c == da"<<endl;
}
if(c != da)
{
cout<<"c != da"<<endl;
}

c = ia;
cout<<"c = ia = "<<c<<endl;
if(c == ia)
{
cout<<"c == ia"<<endl;
}
if(c != ia)
{
cout<<"c != ia"<<endl;
}
}