c++篇——从零开始学类【三】重载,对于复数

c++篇——从零开始学类【三】

hello大家好,最近在做c++实验,就正好更新以下c++一大特色类的使用,主要通过实验来进行讲解,欢迎大家观看学习以及讨论~~

本次内容

先和大家复习以下重载的概念然后进行对于复数的重载还有对于输入和输出流的重载
重载
大家不会的可以先在次网页看看一些相关的内容
重载重载在我看来就是通过一些方式来实现更新,毕竟有时候我们需要更,不然在实现一些地方的时候会很麻烦

实验目的

1创建一个复数类,通过运算符重载方式实现复数的“+、-、X、/”运算。
2.重载流插入运算符“<<”
3.重载流提取运算符“>>”
4.实现单目运算符“++,–”的重载,要求分别实现前置与后置运算符的重载,“++,–”的计算是虚部与实部都加1。

实验实现

这次实验是不是看起来很简单,我们只需要实现加减乘除就可以了,这次实验就和大家看起来那样很简单也很好实现,只需要一些模板代码就可以实现了
所以我们开始吧,先实现+ - * /

	complex(double real=0.0,double image=0.0);
		complex operator+(const complex &c);
		complex operator-(const complex &c);
		complex operator*(const complex &c);
		complex operator/(const complex &c);
————————————————————————————————————————————————————————————————————————————————
//然后分别实现几个构造函数,特别提醒:这里默认函数是一定需要的
//因为我们需要默认函数来导入我们后面输入的复数数据
complex::complex(double real,double image){
	this->real=real;
	this->image=image;
}
complex complex::operator+(const complex &c){
	complex c1;
	c1.real=real+c.real;
	c1.image=image+c.image;
	return c1;
}
complex complex::operator-(const complex& c){
	complex c1;
	c1.real=real-c.real;
	c1.image=image-c.image;
	return c1;
}
complex complex::operator*(const complex& c){
	complex c1;
	c1.real=real*c.real-image*c.image;
	c1.image=image*c.real+real*c.image;
	return c1;

}
complex complex::operator/(const complex& c){
	complex c1;
	c1.real=(real*c.real+image*c.image)/(c.real*c.real+c.image*c.image);
	c1.image=(image*c.real-real*c.image)/(c.real*c.real+c.image*c.image);
	return c1;
}

是不是很简单吧,复数的形式该加加,该减减,该乘乘,该除除,知道知道复数的运算方法和重载模板就可以实现
如何实现的重载也很简单,就是我们在每个运算函数里面再重新定义一个相关类,然后将我们的默认数据进行运算后赋值给新定义的类,最后再返回我们新定义的这个类就可以实现啦
然后是++,–的前置和后置的实现

		friend complex operator--(complex &c);
		friend complex operator--(complex &c,int);
		friend complex operator++(complex &c);
		friend complex operator++(complex &c,int);
————————————————————————————————————————————————————————————————————————————————
complex operator--(complex &c){
	--c.real;
	--c.image;
	return c;
}
complex operator--(complex &c,int){
	complex c1=c;
	c.real--;
	c.image--;
	return c1;
}
complex operator++(complex &c){
	++c.real;
	++c.image;
	return c;
}
complex operator++(complex &c,int){
	complex c1=c;
	c.real++;
	c.image++;
	return c1;
}

这个也是一样的道理啦,咱就不都说了
然后是输入输出重载,依旧是模板化

	friend ostream& operator<<(ostream& cout, const complex& c2);
	friend istream& operator>>(istream& cin,complex& c2);
————————————————————————————————————————————————————————————————————————————————
ostream& operator<<(ostream& cout, const complex& c){
	if (c.image != 0) {
		cout << c.real << " + " << c.image << "i";
	}
	else {
		cout <<c.real;
	}
	return cout;
}
istream& operator>>(istream& cin,complex& c){
	cin>>c.real>>c.image;
	return cin;
}

不过我们这里要注意的是因为我们计算复数的形式多种多样,有实数虚数都输入,或者只输入实数或者只输入虚数,所以这里可以根据自己的要求进行修改和自己多定义一些自己需要的函数,正如完整代码中我还多定义了几个就是为了实现单独的实数与复数的计算而构造的,所以大家可以自己进行修改和定义
那么话不多说我们直接看最后完整代码吧

#include<iostream>
using namespace std;
class complex{
	friend ostream& operator<<(ostream& cout, const complex& c2);
	friend istream& operator>>(istream& cin,complex& c2);
	public:
		complex(double real=0.0,double image=0.0);
		complex operator+(const complex &c);
		complex operator-(const complex &c);
		complex operator*(const complex &c);
		complex operator/(const complex &c);
		friend complex operator--(complex &c);
		friend complex operator--(complex &c,int);
		friend complex operator++(complex &c);
		friend complex operator++(complex &c,int);
		double real,image;
		friend complex operator+(double n, const complex& c);
		friend complex operator-(double n, const complex& c);
		friend complex operator*(double n, const complex& c);
		friend complex operator/(double n, const complex& c);

};
complex::complex(double real,double image){
	this->real=real;
	this->image=image;
}
complex complex::operator+(const complex &c){
	complex c1;
	c1.real=real+c.real;
	c1.image=image+c.image;
	return c1;
}
complex complex::operator-(const complex& c){
	complex c1;
	c1.real=real-c.real;
	c1.image=image-c.image;
	return c1;
}
complex complex::operator*(const complex& c){
	complex c1;
	c1.real=real*c.real-image*c.image;
	c1.image=image*c.real+real*c.image;
	return c1;

}
complex complex::operator/(const complex& c){
	complex c1;
	c1.real=(real*c.real+image*c.image)/(c.real*c.real+c.image*c.image);
	c1.image=(image*c.real-real*c.image)/(c.real*c.real+c.image*c.image);
	return c1;
}
complex operator--(complex &c){
	--c.real;
	--c.image;
	return c;
}
complex operator--(complex &c,int){
	complex c1=c;
	c.real--;
	c.image--;
	return c1;
}
complex operator++(complex &c){
	++c.real;
	++c.image;
	return c;
}
complex operator++(complex &c,int){
	complex c1=c;
	c.real++;
	c.image++;
	return c1;
}
complex operator+(double n, const complex& c){
	complex c1;
	c1.real=c.real+n;
	c1.image=c.image;
	return c1;
}

complex operator-(double n, const complex& c){
	complex c1;
	c1.real=c.real-n;
	c1.image=c.image;
	return c1;
}

complex operator*(double n, const complex& c){
	complex c1;
	c1.real=c.real*n;
	c1.image=c.image*n;
	return c1;
}

complex operator/(double n, const complex& c){
	complex c1;
		c1.real = (c.real*n)/(c.real*c.real+c.image*c.image);
		c1.image = (c.image*n)/(c.real*c.real+c.image*c.image);
	return c1;
}
ostream& operator<<(ostream& cout, const complex& c){
	if (c.image != 0) {
		cout << c.real << " + " << c.image << "i";
	}
	else {
		cout <<c.real;
	}
	return cout;
}
istream& operator>>(istream& cin,complex& c){
	cin>>c.real>>c.image;
	return cin;
}
int main(){
	int a,b;
	cin>>a>>b;
	complex c1(a,b);
	complex c2(6,6);
	cout<<"c1="<<c1<<'\t'<<"c2 ="<<c2<<endl;
	cout<<"c1+c2="<<c1+c2<<endl;
	cout<<"c1-c2="<<c1-c2<<endl;
	cout<<"c1*c2="<<c1*c2<<endl;
	cout<<"c1/c2="<<c1/c2<<endl;
	cout <<"c1+10="<<c1+10<<endl;
	cout <<"c1-10="<<c1-10<<endl;
	cout <<"c1*10="<<c1*10<<endl;
	cout <<"c1/10="<<c1/10<<endl;
	cout <<"10+c1="<<10+c1<<endl;
	cout<<"c1--="<<c1--<<endl;
	cout<<"--c1="<<--c1<<endl;
	cout<<"c1++="<<c1++<<endl;
	cout<<"++c1="<<++c1<<endl;
	return 0;
}

最后得到这样的结果就可以啦
在这里插入图片描述
这一次学习就结束了,你学废了嘛

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酱油牌酱油菌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值