我开始用C ++学习OOP编程。 我写了一个代码,显示读取的复数和数字的总和。
#include "stdafx.h"
#include<iostream>
using namespace std;
class complex
{
double real;
double imag;
public:
complex(double x = 0, double y = 0);
complex(const complex&);
double modul();
void display();
};
complex::complex(double x, double y)
{
real = x;
imag = y;
}
complex::complex(const complex& z)
{
real = z.real;
imag = z.imag;
}
double complex::modul()
{
return(sqrt(real*real + imag*imag));
}
void complex::display()
{
cout << real << " + i" << imag << endl;
}
int main()
{
double x, y, sumax = 0, sumay = 0, i = 0, n;
cout << "n = "; cin >> n;
while (i < n)
{
cout << "Real = "; cin >> x;
cout << "Imaginary = "; cin >> y;
sumax += x;
sumay += y;
complex z(x, y);
cout << endl;
z.display();
i++;
}
complex sz = complex(sumax, sumay);
cout << "Sum = ";
sz.display();
}
complex sz = complex(sumax, sumay);
和complex sz(sumax,sumay);
之间有什么区别吗?
如果是我应该使用哪一个?
该计划适用于两者。