实例003——操作符重载实现编译多态——复数的加法运算

一、运算符重载的限制:

二、源码(VS2017):

// ReloadOperate.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
class CComplex												//定义复数类
{
	double m_fReal, m_fImag;//成员变量
public:
	CComplex(double r = 0, double i = 0) : m_fReal(r), m_fImag(i) {}			//构造函数
	double GetReal() { return m_fReal; }//成员函数,返回复数的实部
	double GetImag() { return m_fImag; }//成员函数,返回复数的虚部
	CComplex operator +(CComplex&);							//重载运算符+
	CComplex operator +(double);								//重载运算符+
};
CComplex CComplex::operator + (CComplex &c)				//重载运算符+,实现两个复数的加法
{
	CComplex temp;
	temp.m_fReal = m_fReal + c.m_fReal;
	temp.m_fImag = m_fImag + c.m_fImag;
	return temp;
}
CComplex CComplex::operator + (double d)				//重载运算符+,实现复数与实数的加法
{
	CComplex temp;
	temp.m_fReal = m_fReal + d;
	temp.m_fImag = m_fImag;
	return temp;
}

void main()//主函数
{
	CComplex c1(2.3, 5.5), c2(3.6, 6.8), c3;//复数类对象
	std::cout << "使用重载的+运算符实现复数与复数的相加" << std::endl;
	std::cout << "C1 = " << c1.GetReal() << "+j" << c1.GetImag() << std::endl;
	std::cout << "C2 = " << c2.GetReal() << "+j" << c2.GetImag() << std::endl;
	c3 = c1 + c2;
	std::cout << "C3 = C1 + C2 =" << c3.GetReal() << "+j" << c3.GetImag() << std::endl;
	std::cout << "使用重载的+运算符实现复数与实数的相加" << std::endl;
	c3 = c3 + 6.5;
	std::cout << "C3 + 6.5 = " << c3.GetReal() << "+j" << c3.GetImag() << std::endl;
}

 三、运算结果:

资源:

               https://download.csdn.net/download/sunjikui1255326447/12101579 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.io.*; public class Book{ double sb; double xb; Book(double x,double y){ this.sb=x; this.xb=y; } Book(){ } public static void main(String args[]){ System.out.println("请输入数据:"); double a=0; double b=0; double c=0; double d=0; String s; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入第一个复数的实部:"); try{ s = in.readLine(); a=Double.parseDouble(s); } catch(IOException e) { System.out.println("抛掷异常");} System.out.println("请输入第一个复述的虚部:"); try{ s = in.readLine(); b =Double.parseDouble(s); } catch(IOException e) { System.out.println("抛掷异常");} System.out.println("请输入第二个复述的实部:"); try{ s = in.readLine(); c =Double.parseDouble(s); } catch(IOException e) { System.out.println("抛掷异常");} System.out.println("请输入第二个复述的虚部:"); try{ s = in.readLine(); d =Double.parseDouble(s); } catch(IOException e) { System.out.println("抛掷异常");} Book h; h=new Book(a,b); Book j; j=new Book(c,d); System.out.println("您输入的一个数为:"); toString(h); System.out.println("您输入的二个数为:"); toString(j); Book k; k=new Book(); char z='y'; do{ System.out.println("请选择您要进行的计算:"); System.out.println("1 :进行加法运算"); System.out.println("2 :进行减法运算"); System.out.println("3 :进行修改"); System.out.println("4 :进行乘法运算"); System.out.println("5 :进行除法运算"); System.out.println("6 :查看修改结果"); int i=0; try{ i= Integer.parseInt(in.readLine()); } catch(IOException e) { System.out.println("抛掷异常");} switch(i) { case 1: k.sb=jia(h.sb,j.sb); k.xb=jia(h.xb,j.xb); System.out.println("计算结果的实部为:"+k.sb); System.out.println("计算结果的虚部为:"+k.xb); toString(k); break ; case 2: k.sb=jian(h.sb,j.sb); k.xb=jian(h.xb,j.xb); System.out.println("计算结果的实部为:"+k.sb); System.out.println("计算结果的虚部为:"+k.xb); toString(k); brea

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值