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;
}
最后得到这样的结果就可以啦
这一次学习就结束了,你学废了嘛