Create a class called Complex for performing arithmetic with complex numbers.
Complex numbers have the form
realPart + imaginaryPart * i
where i is √-1
Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided.
Provide public member functions that perform the following tasks:
a) Adding two Complex numbers: The real parts are added together and the imaginary parts are added together.
b) Subtracting two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.
c) Printing Complex numbers in the form (a, b) where a is the real part and b is the imaginary part.
Complex::Complex( double real, double imaginary ) {
setComplexNumber( real, imaginary );
} // end Complex constructor
Complex Complex::add( const Complex &right ) {
/* Write a statement to return a Complex object. Add
the realPart of right to the realPart of this Complex
object and add the imaginaryPart of right to the
imaginaryPart of this Complex object */
} // end function add
Complex Complex::subtract( const Complex &right ) {
/* Write a statement to return a Complex object. Subtract
the realPart of right from the realPart of this Complex
object and subtract the imaginaryPart of right from
the imaginaryPart of this Complex object */
} // end function subtract
void Complex::printComplex() {
cout << '(' << realPart << ", " << imaginaryPart << ')';
} // end function printComplex
void Complex::setComplexNumber( double rp, double ip ) {
realPart = rp;
imaginaryPart = ip;
} // end function setComplexNumber
1 2 3 4 5 6 7 8
(1, 2) + (3, 4) = (4, 6)
(5, 6) - (7, 8) = (-2, -2)
main.cpp
#include
using std::cin;
using std::cout;
using std::endl;
#include "Complex.hpp"
int main() {
int n[8];
cin >> n[0] >> n[1] >> n[2] >> n[3];
cin >> n[4] >> n[5] >> n[6] >> n[7];
Complex a(n[0], n[1]), b(n[2], n[3]), c;
// create three Complex objects
a.printComplex(); // output object a
cout << " + ";
b.printComplex(); // output object b
cout << " = ";
c = a.add(b);
// invoke add function and assign to object c
c.printComplex(); // output object c
cout << '\n';
a.setComplexNumber(n[4], n[5]); // reset realPart and
b.setComplexNumber(n[6], n[7]); // and imaginaryPart
a.printComplex(); // output object a
cout << " - ";
b.printComplex(); // output object b
cout << " = ";
c = a.subtract(b); // invoke add function and assign to object c
c.printComplex(); // output object c
cout << endl;
} // end main
Complex.hpp*
using namespace std;
class Complex {
public :
Complex(double ,double); //创建带参数的构造函数
/*创建类构造函数,如无创建,则系统默认生成无参数构造函数*/
Complex() //创建不带参数的函数
{
realPart = 0.0;
imaginaryPart = 0.0;
};
void printComplex();
void setComplexNumber( double, double );
Complex add(const Complex &);
Complex subtract(const Complex &);
private :
double realPart;
double imaginaryPart;
};
Complex.cpp*
#include "Complex.hpp"
#include
using namespace std;
using std::cout;
using std::endl;
Complex::Complex( double real, double imaginary ) {
setComplexNumber( real, imaginary );
}
Complex Complex::add( const Complex &right ) {
realPart += right.realPart;
imaginaryPart += right.imaginaryPart;
return *this;
}
Complex Complex::subtract( const Complex &right ) {
realPart -= right.realPart;
imaginaryPart -= right.imaginaryPart;
return *this;
}
void Complex::printComplex() {
cout << '(' << realPart << ", " << imaginaryPart << ')';
}
void Complex::setComplexNumber( double rp, double ip ) {
realPart = rp;
imaginaryPart = ip;
}
http://blog.csdn.net/tiantang46800/article/details/6938762
// 无参数构造函数
// 如果创建一个类你没有写任何构造函数,则系统会自动生成默认的无参构造函数,函数为空,什么都不做
// 只要你写了一个下面的某一种构造函数,系统就不会再自动生成这样一个默认的构造函数,如果希望有一个这样的无参构造函数,则需要自己显示地写出来。
// 一般构造函数(也称重载构造函数)
// 一般构造函数可以有各种参数形式,一个类可以有多个一般构造函数,前提是参数的个数或者类型不同(基于c++的重载函数原理)
// 例如:你还可以写一个 Complex( int num)的构造函数出来
// 创建对象时根据传入的参数不同调用不同的构造函数