include
#include
#include
using namespace std;
class Complex
{
public:double real, imaginary;
public:
Complex(double re, double im) :real(re), imaginary(im)
{}
Complex()
{
real = 0; imaginary = 0;
}
Complex operator+(Complex);
Complex operator-(Complex);
Complex operator*(Complex);
Complex operator/(Complex);
double getmode();
bool operator==(Complex &);
bool operator!=(Complex &);
};
Complex Complex::operator+(Complex obj)
{
real += obj.real;
imaginary += obj.imaginary;
return this;
//Complex temp;
//temp.real+=obj.real;
//temp.imaginary+=obj.imaginary;
//return temp;
}
Complex Complex::operator-(Complex obj)
{
real -= obj.real;
imaginary -= obj.imaginary;
return *this;
}
Complex Complex::operator(Complex obj)
{
real *= obj.real;
imaginary *= obj.imaginary;
return *this;
}
Complex Complex::operator/(Complex obj)
{
real /= obj.real;
imaginary /= obj.imaginary;
return *this;
}
double Complex::getmode()
{
double pp;
pp = pow((real*real + imaginary*imaginary), 0.5);
return pp;
}
bool Complex::operator==(Complex &o1)
{
if (this->real>o1.real)
return false;
else if (this->real < o1.real)
return false;
else
return true;
}
bool Complex::operator!=(Complex &o1)
{
if (this->real>o1.real)
return true;
else if (this->real < o1.real)
return true;
else return true;
}
int main(int argc, char *argv[])
{
int flag = 1;
int command = 0;
Complex demo1;
Complex demo2;
cout << "*****************-1-Input complex1**************" << endl;
cout << "*****************-2-Add*************************" << endl;
cout << "*****************-3-Sub*************************" << endl;
cout << "*****************-4-Mutli***********************" << endl;
cout << "*****************-5-Div*************************" << endl;
cout << "*****************-6-Com*************************" << endl;
cout << "*****************-8-exit************************" << endl;
while (flag)
{
cout << "Please select one function:";
cin >> command;
switch (command)
{
case 1:
cout << "please input the complex,first real backspace and img:";
cin >> demo1.real;
cin >> demo1.imaginary;
break;
case 2:
cout << "please input the complex,first real backspace and img:";
cin >> demo2.real;
cin >> demo2.imaginary;
demo1 = demo1 + demo2;
break;
case 3:
cout << "please input the complex,first real backspace and img:";
cin >> demo2.real;
cin >> demo2.imaginary;
demo1 = demo1 - demo2;
break;
case 4:
cout << "please input the complex,first real backspace and img:";
cin >> demo2.real;
cin >> demo2.imaginary;
demo1 = demo1*demo2;
break;
case 5:
cout << "please input the complex,first real backspace and img:";
cin >> demo2.real;
cin >> demo2.imaginary;
demo1 = demo1 / demo2;
break;
case 6:
cout << "please input the complex,first real backspace and img:";
cin >> demo2.real >> demo2.imaginary;
if (demo1 == demo2)
cout << "comeplex1 eq complex2" << endl;
else cout << "complex2 not eq complex2" << endl;
break;
case 8:
flag = 0;
break;
default:break;
}
cout << "the Complex1 is:" << demo1.real << "+" << demo1.imaginary << "i\t" << "mode=" << demo1.getmode() << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
#include
#include
#include
using namespace std;
class Complex
{
public:double real, imaginary;
public:
Complex(double re, double im) :real(re), imaginary(im)
{}
Complex()
{
real = 0; imaginary = 0;
}
Complex operator+(Complex);
Complex operator-(Complex);
Complex operator*(Complex);
Complex operator/(Complex);
double getmode();
bool operator==(Complex &);
bool operator!=(Complex &);
};
Complex Complex::operator+(Complex obj)
{
real += obj.real;
imaginary += obj.imaginary;
return this;
//Complex temp;
//temp.real+=obj.real;
//temp.imaginary+=obj.imaginary;
//return temp;
}
Complex Complex::operator-(Complex obj)
{
real -= obj.real;
imaginary -= obj.imaginary;
return *this;
}
Complex Complex::operator(Complex obj)
{
real *= obj.real;
imaginary *= obj.imaginary;
return *this;
}
Complex Complex::operator/(Complex obj)
{
real /= obj.real;
imaginary /= obj.imaginary;
return *this;
}
double Complex::getmode()
{
double pp;
pp = pow((real*real + imaginary*imaginary), 0.5);
return pp;
}
bool Complex::operator==(Complex &o1)
{
if (this->real>o1.real)
return false;
else if (this->real < o1.real)
return false;
else
return true;
}
bool Complex::operator!=(Complex &o1)
{
if (this->real>o1.real)
return true;
else if (this->real < o1.real)
return true;
else return true;
}
int main(int argc, char *argv[])
{
int flag = 1;
int command = 0;
Complex demo1;
Complex demo2;
cout << "*****************-1-Input complex1**************" << endl;
cout << "*****************-2-Add*************************" << endl;
cout << "*****************-3-Sub*************************" << endl;
cout << "*****************-4-Mutli***********************" << endl;
cout << "*****************-5-Div*************************" << endl;
cout << "*****************-6-Com*************************" << endl;
cout << "*****************-8-exit************************" << endl;
while (flag)
{
cout << "Please select one function:";
cin >> command;
switch (command)
{
case 1:
cout << "please input the complex,first real backspace and img:";
cin >> demo1.real;
cin >> demo1.imaginary;
break;
case 2:
cout << "please input the complex,first real backspace and img:";
cin >> demo2.real;
cin >> demo2.imaginary;
demo1 = demo1 + demo2;
break;
case 3:
cout << "please input the complex,first real backspace and img:";
cin >> demo2.real;
cin >> demo2.imaginary;
demo1 = demo1 - demo2;
break;
case 4:
cout << "please input the complex,first real backspace and img:";
cin >> demo2.real;
cin >> demo2.imaginary;
demo1 = demo1*demo2;
break;
case 5:
cout << "please input the complex,first real backspace and img:";
cin >> demo2.real;
cin >> demo2.imaginary;
demo1 = demo1 / demo2;
break;
case 6:
cout << "please input the complex,first real backspace and img:";
cin >> demo2.real >> demo2.imaginary;
if (demo1 == demo2)
cout << "comeplex1 eq complex2" << endl;
else cout << "complex2 not eq complex2" << endl;
break;
case 8:
flag = 0;
break;
default:break;
}
cout << "the Complex1 is:" << demo1.real << "+" << demo1.imaginary << "i\t" << "mode=" << demo1.getmode() << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
单单从编译通过的角度来说,随便怎么拆都可以,只要拆开来以后编译源代码,include的部分替换为包含文件的内容,使得合成后的代码成立就可以了。
但是,从.h文件存在的价值看(方便调用者通过包含它调用其中定义的类和函数),还是仅仅包含类的定义,而不包含函数实现,并且.h文件应该用
#if def 一个标志
...
的方式避免重复包含。
你可以参照系统类库头文件的做法,这里不详细说了。
把类的定义个函数声明放.h文件(新建文件.h即可)。
楼主还是去看下C++ Premier再来提问吧,问的问题太基础了
额,头文件包含:库,函数库,类的声明,你可以在头文件中声明类,在cpp文件中定义类