复数加法和使用复制构造函数
complex number addition and using copy constructor
#include<iostream>
using namespace std;
class complex
{
float real,imag;
public:
complex(complex &c)
{
cout<<"copy constructor"<<endl;
real=c.real;
imag=c.imag;
}
void showData()
{
count<<"the sum is"<<endl;
cout<<real<<"+i"<<imag<<endl;
}
complex addition(complex x,complex y)
{
complex temp;
temp.real=x.real+y.real;
temp.imag=x.imag+x.imag;
return temp;
}
};
int main()
{
complex c2(2,3),c3(c2),c1;
c2.showData();
c3.showData();
c1=c1.addition(c2,c3);
c1.showData();
return 0;
}
我想通过从对象传递值并使用复数复制相同的值来添加复数。这是我得到的错误:
Error:C:\Users\Santosh\Documents\cplusplus\complex_addition.cpp|48|error: no matching function for call to 'complex::complex()'|
编译器认为 complex c2(2,3)
正在尝试调用“()”运算符。您尚未定义采用两个整数的构造函数。
complex
class需要定义两个构造函数来解决:
complex() {}
complex(float rl, float im) : real(rl), imag(im) {}
该值从未被初始化,因为没有构造函数将 2 和 3 分别放入 real
和 imag
。 class 对象 c1
将需要 complex() {}
构造函数。
这对我有用。
#include<iostream>
using namespace std;
class complex
{
float real,imag;
public:
complex()
{
cout<<"default constructor"<<endl;
real=0;
imag=0;
}
complex(float r, float i)
{
cout<<"parameterized construction";
real=r;
imag=i;
}
complex(complex &c)
{
cout<<"copy constructor"<<endl;
real=c.real;
imag=c.imag;
}
void getData()
{
cout<<"the complex number are: "<<real<<"+"<<imag<<"i"<<endl;
}
void showData()
{
cout<<"the sum is: ";
cout<<real<<"+"<<imag<<"i"<<endl;
}
complex addition(complex x,complex y)
{
complex temp;
temp.real=x.real+y.real;
temp.imag=x.imag+x.imag;
return temp;
}
};
int main()
{
complex c2(2,3),c3(c2),c1;
c2.getData();
c3.getData();
c1=c1.addition(c2,c3);
c1.showData();
return 0;
}
#include<iostream>
using namespace std;
class complex
{
float real,imag;
public:
complex(complex &c)
{
cout<<"copy constructor"<<endl;
real=c.real;
imag=c.imag;
}
void showData()
{
count<<"the sum is"<<endl;
cout<<real<<"+i"<<imag<<endl;
}
complex addition(complex x,complex y)
{
complex temp;
temp.real=x.real+y.real;
temp.imag=x.imag+x.imag;
return temp;
}
};
int main()
{
complex c2(2,3),c3(c2),c1;
c2.showData();
c3.showData();
c1=c1.addition(c2,c3);
c1.showData();
return 0;
}
我想通过从对象传递值并使用复数复制相同的值来添加复数。这是我得到的错误:
Error:C:\Users\Santosh\Documents\cplusplus\complex_addition.cpp|48|error: no matching function for call to 'complex::complex()'|
编译器认为 complex c2(2,3)
正在尝试调用“()”运算符。您尚未定义采用两个整数的构造函数。
complex
class需要定义两个构造函数来解决:
complex() {}
complex(float rl, float im) : real(rl), imag(im) {}
该值从未被初始化,因为没有构造函数将 2 和 3 分别放入 real
和 imag
。 class 对象 c1
将需要 complex() {}
构造函数。
这对我有用。
#include<iostream>
using namespace std;
class complex
{
float real,imag;
public:
complex()
{
cout<<"default constructor"<<endl;
real=0;
imag=0;
}
complex(float r, float i)
{
cout<<"parameterized construction";
real=r;
imag=i;
}
complex(complex &c)
{
cout<<"copy constructor"<<endl;
real=c.real;
imag=c.imag;
}
void getData()
{
cout<<"the complex number are: "<<real<<"+"<<imag<<"i"<<endl;
}
void showData()
{
cout<<"the sum is: ";
cout<<real<<"+"<<imag<<"i"<<endl;
}
complex addition(complex x,complex y)
{
complex temp;
temp.real=x.real+y.real;
temp.imag=x.imag+x.imag;
return temp;
}
};
int main()
{
complex c2(2,3),c3(c2),c1;
c2.getData();
c3.getData();
c1=c1.addition(c2,c3);
c1.showData();
return 0;
}