复数类(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数相加的运 ...
随机推荐
- SimpleDateFormat的部分方法
1,语法格式:new SimpleDateFormat("yyyy-MM-dd HH:mm") parse:String-->Date format:Date-->St ...
- 记一次令人发狂的 bug Eclipse 开不开 tomcat 7.0
改项目,结果发现以前的项目也出了问题,就删除了系统用户下面workplace里的文件夹,结果,eclipse被清空,重新添加项目,发现一堆bug; 最让我崩溃的是,用tomcat 7.0跑项目,反复出 ...
- 用excel做一幅像素画
开发背景 看到网上有人发教程,如何通过在excel里设置单元格颜色画一幅画,感觉手工做太复杂,就打算用程序实现一个. 开发运行环境 python 2.7 PIL xlsxwriter 用法 pytho ...
- 在shell中运行以不同方式运行脚本
在shell当中,可以有3中方式运行脚本: 1 . ./script_name 或者source ./script_name 2 直接./script_name 3 ./script_name &am ...
- Longest Palindromic Substring -LeetCode
题目 Given a string s,find the longest palindromic substring in S.You may assume that the maximum len ...
- ORA-00845: MEMORY_TARGET not supported
Enabling Automatic Memory Management alter system set memory_max_target=50G scope=spfile; alter syst ...
- Altium Designer 生成 Mach3 G代码的程序
Altium Designer做PCB设计,还是很方便的,最近头脑发热,在网上买了一套CNC机床,用来做钻孔用,但是翻来翻去,基本上所有的软件都是铣削功能,而且很多软件很复杂.翻了好几天,发现没有什么 ...
- python学习(二)
这几天脑子里一直在想一个应用,想以此来练习python.用一句话来概括这个应用的功能,大致表述是这样:自动采集全省各类公共文化机构网站上新发布的信息,并分类呈现.各类公共文化机构,是指公共图书馆.文化 ...
- Identify Smith Numbers
Link: https://www.hackerrank.com/challenges/identify-smith-numbers def sum_digits(n): return sum(int ...
- js与objective-c的交互-备
在写 JavaScript 的时候,可以使用一个叫做 window 的对象,像是我们想要从现在的网页跳到另外一个网页的时候,就会去修改 window.location.href 的位置:在我们的 Ob ...