|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<iostream>
- class Complex //complex表示“复数”
- {
- public:
- Complex();
- Complex(double r,double i);
- Complex complex_add(Complex &d);
- void print();
- private:
- double real;
- double imag;
- };
- Complex::Complex()
- {
- real = 0;
- imag = 0;
- }
- Complex::Complex(double r,double i)
- {
- real = r;
- imag = i;
- }
- Complex Complex::complex_add(Complex &d)
- {
- Complex c;
- c.real = real + d.real;
- c.imag = imag + d.imag;
- return c;
- }
- void Complex::print()
- {
- std::cout<<”(”<<real<<”,”<<imag<<”i)\n”;
- }
- int main()
- {
- Complex c1(3,4),c2(5,-10),c3;
- c3 = c1.complex_add(c2);
- std::cout<<”c1=”;
- c1.print();
- std::cout<<”c2=”;
- c1.print();
- std::cout<<”c1+c2=”;
- c3.print();
- return 0;
- }
复制代码
对于这个代码,我的疑问有如下几点
Complex Complex::complex_add(Complex &d)
{
Complex c;
c.real = real + d.real;
c.imag = imag + d.imag;
return c;
}
①在c.real = real +d.real中,加数real是哪个?以main函数中的c3 = c1.complex_add(c2)为例,加数real是哪个
②real是Complex类的私有成员,Complex&d是一个地址,不是Complex类的函数,为什么可以访问私有成员real
①在c.real = real +d.real中,加数real是哪个?以main函数中的c3 = c1.complex_add(c2)为例,加数real是哪个
real是c1的
②real是Complex类的私有成员,Complex&d是一个地址,不是Complex类的函数,为什么可以访问私有成员real
Complex&d 你看清楚它&的位置,它是一个别名而已,传递的是一个 Complex类 的变量
|
|