拷贝构造函数测试

#include <iostream>
using namespace std;
class Complex
{
private:
	float real;
	float imag;
public:
	//构造函数
	Complex(int x = 0, int y = 0) :	real(x), imag(y){}
	//拷贝构造函数
	Complex(Complex& c) :real(c.real), imag(c.real){}
	//析构函数
	~Complex(){}
	Complex Add(Complex n);   //函数声明
	void show();
	Complex operator=(Complex c)
	{
		real = c.real;
		imag = c.imag+10;
		return *this;
	}
};
void Complex::show()
{
	cout << "my real is" << ' ' << real << endl;
	cout << "my imag is" << ' ' << imag << endl;
}
Complex Complex::Add(Complex n)
{
	Complex z;
	z.real = real + n.real;
	z.imag = imag + n.imag;
	return z;
}
void main()
{
	Complex a(1, 2), b(a), z1;
	cout << "a is :" << endl;
	a.show();
	cout << "b is: " << endl;
	b.show();
	z1 = a.Add(b);
	z1.show();
}

猜你喜欢

转载自blog.csdn.net/shaozheng0503/article/details/130015369