实验十六运算符重载解读
第12-13讲 运算符重载

7.1.2 运算符重载规则
可以重载C++中除下列运算符外的所有运算符: 类属关系运算符. 成员指针运算符* 作用域分辨符:: sizeof运算符 三目运算符?: 只能重载C++语言中已有的运算符,不可臆造新的。 不能改变操作数个数。 不能改变原运算符的优先级和结合性。 不能改变运算符对预定义类型数据的操作方式。 经重载的运算符,其操作数中至少应该有一个是自定 义类型。
14
void complex:: print()//显示输出复数 { cout<<real; if(imag>0) cout<<"+"; if(imag!=0) cout<<imag<<"i";cout<<endl; } void main() { complex A1(2.3,4.6),A2(3.6,2.8),A3,A4,A5,A6;//定义6个复数类对象 A1.print(); A2.print(); A3=A1+A2; A3.print(); A4=A1-A2; A4.print(); A5=A1*A2; A5.print(); A6=A1/A2; A6.print(); }
2
class complex { public: double real,imag; //定义一个实部,一个虚部变量 complex(double r=0,double i=0) { real=r; imag=I; } }; main() { complex com1(1.1,2.2),com2(3.3,4.4),total; total=com1+com2; 错误,2个自定义的数据类 return 0; 型complex类型对象相加, } C++无法实现. 希望实现total=com1+com2;重载“+”
运算符重载实验报告

运算符重载实验报告运算符重载实验报告引言:运算符重载是C++语言中的一项重要特性,它允许用户自定义运算符的行为。
通过运算符重载,可以使得程序更加直观、简洁,并提高代码的可读性和可维护性。
本实验旨在探索运算符重载的用法和效果。
一、实验目的本实验旨在通过实际操作,深入了解运算符重载的机制和使用方法,以及运算符重载对程序设计的影响。
二、实验环境本实验使用C++编程语言,并在Visual Studio开发环境下进行实验。
三、实验过程1. 了解运算符重载的基本概念运算符重载是指通过定义函数,改变运算符的行为。
在C++中,可以通过重载运算符函数来实现运算符的重载。
运算符重载函数的命名规则为"operator 运算符",例如"operator+"表示重载加法运算符。
2. 实现运算符重载的实验示例为了更好地理解运算符重载的使用方法,我们以矩阵的加法为例进行实验。
首先,定义一个Matrix类,并重载"+"运算符。
```cppclass Matrix {private:int** data;int rows;int cols;public:Matrix(int rows, int cols) {this->rows = rows;this->cols = cols;data = new int*[rows];for (int i = 0; i < rows; ++i) {data[i] = new int[cols];}}Matrix operator+(const Matrix& other) {Matrix result(rows, cols);for (int i = 0; i < rows; ++i) {for (int j = 0; j < cols; ++j) {result.data[i][j] = data[i][j] + other.data[i][j]; }}return result;}};```在上述代码中,我们定义了一个Matrix类,其中包含矩阵的数据成员data、行数rows和列数cols。
C++基础知识之运算符重载详解

C++基础知识之运算符重载详解⽬录运算符重载⽅式⼀, 使⽤成员函数重载运算符需求:把⽜⾁换猪⾁, ⽺⾁换猪⾁⽅式⼆, 使⽤⾮成员函数【友元函数】重载运算符两种⽅式的区别两种⽅式的选择:总结运算符重载为什么要使⽤运算符重载-C/C++的运算符,⽀持的数据类型,仅限于基本数据类型。
问题:⼀头⽜+⼀头马 = ?(⽜马神兽?)⼀个圆 +⼀个圆 = ?(想要变成⼀个更⼤的圆)⼀头⽜ – ⼀只⽺ = ? (想要变成4只⽺,原始的以物易物:1头⽜价值5只⽺)解决⽅案:使⽤运算符重载⽅式⼀, 使⽤成员函数重载运算符需求:把⽜⾁换猪⾁, ⽺⾁换猪⾁规则:⼀⽄⽜⾁:2⽄猪⾁⼀⽄⽺⾁:3⽄猪⾁实现:⽜ + ⽜ = ?猪⾁⽜ + ⽺ = ?猪⾁Cow类> Cow.h#pragma onceclass Pork;class Sheep;class Cow{ //⽜类public:Cow(int weight = 0);//使⽤运算符重载, 实现⽜⾁ + ⽜⾁ = 猪⾁Pork operator+(const Cow& cow);//使⽤运算符重载, 实现⽜⾁ + ⽺⾁ = 猪⾁Pork operator+(const Sheep& sheep);private:int weight; //重量};_________________________________________________________________________________________________________________________________ > Cow.cpp#include "Cow.h"#include "Pork.h"#include "Sheep.h"Cow::Cow(int weight){this->weight = weight;}//⼀⽄⽜⾁换两⽄猪⾁Pork Cow::operator+(const Cow& cow){return Pork((this->weight + cow.weight) * 2);}//⼀⽄⽜⾁换两⽄猪⾁, ⼀⽄⽺⾁换三⽄猪⾁Pork Cow::operator+(const Sheep& sheep){int tmp = (this->weight * 2) + (sheep.getWeight() * 3);return Pork(tmp);}Sheep类> Sheep.h#pragma once//⽺类class Sheep{public:Sheep(int weight = 0);int getWeight() const;private:int weight; //重量};_________________________________________________________________________________________________________________________________ > Sheep.cpp#include "Sheep.h"Sheep::Sheep(int weight){this->weight = weight;}int Sheep::getWeight() const{return weight;}Pork类> Pork.h#pragma once#include <string>using namespace std;class Pork{ //猪⾁类public:Pork(int weight = 0);string description() const;private:int weight;};_________________________________________________________________________________________________________________________________ > Pork.cpp#include <sstream>#include "Pork.h"Pork::Pork(int weight){this->weight = weight;}string Pork::description() const{stringstream ret;ret << this->weight << "⽄";return ret.str();}main.cpp#include <iostream>#include <Windows.h>#include "Cow.h"#include "Pork.h"#include "Sheep.h"using namespace std;int main(void) {Pork p1;Cow c1(100);Cow c2(200);Sheep s1(100);//调⽤运算符重载 Pork operator+(const Cow& cow);p1 = c1 + c2;cout << "⽜ + ⽜ = 猪⾁:" << p1.description() << endl;//调⽤运算符重载 Pork operator+(const Sheep& c1);p1 = c1 + s1;cout << "⽜ + ⽺ = 猪⾁:" << p1.description() << endl;//⽺+⽜会报错, 因为没有定义对应的⽺+⽜运算符重载//p1 = s1 + c1;system("pause");return 0;}⽅式⼆, 使⽤⾮成员函数【友元函数】重载运算符实现:⽜ + ⽜ = ?猪⾁⽜ + ⽺ = ?猪⾁Cow类> Cow.h#pragma onceclass Pork;class Sheep;class Cow{ //⽜类public:Cow(int weight = 0);//使⽤友元运算符重载, 实现⽜⾁ + ⽜⾁ = 猪⾁friend Pork operator+(const Cow& c1, const Cow& c2);//使⽤友元运算符重载, 实现⽜⾁ + ⽺⾁ = 猪⾁friend Pork operator+(const Cow& c1, const Sheep& s1);private:int weight; //重量};_________________________________________________________________________________________________________________________________ > Cow.cpp#include "Cow.h"Cow::Cow(int weight){this->weight = weight;}Sheep类> Sheep.h#pragma once//⽺类class Sheep{public:Sheep(int weight = 0);int getWeight() const;private:int weight; //重量};_________________________________________________________________________________________________________________________________ > Sheep.cpp#include "Sheep.h"Sheep::Sheep(int weight){this->weight = weight;}int Sheep::getWeight() const{return weight;}Pork类> Pork.h#pragma once#include <string>using namespace std;class Pork{ //猪⾁类public:Pork(int weight = 0);string description() const;private:int weight;};_________________________________________________________________________________________________________________________________ > Pork.cpp#include <sstream>#include "Pork.h"Pork::Pork(int weight){this->weight = weight;}string Pork::description() const{stringstream ret;ret << this->weight << "⽄";return ret.str();}main.cpp#include <iostream>#include <Windows.h>#include "Cow.h"#include "Pork.h"#include "Sheep.h"using namespace std;//要想访问类的私有数据成员, 就把这个函数定义为友元Pork operator+(const Cow& c1, const Cow& c2) {return ((c1.weight + c2.weight) * 2);}//要想访问类的私有数据成员, 就把这个函数定义为友元Pork operator+(const Cow& c1, const Sheep& s1) {return((c1.weight * 2) + (s1.getWeight() * 3));}int main(void) {Pork p1;Cow c1(100); //100⽄的⽜Cow c2(200); //200⽄的⽜Sheep s1(100); //100⽄的⽺//调⽤ friend Pork operator+(const Cow& c1, const Cow& c2);p1 = c1 + c2;cout << "使⽤友元⽜ + ⽜ = 猪⾁:" << p1.description() << endl;//调⽤ friend Pork operator+(const Cow& c1, const Sheep& s1);p1 = c1 + s1;cout << "使⽤友元⽜ + ⽺ = 猪⾁:" << p1.description() << endl;system("pause");return 0;}两种⽅式的区别区别:使⽤成员函数来实现运算符重载时,少写⼀个参数,因为第⼀个参数就是this指针。
9.6.2 运算符重载[共11页]
![9.6.2 运算符重载[共11页]](https://img.taocdn.com/s3/m/6d98a6f80066f5335b812112.png)
答案 C
面试例题 34:重载和覆写有什么区别?
考点:重载和覆写之间区别的理解。 出现频率:★★★★ 解析 重载(overriding)是指子类改写父类的方法;覆写(overloading)是指同一个函数的不同 版本之间参数不同。 重载是编写一个与已有函数同名但是参数表不同(参数数量或参数类型不同)的方法,它 具有如下所示的特征。 方法名必须相同。 参数列表必须不相同,与参数列表的顺序无关。 返回值类型可以不相同。 覆写是派生类重写基类的虚函数,它具有如下所示的特征。 只有虚方法和抽象方法才能够被覆写。 具有相同的函数名。 具有相同的参数列表。 具有相同的返回值类型。 重载是一种语法规则,由编译器在编译阶段完成,不属于面向对象的编程;而覆写是由运 行阶段决定的,是面向对象编程的重要特征。
面试例题 35:编程题—MyString 类的编写。
考点:重载=和+运算符。
出现频率:★★★
对于下面的类 MyString,要求重载运算符后可以计算表达式 a=b+c(a、b、c 都是类 MyString
的对象)。请重载相应的运算符并编写程序测试。
1 class MyString
2{
3 public:
218 第 9 章 C++面向对象
在少数情况下重载 new、delete 运算符。 运算符重载需要遵循以下所示的规则; 重载的运算符不能违反语言的语法规则。 如果一个运算符可以放在两个操作数之间,就可以重载它来满足类操作的需要,哪怕
这种用法原本为编译器所不能接受。 不能创造 C++语言中没有的运算符。 重载时不能改变运算符的优先级。
个是“=”运算符,用于对象 a 的赋值。程序代码如下:
【免费下载】第8章 运算符重载

complex(double re=0.0,double im=0.0,char ch1='U' ); complex operator+(complex& c);//声明重载加法操作 void show(); private: double real,imag; char ch; }; complex::complex(double re,double im,char ch1 ) { real=re;imag=im;ch=ch1; cout<<"constructor is called!"<<endl; } complex complex::operator+(complex& c)
{ //定义重载加法操作,扩充加法操作的语义 double x,y; if(ch==c.ch){ x=real+c.real;y=imag+c.imag; return complex(x,y,ch); } else cout<<"can't execute the plus opetating!"<<endl; return complex(0,0,'U');
1
对全部高中资料试卷电气设备,在安装过程中以及安装结束后进行高中资料试卷调整试验;通电检查所有设备高中资料电试力卷保相护互装作置用调与试相技互术关,系电,力根保通据护过生高管产中线工资敷艺料设高试技中卷术资配,料置不试技仅卷术可要是以求指解,机决对组吊电在顶气进层设行配备继置进电不行保规空护范载高与中带资负料荷试下卷高总问中体题资配,料置而试时且卷,可调需保控要障试在各验最类;大管对限路设度习备内题进来到行确位调保。整机在使组管其高路在中敷正资设常料过工试程况卷中下安,与全要过,加度并强工且看作尽护下可关都能于可地管以缩路正小高常故中工障资作高料;中试对资卷于料连继试接电卷管保破口护坏处进范理行围高整,中核或资对者料定对试值某卷,些弯审异扁核常度与高固校中定对资盒图料位纸试置,.卷编保工写护况复层进杂防行设腐自备跨动与接处装地理置线,高弯尤中曲其资半要料径避试标免卷高错调等误试,高方要中案求资,技料编术试5写交卷、重底保电要。护气设管装设备线置备4高敷动调、中设作试电资技,高气料术并中课3试中且资件、卷包拒料中管试含绝试调路验线动卷试敷方槽作技设案、,术技以管来术及架避系等免统多不启项必动方要方式高案,中;为资对解料整决试套高卷启中突动语然过文停程电机中气。高课因中件此资中,料管电试壁力卷薄高电、中气接资设口料备不试进严卷行等保调问护试题装工,置作合调并理试且利技进用术行管,过线要关敷求运设电行技力高术保中。护资线装料缆置试敷做卷设到技原准术则确指:灵导在活。分。对线对于盒于调处差试,动过当保程不护中同装高电置中压高资回中料路资试交料卷叉试技时卷术,调问应试题采技,用术作金是为属指调隔发试板电人进机员行一,隔变需开压要处器在理组事;在前同发掌一生握线内图槽部纸内故资,障料强时、电,设回需备路要制须进造同行厂时外家切部出断电具习源高题高中电中资源资料,料试线试卷缆卷试敷切验设除报完从告毕而与,采相要用关进高技行中术检资资查料料和试,检卷并测主且处要了理保解。护现装场置设。备高中资料试卷布置情况与有关高中资料试卷电气系统接线等情况,然后根据规范与规程规定,制定设备调试高中资料试卷方案。
运算符重载

1、多态性的基本概念2、派生类对象替换基类对象3、虚函数的定义4、抽象类的定义5、宠物类的设计6、运算符重载7、日期类对象判断大小8、分数类对象运算符重载☐运算符重载指赋予运算符新的操作功能,主要用于对类的对象的操作☐运算符+意味着多少对象类型的加法呢?☐还可以定义新的对象类型加法☐运算符重载定义形式:<类型><类名>::operator<操作符>(<参数表>){函数体}☐首先定义虚数类☐虚数可以描述为:a+bi☐a与b看成实数,定义成double类型☐成员函数除了构造与析构外,还有:☐输出虚数、修改虚数、得到实部a、得到虚部b ☐相加+、判相等==#include <iostream>using namespace std;class Complex{private:double real, imag;public:Complex(double r = 0, double i = 0): real(r), imag(i){ }double Real(){return real;}double Imag(){return imag;}Complex operator +(Complex&);Complex operator +(double);bool operator ==(Complex);~Complex(){ };Complex Complex::operator +(Complex &c)// 重载运算符+,两边是虚数对象{Complex temp;temp.real = real+c.real;temp.imag = imag+c.imag;return temp;}Complex Complex::operator +(double d)// 重载运算符+,左边是虚数对象,右边是双精度数{Complex temp;temp.real = real+d;temp.imag=imag;return temp;}bool Complex::operator ==(Complex c)// 重载运算符=={if (real == c.real && imag == c.imag)return true;elseint main(){Complex c1(3,4),c2(5,6),c3;cout << "C1 = " << c1.Real() << "+j" << c1.Imag() << endl;cout << "C2 = " << c2.Real() << "+j" << c2.Imag() << endl;c3 = c1+c2;cout << "C3 = " << c3.Real() << "+j" << c3.Imag() << endl;c3 = c3+6.5;cout << "C3 + 6.5 = " << c3.Real() << "+j" << c3.Imag() << endl;if ( c1==c2 )cout<<“两个复数相等”;elsecout<<“两个复数不相等”;return 0;☐运算符++分前置运算符和后置运算符☐例如: ++Y与Y++☐前置运算符定义Complex Complex::operator ++ () {real+=1;return *this;}☐后置运算符定义Complex Complex::operator ++ (int) {real+=1;return *this;}。
C_运算符重载_各类详细介绍

▪ 说明
运算符重载函数 operator@()可以返回任何类型,甚至可 以是 void类型,但通常返回类型与它所操作的类的类型 相同,这样可使重载运算符用在复杂的表达式中。例如, 在例7-2中,可以将几个复数连续进行加、减、乘、除的 运算。
用友元函数重载单目运算符时,需要一个显式的操作数, 例7-3中,用友元函数重载单目运算符“-”
#include<iostream.h> class nclass{ int a,b; public:
nclass(int x=0,int y=0) { a=x;b=y;} friend nclass operator -(nclass obj); void show(); };
▪ complex operator+(complex com1,complex com2) { return complex(com1.real+com2.real,com1.imag+com2.imag;}
▪ 这种方法是直接将一个无名临时对象创建到主调函数中,那么 运行效率高于前一种。
▪ 单目运算符重载
nclass operator-(nclass obj) { obj.a=-obj.a;
obj.b=-obj.b; return obj;} void nclass::show() { cout<<"a="<<a<<" b"<<b;} ▪ main() ▪{ ▪ nclass ob1(10,20),ob2; ▪ ob1.show(); ▪ ob2=-ob1; ▪ ob2.show(); ▪ return 0; ▪}
C语言运算符的重载详解

C语⾔运算符的重载详解⽬录写⼀个Add函数为什么不⽤加号作为函数名运算符的重载上⾯问题解决总结写⼀个Add函数我们先讨论下⾯代码,并复习前⾯的内容class Complex{private:double Real, Image;public:Complex() :Real(0), Image(0) {}Complex(double r, double i) :Real(r), Image(i) {}~Complex() {}//Complex Add(const Complex* const this,const Complex &c)Complex Add(const Complex& x)const{Complex y;y.Real = Real + x.Real;y.Image = Image + x.Image;return y;//return Complex(this->Real + x.Real, this->Image + x.Image);}void Print(){cout << Real << "+" << Image << "i" << endl;}};int main(){Complex c1(12, 23);Complex c2(4.5, 5.6);Complex c3;c3 = c1.Add(c2);c3.Print();return 0;}直接return可以使⽤⽆名函数直接代替将亡值对象,相⽐可以省⼀次对象的构建我们再分析如果我们使⽤引⽤返回Add函数const Complex& Add(const Complex& x)const{Complex y;y.Real = Real + x.Real;y.Image = Image + x.Image;return y;//return Complex(this->Real + x.Real, this->Image + x.Image);}若我们以引⽤返回,将亡值对象会创建在Add函数的栈帧中,然后返回将亡值地址,函数return结束该空间会被释放、若没有引⽤,构建⽆名对象也就是将亡值对象会构建在主函数的空间中,这⾥使⽤将亡值对象值给到c3是没有问题的我们查看对象的构造与析构class Complex{private:double Real, Image;public:Complex() :Real(0), Image(0) {}Complex(double r, double i) :Real(r), Image(i){cout << "Create:" << this << endl;}Complex(const Complex& x):Real(x.Real),Image(x.Image){cout << "Copy Create:" << this << endl;}~Complex(){cout << "~Complex:" << this << endl;}//Complex Add(const Complex* const this,const Complex &c)Complex Add(const Complex& x)const{return Complex(this->Real + x.Real, this->Image + x.Image);}void Print(){cout << Real << "+" << Image << "i" << endl;}};int main(){Complex c1(12, 23);Complex c2(4.5, 5.6);Complex c3;c3 = c1.Add(c2);c3.Print();return 0;}⾸先我们使⽤引⽤返回需要加上const修饰,这是因为我们返回将亡值在临时空间具有常性,所以普通引⽤是不能进⾏返回的,需要使⽤常引⽤返回const Complex& Add(const Complex& x)const{return Complex(this->Real + x.Real, this->Image + x.Image);}我们发现对临时对象的构建后马上就进⾏析构,那么是怎么将数据拿出给到c3的?这个在最后我们进⾏分析为什么不⽤加号作为函数名//Complex operator+(const Complex* const this,const Complex &c)Complex operator+(const Complex &c) const{return Complex(this->Real + x.Real, this->Image + x.Image);}这⾥是不可以的,加号是⼀个操作符,不能使⽤操作放作为有效的函数名称;但是在C++中为了使这些操作符号能够当作函数名,那么我们需要在前⾯加上⼀个关键字operator//Complex operator+(const Complex* const this,const Complex &c)Complex operator+(const Complex &c) const{return Complex(this->Real + x.Real, this->Image + x.Image);}也就是告诉编译器,加号是⼀个有效的函数名,这就叫做运算符的重载;随后我们之间使⽤ c3 = c1 + c2 就是可以的int main(){Complex c1(12, 23);Complex c2(4.5, 5.6);Complex c3;c3 = c1 + c2;//编译器编译会是下⾯的情况//c3 = c1.operator+(c2);//c3 = operator+(&c1,c2); 加上this指针}运算符的重载⼀个对象,编译器会给它有6个缺省函数我们再来看下⾯这个问题//我们写⼀个赋值运算符重载void operator=(const Object& obj){this->value = obj.value;}//返回类型为void,这样不可以就不可以连等//obja = objb = objc;//obja = objb.operator=(objc);//obja = operator=(&objb,objc); 返回的⽆类型,不能给obja赋值且赋值函数不可以定义为const修饰Object& operator=(const Object& obj){this->value = obj.value;return *this;}obja = objb = objc;//改写obja = objb.operator=(objc);obja = operator=(&objb,objc);obja.operator=(operator=(&objb,objc));operator=(&obja,operator=(&objb,objc));通过返回对象,就可以实现连等;并且我们可以通过引⽤返回,因为此对象的⽣存期并不受函数的影响,不会产⽣⼀个临时对象作为⼀个过度防⽌⾃赋值若是我们将obja给obja赋值,也就是⾃赋值obja = objaoperator=(&obja,obja);我们就需要进⾏⼀步判断Object& operator=(const Object& obj){if(this != &obj)//防⽌⾃赋值{this->value = obj.value;}return *this;}上⾯问题解决我们通过这段代码来看,与上⾯问题相同Object& operator=(const Object& obj){if (this != &obj){this->value = obj.value;}return *this;}};Object& fun(const Object& obj){int val = obj.Value() + 10;Object obja(val);return obja;}int main(){Object objx(0);Object objy(0);objy = fun(objx);cout << objy.Value() << endl;return 0;}我们在这⾥希望通过引⽤返回,这⾥return的临时对象会构建在fun函数的栈帧中,并且在函数结束栈帧释放,随后调⽤赋值运算符重载,但是数值依旧是正确的我们跟踪这个被析构对象的地址,⾸先我们定位在fun函数的return obja;,随后进⼊析构函数将我们的obja进⾏析构接着运⾏到回到主函数进⾏赋值,接着进⼊赋值运算符重载,可以看到,这⾥的obj地址与已被析构的obja地址相同可以看到这个值依旧存在,依旧可以打印给出,这是因为vs2019的特殊性质造成的;我们每次运⾏程序会发现每次的对象地址都在变化,逻辑地址会随机改变,被析构对象的栈帧不会被接下来的赋值运算符重载扰乱地址空间,所以即使我们引⽤返回的对象已经死亡依旧可以将数值正确返回但是在vc中,我们得到的值会是随机值,这是因为vc中每次运⾏程序地址都不会改变,当我们从fun函数退出进⼊赋值语句中,就会将原本存储数据的地址扰乱,继⽽变成了随机值尽管我们引⽤返回能够将数据正确打印,但是该对象已经死亡,这是不安全的,所以我们⼀定不要以引⽤返回对象VS2019具有⼀个特点:当我们调⽤函数,若函数中没有定义局部变量或局部对象时,该函数基本不对栈帧进⾏清扫总结到此这篇关于C语⾔运算符的重载详解的⽂章就介绍到这了,更多相关C语⾔运算符重载内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验十六运算符重载
一、实验目的
1、理解运算符重载作用和意义;
2、掌握类运算符和友元运算符重载的定义和使用;
3、掌握常用运算符(++、--、+、-、*、/、=、+=、-=、*=、/=、<<(插入)、>>(提取))的重载。
二、实验内容
1、下列程序定义了一个复数类,重载"+"运算符以实现复数的加法运算。
#include<iostream.h>
class Complex{
float Real,Image; //复数的实部和虚部
public:
Complex(float r=0,float i=0) //初始化对象
{ Real=r,Image=i; }
float& AccessR() //存取实部
{ return Real; }
float& AccessI() //存取虚部
{ return Image; }
void Show() //显示复数
{ cout<<Real;
if(Image>=0) cout<<"\t+"; else cout<<"\t";
cout<<Image<<"i\n";
}
Complex operator+(Complex&); //重载"+",实现:复数+复数
Complex operator+(float); //重载"+",实现:复数+实数
Complex& operator+=(Complex&); //重载"+=",实现:复数+=复数
Complex& operator=(Complex&); //重载"="
};
Complex Complex::operator+(Complex&c)//问1.返回值类型能否为Complex&,为什么?
{ Complex t;
t.Real =Real //当前对象之数据成员,即第一操作数之数据成员+c.Real; //第二操作数之数据成员
t.Image =Image+c.Image;
return t;
}
Complex Complex::operator+(float s)
{ Complex t;
t.Real =Real+s;
t.Image =Image;
return t;
}
Complex& Complex::operator+=(Complex&c)//问2.返回值类型为void有何不足?
{ Real+=c.Real;
Image+=c.Image;
return *this;
}
Complex& Complex::operator=(Complex&c) //问3.此函数可否省略,为什么?
{ Real=c.Real;
Image=c.Image;
return *this;
}
void main(void)
{ Complex c1(25,50),c2,c3(100,200),c4(200,400),c;
c1.Show();
c2=c1; //等价于c2.operator=(c1);
c2.Show();
c=c1+c3; c.Show();
c+=c1; c.Show(); //问4.可否实现c+=20;?若不能如何改进类Complex?
c4+=c1+c2; c4.Show();
c4=c4+200; c4.Show(); //问5.可否实现c4=200+c4;?若不能如何改进类Complex?
}
首先回答上述程序中的问题,然后为上述程序中的复数类定义比较完善的操作(至少包括:+、-、*、/、取负、+=、!=、==),并作必要的测试。
2、定义描述平面上一个点的类Point,重载++和—运算符,并区分这两个运算符的前置和后置运算。
并作必要的测试。
三、选做题
定义分数类,使其具有完善的操作。
要求:①分数的分子和分母采用int型②分数的运算结果应为最简分数。