2个回答
展开全部
//Complex.hpp
#ifndef __COMPLX__
#define __COMPLX__
class Complex
{
public:
Complex();
Complex(double, double);
Complex(const Complex&);
~Complex();
Complex& operator = (const Complex&);
Complex& operator = (double);
void add(double);
Complex add(const Complex&) const;
friend Complex add(const Complex&, const Complex&);
void sub(double);
Complex sub(const Complex&) const;
void display() const;
private:
double real;
double image;
};
//Complex.cpp
#include "Complex.hpp"
#include <cstdio>
Complex::Complex()
{
real = 0.;
image = 0.;
}
Complex::Complex(double r, double i)
{
real = r;
image = i;
}
Complex::Complex(const Complex& cmin)
{
real = cmin.real;
image = cmin.image;
}
Complex::~Complex(){}
Complex& Complex::operator = (const Complex& cmin)
{
real = cmin.real;
image = cmin.image;
return (*this);
}
Complex& Complex::operator = (double cminr)
{
real = cminr;
image = 0.;
return (*this);
}
Complex Complex::add (const Complex& cmin) const
{
Complex res;
res.real = real + cmin.real;
res.image = image + cmin.image;
return res;
}
void Complex::add (double cminr)
{
real += cminr;
}
Complex add(const Complex& c1, const Complex& c2)
{
return c1.add(c2);
}
Complex Complex::sub (const Complex& cmin) const
{
Complex res;
res.real = real - cmin.real;
res.image = image - cmin.image;
return res;
}
void Complex::sub (double cminr)
{
real -= cminr;
}
void Complex::display() const
{
printf("Complex: %f+%fi\n", real, image);
}
//main.cpp
#include "Complex.hpp"
int main()
{
Complex c1(3.4, 5.0);
Complex c2 = c1;
Complex c3;
c1.add(5.0);
c2.sub(5.0);
c3 = c1.add(c2);
c3.display();
c3 = c1.sub(c2);
c3.display();
c3 = add(c1, c2);
c3.display();
c3 = 10;
c3.display();
return 0;
}
#endif
#ifndef __COMPLX__
#define __COMPLX__
class Complex
{
public:
Complex();
Complex(double, double);
Complex(const Complex&);
~Complex();
Complex& operator = (const Complex&);
Complex& operator = (double);
void add(double);
Complex add(const Complex&) const;
friend Complex add(const Complex&, const Complex&);
void sub(double);
Complex sub(const Complex&) const;
void display() const;
private:
double real;
double image;
};
//Complex.cpp
#include "Complex.hpp"
#include <cstdio>
Complex::Complex()
{
real = 0.;
image = 0.;
}
Complex::Complex(double r, double i)
{
real = r;
image = i;
}
Complex::Complex(const Complex& cmin)
{
real = cmin.real;
image = cmin.image;
}
Complex::~Complex(){}
Complex& Complex::operator = (const Complex& cmin)
{
real = cmin.real;
image = cmin.image;
return (*this);
}
Complex& Complex::operator = (double cminr)
{
real = cminr;
image = 0.;
return (*this);
}
Complex Complex::add (const Complex& cmin) const
{
Complex res;
res.real = real + cmin.real;
res.image = image + cmin.image;
return res;
}
void Complex::add (double cminr)
{
real += cminr;
}
Complex add(const Complex& c1, const Complex& c2)
{
return c1.add(c2);
}
Complex Complex::sub (const Complex& cmin) const
{
Complex res;
res.real = real - cmin.real;
res.image = image - cmin.image;
return res;
}
void Complex::sub (double cminr)
{
real -= cminr;
}
void Complex::display() const
{
printf("Complex: %f+%fi\n", real, image);
}
//main.cpp
#include "Complex.hpp"
int main()
{
Complex c1(3.4, 5.0);
Complex c2 = c1;
Complex c3;
c1.add(5.0);
c2.sub(5.0);
c3 = c1.add(c2);
c3.display();
c3 = c1.sub(c2);
c3.display();
c3 = add(c1, c2);
c3.display();
c3 = 10;
c3.display();
return 0;
}
#endif
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询