|
#include <cstdlib>
#include <iostream>
using namespace std;
class Complex{
public:
Complex(double,double);
Complex(const Complex &c);
void print(int i);
private:
double real,imag;
};
Complex::Complex(double r,double i)
{
real=r;
imag=i;
cout<<"constructor(1) called:real="<<real<<",imag="<<imag<<endl;
}
Complex::Complex(const Complex &c)
{
real=c.real; //这里real 和 imag 是私有数据成员 对象怎么可以直接访问呢?
imag=c.imag;
cout<<"constructor(2) called:real="<<real<<",imag="<<imag<<endl;
}
void Complex::print(int i)
{
if( imag<0 )
cout<<"c"<<i<<"="<<real<<imag<<"i"<<endl;
else
cout<<"c"<<i<<"="<<real<<"+"<<imag<<"i"<<endl;
}
int main(int argc, char *argv[])
{
Complex c1(6,-8),c2=c1;
c1.print(1);
c2.print(2);
system("PAUSE");
return EXIT_SUCCESS;
}
程序中注释的问题到底是怎么回事呢? |
|