C++走向远洋——48(项目一1、复数类中的运算符重载、类的成员函数)
*/
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名:text.cpp
* 作者:常轩
* 微信公众号:Worldhello
* 完成日期:2016年5月14日
* 版本号:V1.0
* 问题描述:复数类中的运算符重载
* 程序输入:无
* 程序输出:见运行结果
*/
#include<iostream>
using namespace std; class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r; imag=i;}
Complex operator+(const Complex &c2);
Complex operator-(const Complex &c2);
Complex operator*(const Complex &c2);
Complex operator/(const Complex &c2);
void display();
private:
double real;
double imag;
}; //下面定义成员函数 Complex Complex::operator+(const Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
Complex Complex::operator-(const Complex &c2)
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
Complex Complex::operator*(const Complex &c2)
{
Complex c;
c.real=real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
return c;
}
Complex Complex::operator/(const Complex &c2)
{
Complex c;
c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
return c;
} void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
} //下面定义用于测试的main()函数
int main()
{
Complex c1(3,4),c2(5,-10),c3;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
c3=c1+c2;
cout<<"c1+c2=";
c3.display();
c3=c1-c2;
cout<<"c1-c2=";
c3.display();
c3=c1*c2;
cout<<"c1*c2=";
c3.display();
c3=c1/c2;
cout<<"c1/c2=";
c3.display();
return 0;
}
运算结果:
心得:
以后要细心,做这个程序时看着实践指导敲,还敲错了好几个地方
C++走向远洋——48(项目一1、复数类中的运算符重载、类的成员函数)的更多相关文章
- C++走向远洋——49(项目一2、复数类中的运算符重载、类的友元函数)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- C++走向远洋——50(Time类中的运算符重载、一目,二目比较运算符、二目赋值运算符、二目加减法运算符)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- C++走向远洋——(项目二、存储班长信息的学生类、派生)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- 如何将Eclipse中的项目迁移到Android Studio 中
如何将Eclipse中的项目迁移到Android Studio 中 如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的 ...
- Myeclipse中导入项目后java类中汉字注释出现乱码问题(已解决)
今天重装系统,安装了新的Myeclipse后,导入之前的项目后,,出现了乱码问题.乱码问题主要是java类中的注释,而jsp页面中汉字却完好如初: 右键项目,查看项目的编码格式,UTF-8,把java ...
- 【tomcat 无法部署】svn上下载的maven项目无法部署到tomcat中
问题: svn上下载的maven项目无法部署到tomcat中,tomcat不识别项目,但是这个项目确实是web项目 发现的过程: 然后依次产看项目的编译版本: 项目的依赖架包: 才发现: 解决方法: ...
- MVC项目实践,在三层架构下实现SportsStore,从类图看三层架构
在"MVC项目实践,在三层架构下实现SportsStore-02,DbSession层.BLL层"一文的评论中,博友浪花一朵朵建议用类图来理解本项目的三层架构.于是就有了本篇: I ...
- [转]-如何将Eclipse中的项目迁移到Android Studio 中
英文地址:http://developer.android.com/sdk/installing/migrate.html 翻译:Android Studio 中文组(大锤译) 如果你之前有用Ecli ...
- studio--如何将Eclipse中的项目迁移到Android Studio 中
如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的是生成Build Gradle的文件.因为Android Studi ...
随机推荐
- MySQL报错解决:The MySQL server is running with the --read-only option so it cannot execute this statement
MySQL报错:The MySQL server is running with the --skip-grant-tables option so it cannot execute this st ...
- pom配置项目build jar 包含源码的配置
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...
- 微信中的APP、公众号、小程序的openid及unionid介绍
微信中的APP.公众号.小程序的openid及unionid介绍 1.unionid 如果开发者拥有多个移动应用.网站应用.和公众帐号(包括小程序),可通过 UnionID 来区分用户的唯一性,因为只 ...
- for in 循环获取json中的键(key)与值(value)
一 .for in 循环 1.获取json中的键(key)与值(value): var data = {name:'张三',age:'20岁',sex:'男'}; for (var a in data ...
- vue_webpack初始化项目
整体架构:此处npm安装过于缓慢,因此使用的是淘宝的镜像cnpm vue+webpack 初始化项目:1.安装vue: cnpm install vue 检验版本: vue -V2.创建一个vue项目 ...
- 吴裕雄--天生自然 Hadoop大数据分布式处理:关闭CenterOS 7防火墙
使用命令:systemctl status firewalld.service 查看防火墙状态 执行后可以看到绿色字样标注的“active(running)”,说明防火墙是开启状态 使用命令:syst ...
- 作业:for循环,迭代法和穷举法
for()循环 四要素:初始条件,循环条件,状态改变,循环体. 执行过程:初始条件--循环条件--循环体 ...
- The Pomodoro Technique
目录 简介 What to solve How to use Some applications 自我总结 结束语 简介 番茄工作法是简单易行的时间管理方法,是由弗朗西斯科·西里洛于1992年创立的一 ...
- Linux下重要文件
1:/etc/sysconfig/network-scripts/ifcfg-ens130 2: /etc/resolv.conf DNS配置文件 3:/etc/hosts 4:/etc/sy ...
- [LC] 209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...