运算符重载实验8

运算符重载实验8
运算符重载实验8

《面向对象程序设计》实验七运算符重载(电信系教师陈小常)

一、实验目的

1、掌握用成员函数重载运算符的方法

2、掌握用友元函数重载运算符的方法

二、实验内容

1.重载自增运算符

修改下面程序中的错误,添加成员函数的定义重载自增运算符,并调试输出运行结果。#include

using namespace std;

class counter

{

public;

Counter() {v=0;}

Counter opretor ++()

Counter opretor ++(int)

void print() {cout<

private:

unsingned v;

};

int main()

{

Counter c;

c.print();

(c++).print();

c.print();

++c;

c.print();

}

//修改下面程序中的错误,添加成员函数的定义重载自增运算符,并调试输出运行结果。#include

using namespace std;

class counter

{

public:

counter() {v=0;}

counter operator ++()//前缀

{

++v;

return *this;

}

counter operator ++(int)//后缀

{

counter te(*this);

v++;

return te;

}

void print()

{cout<

private:

unsigned v;

};

int main()

{

counter c;

c.print();

(c++).print();

c.print();

++c;

c.print();

return 0;

}

2.、重载加、赋值运算符

已知Mydatatype类的定义如下,添加成员函数的定义重载加或者赋值运算符,并调试输出运行结果:

# include

class mydatatype

{

protected:

int mndata;

public:

mydatatype();

mydatatype & operator=(int);

mydatatype & operator=(mydatatype & );

mydatatype & operator+(mydatatype & );

operator int()

{

return mndata;

}

};

int main()

{

mydatatype obj1,obj2,obj3;

obj1=10;

obj2=obj1;

obj3=obj1+obj2;

int nvalue=(int)obj3;

cout<<"nvalue="<

return 0;

}

//已知Mydatatype类的定义如下,添加成员函数的定义重载加或者赋值运算符,并调试输出运行结果:

# include

class mydatatype

{

protected:

int mndata;

public:

mydatatype();

mydatatype & operator=(int);

mydatatype & operator=(mydatatype & );

mydatatype& operator+(mydatatype & );

operator int()

{

return mndata;

}

void print()

{

cout<

}

};

mydatatype::mydatatype()

{

mndata=0;

}

mydatatype& mydatatype::operator=(int a)

{

mndata=a;

return *this;

}

mydatatype& mydatatype::operator=(mydatatype & p)

{

mndata=p.mndata;

return *this;

}

mydatatype& mydatatype::operator+(mydatatype & m)

{

mndata=mndata+m.mndata;

return *this;

}

int main()

{

mydatatype obj1,obj2,obj3;

obj1=10;

//obj1.print();

obj2=obj1;

//obj2.print();

obj3=obj1+obj2;

int nvalue=(int)obj3;

cout<<"nvalue="<

return 0;

}

3、建立类StrType,允许下面的操作符类型:

(1) 使用+操作符的字符串连接;

(2) 使用=操作符的字符串赋值;

(3) 使用<、>==操作符的字符串比较。

可以自由使用定长字符串。

修改下面程序中的错误,添加成员函数定义重载+、=、<、>、==运算符,并调试输出运行结果。

#include < iostream.h>

#include < string.h>

class StrType

{

public:

StrType(){ *s='\0';)

StrType(char * p){ strcpy(s,p);}

char * Get(){return s;}

StrType oprator + (StrType& s2);

StrType& oprator = (strType& s2);

int operator < (StrType& s2);

int operator > (StrType& s2);

int operator == (StrType& s2);

private:

char s[80];

}

void main()

{

StrType o1("Hollo"),o2("There"),o3;

cout << "o1= " << o1.Get() << endl;

cout << "o2= " << o2.Get() << endl;

o3 = o1 + o2;

cout << "o3= o1+o2= " << o3.Get() << endl;

o3 = o1;

cout << "o1 = o3 \ n";

if(o1==o3)

cout << "o1 equals o3 \ n";

if(o1>o2)

cout << "o1 > o2 \ n";

if(o1

cout << "o1 < o2 \ n";

}

/*3、建立类StrType,允许下面的操作符类型:

(1) 使用+操作符的字符串连接;

(2) 使用=操作符的字符串赋值;

(3) 使用<、>==操作符的字符串比较。

可以自由使用定长字符串。

修改下面程序中的错误,添加成员函数定义重载+、=、<、>、==运算符,并调试输出运行结果。*/

#include < iostream.h>

#include < string.h>

class StrType

{

public:

StrType()

{ *s='\0';}

StrType(char *p)

{ strcpy(s,p);}

char *Get()

{return s;}

StrType operator+(StrType& s2);

StrType& operator=(StrType& s2);

friend int operator<(StrType& s2,StrType& s3);

friend int operator>(StrType& s2,StrType& s3);

friend int operator==(StrType& s2,StrType& s3);

private:

char s[80];

};

StrType StrType:: operator +(StrType& s2) {

StrType str;

strcpy(str.s,s);

strcat(str.s,s2.s);

return str;

}

StrType& StrType::operator=(StrType &s2) {

strcpy(s,s2.s);

return *this;

}

int operator<(StrType& s2,StrType& s3) {

if(strcmp(s2.s,s3.s)<0)

return 1;

else

return 0;

}

int operator > (StrType& s2,StrType& s3) {

if(strcmp(s2.s,s3.s)>0)

return 1;

else

return 0;

}

int operator==(StrType& s2,StrType& s3) {

if(strcmp(s2.s,s3.s)==0)

return 1;

else return 0;

}

void main()

{

StrType o1("Hollo"),o2("There"),o3;

cout << "o1= " << o1.Get() << endl;

cout << "o2= " << o2.Get() << endl;

o3 = o1 + o2;

cout << "o3= o1+o2= " << o3.Get() << endl;

o3 = o1;

cout << "o1 = o3 \ n";

if(o1==o3)

cout << "o1 equals o3 \ n";

if(o1>o2)

cout << "o1 > o2 \ n";

if(o1

cout << "o1 < o2 \ n";

}

void main()

{

StrType o1("Hollo"),o2(" boy and girl"),o3;

cout<< "o1= " << o1.Get() << endl;

cout<< "o2= " << o2.Get() << endl;

o3 = (o1 + o2);

cout<<"o3= o1+o2= "<

o3 = o1;

cout<< "o1 = o3 \n";

if(o1==o3)

cout<< "o1 equals o3 \n";

if(o1>o2)

cout<< "o1 > o2 \n";

if(o1

cout<< "o1 < o2 \n";

}

实验8--友元函数与运算符重载函数

实验十三 1.实验目的 通过本次实验 (1)理解友元函数与运算符重载函数的概念; (2)学会友元函数的定义与使用方法; (3)掌握运算符重载函数的定义与使用方法; 2.实验要求 (1)编写实验程序 (2)在VC++运行环境中,输入源程序 (3)编译运行源程序 (4)输入测试数据进行程序测试; (5)写出运行结果。 3.实验内容 (1)定义一个复数类,重载“-=”运算符,使这个运算符能直接完成复数的“-=”运算。分别用成员函数与友元函数编写运算符重载函数。在主函数中定义复数对象c1(10,20)、c2(15,30),进行c2-=c1的复数运算,并输出c1、c2的复数值。 参考资料: (1)成员函数 # include class Complex { private: float Real,Image; public: Complex(float r=0,float i=0) { Real=r;Image=i;} void Show(int i) { cout<<"c"< class Complex { private: float Real,Image; public: Complex(float r=0,float i=0) { Real=r;Image=i;} void Show(int i) { cout<<"c"<

实验五 运算符重载

实验五运算符重载 【实验目的】 1.进一步了解运算符重载的概念和使用方法。 2.掌握几种常用的运算符重载的方法。 3.了解转换构造函数的使用方法。 4.了解在Visual C++6.0环境下进行运算符重载要注意的问题. 【实验要求】 1.硬件基本配置:Intel PentiumIII以上级别的CPU,大于64MB的内存。 2.软件要求:Window 2000操作系统,Visual Studio 6.0或更高版本开发环境。 3.实验学时:2学时 4.实现实验内容中的题目。 5.写实验报告 【实验内容】 (1)声明一个复数类Complex,重载运算符“+’’,“一”,“*”,“/”使之能用于复数的加、减、乘、除.运算符重载函数作为Complex类的成员函数,重载流提取运算符实现对复数的输出。编程序,分别求两个复数之和、差、积和商并用重载后的流运算符进行输出。 请思考:你编的程序能否用于一个整数与一个复数的算术运算?如4+ (5-2i). (2)声明一个复数类Complex,重载运算符“+“,使之能用于复数的加法运算。 参加运算的两个运算量可以都是类对象.也可以其中有一个是整数,顺序任意。例如, cl+c2, i+cl,cl+i均合法(设i为整数,c1, c2为复数)。 运行程序.分别求两个复数之和、整数和复数之和。 (3)有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如c=a+b。重载流提取运算符实现对矩阵的输出如:cout<

运算符重载练习题.

运算符重载 一.单项选择题 1.下列运算符中,运算符在C++中不能重载。 A.?: B.+ C. D.<= 解:C++中不能被重载的运算符有:·,一,::,?:。本题答案为A。 2.下列运算符中,运算符在C++中不能重载。 A.&& B.[] C.:: D.new 解:c++中不能被重载的运算符有:·,·+,::,?:。本题答案为c。 3.下列关于运算符重载的描述中,是正确的。 A.运算符重载可以改变操作数的个数 B.运算符重载可以改变优先级 C.运算符重载可以改变结合性 D.运算符重载不可以改变语法结构 解:运算符重载不能改变操作数的个数、运算符的优先级、运算符的结合性和运算程的语法结构。本题答案为D。 4.友元运算符objl>obj2被C++编译器解释为。 A.operator>(objl,obj2) B.>(obj1,obj2) C.obj2.operator:>(obj1) D.objl.operator>(obj2) 解:重载为友元函数的运算符的调用形式如下: operator<运算符>(<参数1>,<参数2>) 等价于:<参数1><运算符><参数2> 本题答案为A。 5.现需要对list类对象使用的逻辑运算符“==”重载,以下函数声明是正确的。 A、list & list::operator==(const list &a); B、list list::operator==(const list &a); C、bool & list::operator==(const list &a); D、bool list::operator==(const list &a); 6. 以下类中分别说明了“+=”和“++”运算符重载函数的原型。如果主函数中有定义: fun m,c,d;,那么,执行语句c=m++; 时,编译器把m++解释为: (33) A) c.operator++(m); B) m=operator++(m); C) m.operator++(m); D) operator++(m); class fun { public: .. .. .. fun operator +=(fun ); friend fun operator ++(fun &,int); }; 答案:D 7. 在第33题中,当执行语句d+=m; 时,C++编译器对语句作如下解释: (34) A. d=operator+=(m); B. m=operator+=(d); C. d.operator+=(m); D. m.operator+=(d); 答案:C 8. 设有以下类定义,其中说明了“+”运算符重载函数的原型。这是一个友元函数,当类

实验十_运算符重载

实验十运算符重载1.实验目的及要求 1)掌握运算符重载的基本概念和方法。 2)熟习几种特殊的运算符的重载。 2.实验内容 1.分析下面的程序,指出程序运行的结果: 1) #include class point{ int x,y; public: point(int vx,int vy){ x=vx; y=vy;} point(){x=0,y=0;} point operator+(point p1) { int px=x+p1.x; int py=y+p1.y; return point(px,py); } point operator-(point p1) { point p; int px=x-p1.x; int py=y-p1.y; return point(px,py); } void print(){cout<

运行结果为: 30,30 Press any key to continue 2)分析下面程序,指出程序运行的结果: #include static int dys[]={31,28,31,30,31,30,31,31,30,31,30,31}; class date { int mo,da,yr; public: date(int m,int d,int y){mo=m;da=d;yr=y;} date(){} void disp() {cout<dys[dt.mo-1]) {day-=dys[dt.mo-1];//cout<

国家二级C++机试(运算符重载、模板和C++流)模拟试卷7

国家二级C++机试(运算符重载、模板和C++流)模拟试卷7 (总分:58.00,做题时间:90分钟) 一、选择题(总题数:29,分数:58.00) 1.下列关于函数模板的描述中,正确的是( )。 (分数:2.00) A.函数模板是一个实例函数 B.使用函数模板定义的函数没有返回类型 C.函数模板的类型参数与函数的参数相同 D.通过使用不同的类型参数,可以从函数模板得到不同的实例函数√ 解析:解析:函数模板是一系列相关函数的模型或样板,这些函数的源代码相同,只是所针对的数据类型不同。数据类型成了函数模板的参数,所以函数模板是一种参数化类型的函数。 2.有如下函数模板定义: template<typename T1,Typename T2> T1 Fun(T2 n){ return n*5.0;} 若要求以int型数据9作为函数实参调用该模板,并返回一个double型数据,则该调用应表示为( )。 (分数:2.00) A.FUN(9) B.FUN<9> C.FUN<double>[9] √ D.FUN<9>(double) 解析:解析:根据函数模板的定义,在选项C的调用中,把double类型传递给T1,int型传递给T2。 3.下列关于模板的描述中,错误的是( )。 (分数:2.00) A.类模板的成员函数都是模板函数 B.函数模板是一种参数化类型的函数 C.满足一定条件时可以省略模板实参 D.模板形参只能由关键字typename声明√ 解析:解析:同一模板的声明和定义中,模板形参的名字不必相同。每个模板类型形参前面必须带上关键字typename/class,每个非类型形参前面必须带上类型名字。 4.已知主函数中通过如下语句序列实现对函数模板swap的调用: int a[10],b[10]; swap(a,b,10);下列对函数模板swap的声明中,会导致上述语句序列发生编译错误的是( )。 (分数:2.00) A.template<typename T> void swap(T a[],T b[],int size); B.template<typename T> void swap(int size,T a[],T b[]);√ C.template<typename T1,typename T2> void swap(T1 a[],T2 b[],int size}; D.template<class T1,class T2> void swap(T1 a[],T2 b[],int size); 解析:解析:由题目中函数swap(a,b,10)调用语句可知,在对函数模板swap的声明语句中,应将第一、二个参数设为数组变量,第三个参数为整型变量。 5.在定义函数模板或类模板时,开头的保留字是( )。 (分数:2.00) A.typename B.template √ C.class D.typedef 解析:解析:定义函数模板或类模板时,开头的保留字是template。 6.若有函数模板mySwap和一些变量定义如下:( )。template<class T>void mySwap(T x,T y);double d1,d2;int i1,i2;下列对mySwap的调用中,错误的是 (分数:2.00) A.mySwap(i1,i2)

实验5-运算符重载、继承 ( 1 )

实验五运算符重载、继承 ●实验目的 1、了解类的两种使用方式。 2、学习从现有类派生出新类的方式。 3、了解在派生类中如何使用基类的成员。 4、了解基类成员在派生类中的访问控制。 5、掌握运算符重载的方法。 ●实验内容 1、从类Person中派生出一个教师类,新增的属性有专业(Specialty)、职称(Position)和主讲课程(MajorCourse,一门),并为这些属性定义相应的方法。 [实现要求] Person类的设计如下,请将空白处补充完整。 class Person { string Name; int Age; string Sex; public: void Register(char *name,int age,char *sex) { ; Age=age; Sex=sex; } string GetName() { } string GetSex() { return Sex; } int GetAge() { } void ShowMe() { cout<

//带参数的构造函数去初始化各个成员的值,使其显示运行结果的第一行t.TeacherRegister("张三",40, "f","计算机","副教授","C++"); t.ShowMe(); return 0; } 运行结果如下: XXX m 0 XXX XXX XXX 张三 f 40 计算机副教授C++ [思考问题] ①在Teacher类中是否要需要重新定义ShowMe成员函数?不重新定义ShowMe成员函数能否得到上述要求的运行结果?为什么? 2、从Point类中派生出一个Line类。Line类增加一个数据成员EndPoint,计算线的长度。 [实现提示] Point类可以按下面方式进行设计: class Point{ int x,y; public: Point(int a=0,int b=0){SetPoint(a,b);} void SetPoint(int a,int b); //设置点的坐标值 int GetX(){return x;} int GetY(){return y;} void Print(); //显示点的坐标值 }; Line类增加一个数据成员EndPoint为Point类对象,注意在设计Line类的构造函数时应为其基类和对象成员EndPoint提供形参。 为了检验Line类的功能,主函数按如下方式设计: int main(){ Line line(1,1,10,10); cout<<"Line line:"; line.Print(); cout<<"\n线line的长度:"; cout<

Student类(友元,运算符重载,继承)综合题

//定义Student类,Date类,类定义与成员函数,实现分离。 //将Student类声明为Date的友元类。Student类需要提前声明。 //Student类的成员函数可以访问Date类的私有成员。 //成员函数:构造函数,构造函数重载,析构函数, //输出函数,求成绩最低者函数,排序函数。 //使用new,delete,动态分配内存。实现班级人数不固定,可以从键盘输入。 //定义Doctor类(研究生)-公共继承于Student类 //增加私有成员:string thesis(论文评价),int sci_value(科研分值) //增加相应的成员函数 //增加友元函数-运算符重载<<,>> //增加成员函数--按照科研分值高低排序 //student.h---头函数,类的定义 #include #include using namespace std; class Student; //提前声明 class Date //定义Date类-存放出生年月日 { friend class Student; //友元类,Student成员函数可以访问Date类的私有成员public: Date(); //无参构造函数 Date(int,int,int); //有参构造函数 ~Date(); //析构函数 //protected: //这里是保护成员//这里是VC6.0的原因 int year; //年 int month; //月 int day; //日 }; class Student //定义Student类-存放学生信息 { public: Student(); //无参构造函数 Student(string ,string ,char,Date,int); //有参构造函数 ~Student(); //析构函数 void display(); //输出函数 void input(); //输入函数 void min(Student*,int); //求最低成绩函数 void sort(Student*,int); //按照成绩高低排序函数 //protected: //这里是保护成员//这里是VC6.0的原因string num; //学号 string name; //姓名 char sex; //性别

实验十六运算符重载解读

实验十六运算符重载 一、实验目的 1、理解运算符重载作用和意义; 2、掌握类运算符和友元运算符重载的定义和使用; 3、掌握常用运算符(++、--、+、-、*、/、=、+=、-=、*=、/=、<<(插入)、>>(提取))的重载。 二、实验内容 1、下列程序定义了一个复数类,重载"+"运算符以实现复数的加法运算。 #include 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<=0) cout<<"\t+"; else cout<<"\t"; cout<

关系运算符重载实例2015

#include //using namespace std; class Date { private: int year; int month; int day; public: Date(){} Date(int y,int m,int d); void display(); friend bool operator >(Date &d1,Date &d2); friend bool operator <(Date &d1,Date &d2); friend bool operator ==(Date &d1,Date &d2); }; Date::Date(int y,int m,int d) { this->year=y; this->month=m; this->day=d; } void Date::display() { cout<year<<"-"<month<<"-"<day; } bool operator >(Date &d1,Date &d2) { if(d1.year>d2.year) return true; else if(d1.year==d2.year) { if(d1.month>d2.month) return true; else if(d1.month==d2.month) { if(d1.day>d2.day) return true; else return false; } else return false; } else

return false; } bool operator <(Date &d1,Date &d2) { if(d1.yeard2) cout<<"大于"; if(d1

实验5 运算符重载

实验5 运算符重载 1.实验目的 通过本次实验 (1)理解运算符重载函数的概念; (2)掌握运算符重载函数的定义与使用方法; 2.实验要求 (1)编写实验程序 (2)在运行环境中,输入源程序 (3)编译运行源程序 (4)输入测试数据进行程序测试; (5)写出运行结果。 3.实验内容 (1)定义一个复数类,重载“-=”运算符,使这个运算符能直接完成复数的“-=”运算。分别用成员函数与友元函数编写运算符重载函数。在主函数中定义复数对象c1(10,20)、c2(15,30),进行c2-=c1的复数运算,并输出c1、c2的复数值。 (2)定义一个数组类Array,其私有数据成员为整型一维数组a[10]。通过构造函数给a[10]赋初值。用Show函数显示a[10]的元素值。用成员函数重载运算符“+”,直接实现两个一维数组对应元素相加的运算。在主函数中定义数组a、b分别为: int a[10]={1,2,3,4,5,6,7,8,9,10}; int b[10]={4,5,6,7,8,9,10,11,12,13}; 用Array定义三个数组对象arr1(a)、arr2(b)、arr3,执行arr3=arr1+arr2运算,输出arr3的数组元素值。 (3)定义一个人民币类Money,类中数据成员为元、角、分。用成员函数与友元函数重载“――”运算符,实现人民币对象的减1运算。在主函数中定义人民币对象m1=10元8角5分及对象m2、m3。对m1作前置“――”并赋给m2。对m1作后置“――”并赋给m3。显示m1、m2、m3的结果。 (4)定义描述字符串的类String,编写字符串运算符“+=”的重载函数,使运算符“+=”用于两个字符串联接操作,即用str1+=str2实现字符串函数strcat(str1,str2)的操作功能。要求分别用成员函数与友元函数编写运算符重载函数。在主函数中定义字符串对象s1("software and ")与s2("hardware"),进行s1+=s2的字符串联接,并输出s1、s2的值。

c++运算符重载习题

Task8-1 /* 1. 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和*/ #include using namespace std; class Complex { public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} void display(); double real; double imag; }; void Complex::display() { cout<<"("<>";Time timeslot;timeslot.getslot();cout<<"New time>>"; second=second+timeslot.second;if(second>=60){second-=60;minute=minute+1; if(minute>=60){minute-=60; hour=(hour+1)%24; }} minute=minute+timeslot.minute; if(minute>=60){minute-=60; hour=(hour+1)%24;} hour=hour+timeslot.hour;if(hour>=24){hour=(hour+1)%24; } return *this; } int main(){

C++实验六运算符重载

实验六运算符重载 一、实验目的 1.掌握运算符重载的概念; 2.握使用friend重载运算符的方法; 二、实验要求 1.独立完成程序的编辑、编译、调试及运行; 2.对于程序编译及运行中出现的错误,能够进行改正; 三、实验内容 1、重载加、减、乘、除运算符,实现复数类complex的四则运算。 程序分析: 程序中对于复数的四则运算——加,减,乘,除符号进行重载,采用的是成员函数的方法: Complex operator+(const Complex &c) Complex operator-(const Complex &c) Complex operator*(const Complex &c) Complex operator/(const Complex &c) 当c1+c2时,程序将运算符(+)认为是重载的复数加法运算符,因运算符的两个操作数都是复数,被解释为:c1.operator+(c2),其中,c1为第一操作数,作为调用重载运算符成员函数的对象,c2是第二操作数,作为重载运算符函数的实参,c1和c2都是类的Complex的对象。

#include class Complex { public: Complex() {real=imag=0;} Complex(double r) {real=r;imag=0;} Complex(double r,double i) {real=r;imag=i;} Complex operator + (const Complex &c); Complex operator - (const Complex &c); Complex operator * (const Complex &c); Complex operator / (const Complex &c); friend void Print(const Complex &c); private: double real,imag; }; Complex Complex::operator +(const Complex &c) { return Complex(real+c.real,imag+c.imag); } Complex Complex::operator -(const Complex &c) { return Complex(real-c.real,imag-c.imag); } Complex Complex::operator *(const Complex &c) { return Complex(real*c.real-imag*c.imag,real*c.imag+imag*c.real);

实验5 运算符重载和多态性

实验5 运算符重载与多态性 班级学号姓名成绩 一.实验目的 1.掌握用成员函数重载运算符的方法 2.掌握用友元函数重载运算符的方法 3.理解并掌握利用虚函数实现动态多态性和编写通用程序的方法 4.掌握纯虚函数和抽象类的使用 二.实验内容 1.复数类加减法乘除运算(用成员函数定义运算符重载)。 复数类的定义: class complex //复数类声明 { public: //外部接口 complex(double r=0.0,double i=0.0) //构造函数 {real=r,imag=i;} complex operator +(complex c2); //运算符"+"重载成员函数 complex operator - (complex c2); //运算符"-"重载成员函数 complex operator *(complex c2 ); //运算符"*"重载成员函数 complex operator /(complex c2); //运算符"/"重载成员函数 complex operator =(complex c2); //运算符"="重载成员函数 void display(); //输出复数 private: //私有数据成员 double real; //复数实部 double imag; //复数虚部 }; 2.复数类比较运算(用友元函数定义运算重载)。 注意: 1)复数类比较运算按复数的模比较两个复数的大小。 2)复数相等判断当两个复数的实部和虚部都相等,两个复数才相等,否则不相等。 类的定义 class complex //复数类声明 { public: complex(double r=0.0,double i=0.0) {real=r;imag=i;} //构造函数 friend int operator> (complex c1,complex c2); //运算符">"重载友元函数 friend int operator>=(complex c1,complex c2); //运算符">="重载友元函数

相关文档
最新文档