设计一个复数类,重载运算符+、—.在Main中定义两个复数对象并用+、—运算获取结果.
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/24 04:01:25
设计一个复数类,重载运算符+、—.在Main中定义两个复数对象并用+、—运算获取结果.
设计一个复数类,重载运算符+、—.在Main中定义两个复数对象并用+、—运算获取结果.
设计一个复数类,重载运算符+、—.在Main中定义两个复数对象并用+、—运算获取结果.
// 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