马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
关于第二十五课的示例,小甲鱼老师说的看懂了,倒是有一个地方迷糊了。
仿小甲鱼老师的代码:#include <iostream>
#include <string>
class Complex
{
public:
Complex();
Complex(int x,int y);
Complex add_Complex(Complex &c);
void print();
private:
int a;
int b;
};
Complex::Complex()
{
a=0;
b=0;
}
Complex::Complex(int x,int y)
{
a=x;
b=y;
}
Complex Complex::add_Complex(Complex &c)
{
return Complex(a+c.a,b+c.b);
}
void Complex::print()
{
std::cout<<"a:"<<a<<" b:"<<b<<std::endl;
}
int main()
{
Complex c1(3,5);
Complex c2(8,-1);
Complex c3;
c3=c1.add_Complex(c2);
c3.print();
return 0;
}
自己又改了改#include <iostream>
#include <string>
class Complex
{
public:
Complex();
Complex(int x,int y);
Complex add_Complex(Complex c);
void print();
private:
int a;
int b;
};
Complex::Complex()
{
a=0;
b=0;
}
Complex::Complex(int x,int y)
{
a=x;
b=y;
}
Complex Complex::add_Complex(Complex c)
{
return Complex(a+c.a,b+c.b);
}
void Complex::print()
{
std::cout<<"a:"<<a<<" b:"<<b<<std::endl;
}
int main()
{
Complex c1(3,5);
Complex c2(8,-1);
Complex c3;
c3=c1.add_Complex(c2);
c3.print();
return 0;
}
结果是一样的,那么方法里面有没有&到底有什么区别呢? |