设计一个复数类,重载运算符+、—.在Main中定义两个复数对象并用+、—运算获取结果.

问题描述:

设计一个复数类,重载运算符+、—.在Main中定义两个复数对象并用+、—运算获取结果.
1个回答 分类:综合 2014-09-27

问题解答:

我来补答
// Complex.h
#pragma once
class CComplex
{
public:
CComplex(int real=0,int image=0)
{
m_real = real;
m_image = image;
}
CComplex()
{}
public:
int GetReal()
{
return m_real;
}
int GetImage()
{
return m_image;
}
public:
CComplex operator +(const CComplex &c)
{
return (CComplex(m_real+c.m_real,m_image+c.m_image));
}
CComplex operator -(const CComplex &c)
{
return (CComplex(m_real-c.m_real,m_image-c.m_image));
}
CComplex& operator =(const CComplex &c)
{
this->m_real = c.m_real;
this->m_image = c.m_image;
return (*this);
}
private:
int m_real;
int m_image;
};
// Main.cpp
#include
#include "Complex.h"
using namespace std;
void main(void)
{
CComplex comp1(10,23);
CComplex comp2(1,43);
CComplex res;
res = comp1 + comp2;
cout
 
 
展开全文阅读
剩余:2000
上一页:单调性最小值
下一页:溶质质量