复数类(C++练习一)
写一个复数类,实现基本的运算,目的熟悉封装与数据抽象。
- 类的定义
#include <iostream>
#include <vector>
using namespace std;
class Complex{
friend ostream & operator << (ostream & os, const Complex &); //重载输出标识符
friend Complex operator + (const Complex&, const Complex &);
friend Complex operator - (const Complex&, const Complex &);
friend Complex operator * (const Complex&, const Complex &);
friend Complex operator % (const Complex&, const Complex &);
public:
Complex() = default;
Complex(int a, int b) : first(a), second(b) {}; //构造函数
Complex(const Complex&); //拷贝构造函数;参数必须是const型的
Complex & operator = (const Complex &); //拷贝赋值运算符 Complex & operator += (const Complex &);
Complex & operator -= (const Complex &);
Complex & operator *= (const Complex &);
Complex & operator %= (const Complex &);
~Complex(){}; //析构函数
private:
int first = ;
int second = ;
};
- 类函数的实现
#include "complex.h"
using namespace std;
ostream & operator << (ostream &os, const Complex &item)
{
if (item.second > )
os << item.first << " + " << item.second << "i";
else if (item.second < )
os << item.first << " - " << -item.second << "i";
else
os << item.first;
return os;
} Complex::Complex(const Complex& rhs)
{
this->first = rhs.first;
this->second = rhs.second;
} Complex &Complex::operator= (const Complex &rhs)
{
this->first = rhs.first;
this->second = rhs.second;
return *this;
}
// 重载运算符 += 和 + :各自实现
Complex & Complex::operator += (const Complex & rhs) //类内的重载运算符,参数只能有一个
{
this->first += rhs.first;
this->second += rhs.second;
return *this;
} Complex operator + (const Complex &lhs, const Complex &rhs)
{
Complex tmp;
tmp.first = lhs.first + rhs.first;
tmp.second = lhs.second + rhs.second;
return tmp;
}
// 重载运算符- 和-= : -= 用 - 来实现
Complex operator - (const Complex &lhs, const Complex &rhs)
{
Complex tmp;
tmp.first = lhs.first - rhs.first;
tmp.second = lhs.second - rhs.second;
return tmp;
} Complex & Complex::operator-= (const Complex &rhs)
{
*this = *this - rhs;
return *this;
}
// 重载运算符 *= 和 * :* 用 *= 来实现
Complex & Complex::operator *= (const Complex &rhs)
{
int tmpFirst = first * rhs.first;
if (second * rhs.second > )
tmpFirst -= second * rhs.second;
else
tmpFirst += second * rhs.second;
second = first * rhs.second + second * rhs.first;
first = tmpFirst;
return *this;
} Complex operator * (const Complex &lhs, const Complex &rhs)
{
Complex tmp = lhs;
tmp *= rhs;
return tmp;
}
- %和%=没有实现,和前面的应该都一样
- 在同时定义了算术运算符和相关的复合赋值运算符时,通常情况下用复合赋值来实现算术运算符
- 对于类外的重载运算符,返回的不是引用,在函数内定义的临时变量在离开时不会被系统收回,如果返回引用则会指向未开辟的区域
- 对于类内的重载运算符,会默认把第一个参数绑定到this上,所以形参永远少一个,所以对于二目的运算符,只能有一个参数
- friend只是告诉类友元,并没有声明;最好在头文件中再次单独声明一次来满足可移植性
- 在visul stdio中可以直接在其他文件中定义,自动会找到
- 如果在clang编译器中需要在头文件中声明一次,包含进去
- 注意在友元函数声明时,如果要写在类前面,必须保证类已经声明过
复数类(C++练习一)的更多相关文章
- [GeekBand] C++学习笔记(1)——以复数类为例
本篇笔记以复数类(不含指针的类)为例进行面向对象的学习 ========================================================= 复数类的声明: class ...
- C++习题 复数类--重载运算符2+
Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意.例如,c1+ ...
- C++习题 复数类--重载运算符+
Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.将运算符函数重载为非成员.非友元的普通函数.编写程序,求两个复数之和. Input ...
- C#复数类的总结
复数是C#中没有的,不能直接调用的.但是我们可以通过封装,构造自己的复数形式.这里我自己封装了一个Complex类,也不知道写得如何.可能还有一些东西没有考虑. 不过这里包含了复数的基本晕算了了,包括 ...
- 15.C++-操作符重载、并实现复数类
首先回忆下以前学的函数重载 函数重载 函数重载的本质为相互独立的不同函数 通过函数名和函数参数来确定函数调用 无法直接通过函数名得到重载函数的入口地址 函数重载必然发生在同一个作用域中 类中的函数重载 ...
- C++ 实验 使用重载运算符实现一个复数类
实验目的: 1.掌握用成员函数重载运算符的方法 2.掌握用友元函数重载运算符的方法 实验要求: 1.定义一个复数类,描述一些必须的成员函数,如:构造函数,析构函数,赋值函数,返回数据成员值的函数等. ...
- 定义一个复数(z=x+iy)类Complex,包含: 两个属性:实部x和虚部y 默认构造函数 Complex(),设置x=0,y=0 构造函数:Complex(int i,int j) 显示复数的方法:showComp()将其显示为如: 5+8i或5-8i 的形式。 求两个复数的和的方法:(参数是两个复数类对象,返回值是复数类对象)public Complex addComp(Compl
因标题框有限,题目未显示完整,以下再放一份: 定义一个复数(z=x+iy)类Complex,包含: 两个属性:实部x和虚部y 默认构造函数 Complex(),设置x=0,y=0 构造函数:Compl ...
- 侯捷《C++面向对象开发》——动手实现自己的复数类
前言 最近在看侯捷的一套课程<C++面向对象开发>,刚看完第一节introduction之后就被疯狂圈粉.感觉侯捷所提及所重视的部分也正是我一知半解的知识盲区,我之前也写过一些C++面向对 ...
- YTU 2443: C++习题 复数类--重载运算符3+
2443: C++习题 复数类--重载运算符3+ 时间限制: 1 Sec 内存限制: 128 MB 提交: 1368 解决: 733 题目描述 请编写程序,处理一个复数与一个double数相加的运 ...
随机推荐
- Qt窗口句柄
关键字: 透明效果,异形,子窗口,控件,浮窗,同级句柄
- Kendo Web UI Grid里时间格式转换
我和大家分享一下我的kendo的学习心得.如果不好的地方多多包含或者给我留言请看图 kendo里时间格式如图Oder Date列里的格式.但是我们想把时间转换成中国人习惯的格式.如Shipped Da ...
- Python操作 Memcache、Redis、RabbitMQ、SQLAlchemy
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- Java学习笔记--JDBC数据库的使用
参考 hu_shengyang的专栏 : http://blog.csdn.net/hu_shengyang/article/details/6290029 一. JDBC API中提供的常用数据库 ...
- Java笔记--File,FileInputStream,FileReader,InputStreamReader,BufferedReader 的使用和区别
转自:http://hi.baidu.com/danghj/item/0ef2e2c4ab95af7489ad9e39 参考资料: < core java > 12 章 使用 Java ...
- INI文件格式
最近在看git命令,遇到INI文件格式,上网查了一下,把它总结一下: 程序没有任何配置文件,那么它对外是全封闭的,一旦程序需要修改一些参数必须要修改程序代码本身并重新编译,为了让程序出厂后还能根据需要 ...
- 让WordPress的作者在后台只能看到自己的文章
今天需要对WordPress后台进行调整,目的是为了只能让当前用户看见自己所发表的文章,而WordPress默认是登陆用户可以看到所有用户发表的文章. WordPress中的用户角色分的比较详细,作者 ...
- PostgreSQL的时间函数使用整理
PG的时间函数使用整理如下 1.获取系统时间函数 select now(); --2012-05-12 18:51:59.562+08 select current_timestamp; --2012 ...
- 去除winXP访问共享的“记住密码”
控制面板->用户帐户,选择自己的用户,在左侧的管理我的网络密码里有删除选项 控制面板-->用户-->点击你登陆用户-->点击左上角“管理我的网络密码”-->在列表中删除密 ...
- rsyslog VS syslog-ng,日志记录哪家强?
还有慢慢摸索,NG的MYSQL配置,我始终没搞好. RSYSLOG则比较容易. 另外,也可以每个RSYSLOG直接入库,不需要经过LOG SERVER..如果有一个大内网的话... 配合LOGANAL ...