运算符重载综合实例

合集下载

运算符重载的案例

运算符重载的案例
p1=p2/p3;
cout<<p1.x<<","<<p1.y<<endl;
p1=p2++;//验证单目运算符重载
cout<<p1.x<<","<<p1.y<<endl;
bool p4;//验证关系运算符
p4=(p2==p3);
printf("%d",p4);
p4=(p2>p3);
point operator -(const point &p);//声明运算符重载-
point operator *(const point &p);//声明运算符重载*
point operator /(const point &p);//声明运算符重载/
//关系运算符重载
bool operator ==(const point &p);//声明运算符重载==
{
point temp;
temp.x=x/p.x;
temp.y=y/p.y;
return temp;
}
bool point::operator ==(const point &p)//==运算符重载
{
if(x==p.x&&y==p.y)
return true;
else
bool operator > (const point &p);//声明运算符重载>
//赋值运算符重载
point &operator =(const point &p);//声明赋值运算符重载=

运算符重载

运算符重载

void complex::print( ) { if (imag==0) cout<<real<<endl; else if(imag>0) cout<<real<<"+"<<imag<<"i\n"; else cout<<real<<imag<<"i\n"; } complex operator -(complex obj) { complex temp; temp. real= -obj.real ; temp. imag= -obj.imag; return temp; }
10
void main( ) { complex com1(2.3,4.6),com2(3.6,2.8),result; cout<<"complex com1 and com2 orderly:\n"; com1.print( ); com2.print( ); result=com1+com2; cout<<"com1+com2="; result.print( ); result=com1-com2; cout<<"com1-com2="; result.print( ); result=com1*com2; cout<<"com1*com2="; result.print( ); result=com1/com2; cout<<"com1/com2="; result.print( ); }
2、双目运算符的重载

第14课 运算符重载

第14课 运算符重载

第14课运算符重载一、运算符重载的概念1、概念一符多用。

通过重载,对运算符赋予新的含义。

如“+”号,通过重载(C++系统完成),既可以进行整数运算,也可以进行浮点数运算。

2、实例:复数的加法(14_001.cpp)3、上例:对“+”运算符进行重载(14_002.cpp)-------------------------------------------------------------------------------二、运算符重载的规则1、不允许自己定义新的运算符2、不能重载的运算符(1). (成员访问运算符)(2)* (成员指针访问运算符)(3):: (域运算符)(4)sizeof (长度运算符)(5)?: (条件运算符)3、不能改变运算对象的个数4、不能改变运算符的运算级别5、不能改变结合性6、重载运算符的函数不能有默认的参数7、至少有一个参数是自定义的类对象(不能全是C++的标准类型)8、用于类对象的运算符必须重载,有两个例外:“=”“&”9、重载功能应该类似于标准的功能------------------------------------------------------------------------------- 三、运算符重载函数作为友元函数实例:复数相加(14_003.cpp)说明:1、在VC++ 6.0中运行此程序,应修改头两行,见程序。

2、由于友元函数会破坏类的封装性,所以应尽量避免使用。

四、重载双目运算符(有两个操作数,如“+”)1、实例:重载字符串比较运算符“==”、“>”、“<”、“!=”说明:C++的字符串类型,就是对以上的运算符进行了重载,方便了字符串的比较 2、程序(14_004.cpp):以“==”运算符重载为例注意:如果将重载函数定义为友元函数,则程序开头两句应改为:#include <iostream.h>3、重载“+”运算符,对类对象进行相加运算(14_exam.cpp)4、重载“<”运算符,对类对象进行比较(14_011.cpp)5、重载“<”运算符,判断点在不在背景空白处(14_012.cpp)-------------------------------------------------------------------------------五、重载单目运算符1、实例:重载运算符“++”(14_005.cpp)说明:第二个重载函数,多了一个“int”参数,无实际意义,只是与第一个函数区别开来。

实验9 运算符重载(2)

实验9 运算符重载(2)

实验9 运算符重载(1)一、实验目的1、掌握运算符重载的概念;二、实验内容1、用成员函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算。

要求:(1)输出结果是最简分数(可以是带分数);(2)分母为1,只输出分子。

过程分析:1) 定义一个类Complex,在公共部分定义构造函数,输出函数,和运算符=、+、-、*、/的重载函数,此处运算符=可以使用系统默认的运算符=的函数。

在私有部分定义两个数据成员x和y。

2) 定义构造函数时先在类里面声明构造函数,并对参数初始化,再在类外定义构造函数,分别给x和y初始化。

3) 定义输出函数print(),对分数进行化简,采用的方法是利用for循环,分子分母均除以i,i从2增加到分子分母中更小的一个数后截止,每次增加1,在利用if else 语句,如果分子分母除以i均被整除,则说明这是的i是分子分母的公约数,分子分母均赋值为整除后的结果值,同时将i重新赋值为2,因为再求公约数是要再从i=2开始循环;否则i++,表示及进入下一个循环。

化成最简形式后在利用if else语句,判断最终结果值的分母是否为1,如果不是,则输出分数,否则只输出分子。

4) 定义运算符+重载函数,参数作为+的右操作对象,调用函数的对象作为左操作对象,在函数里定义一个Complex对象d,将左右操作对象的分数相加,将得到的结果的分子和分母分别赋给d的分子和分母,返回类的对象d。

5) 定义运算符-重载函数,参数作为-的右操作对象,调用函数的对象作为左操作对象,在函数里定义一个Complex对象d,将左右操作对象的分数相减,并将得到的结果的分子和分母分别赋给d的分子和分母,返回类的对象d。

6) 定义运算符*重载函数,参数作为*的右操作对象,调用函数的对象作为左操作对象,在函数里定义一个Complex对象d,将左右操作对象的分数相乘,并将得到的结果的分子和分母分别赋给d的分子和分母,返回类的对象d。

6运算符重载

6运算符重载
北华航天工业学院计算机系 安志远 2004
6.1.2 运算符重载的语法形式
➢ 运算符通常是对类中的私有成员进行操作,故重载运算符应 能访问类中的私有成员,所以重载运算符一般采用成员函数或 友员函数的形式。 ➢ 成员函数的语法形式为:
类型 类名 :: operator op ( 参数表 )
{
// 相对于该类定义的操作
else
{ cout << "\n Data overflow !" << endl ;
Hale Waihona Puke #include<iostream.h> #include<stdlib.h> class Calculator { public:
Calculator() { value = 0 ; } ; void operator ++ () ; void operator -- () ; unsigned int operator() () ; private: unsigned int value; }; void main()
第6章 运算符重载
运算符重载使得用户自定义的数据以一种更简洁的方式工作,
就是赋予已有的运算符多重含义。
能表示为
例如
? c1 = c1 + c2 ;
int x , y ; y=x+y;
complex c1 , c2 ; c1 = Cadd (c1 , c2 ) ;
定能义表示为
? // 运复m算数1类符=对重m象载1 +函m数2 ;
{
// 相对于该类定义的操作
} ➢ 一个运算符被重载后,原有意义没有失去,只是定义了相对 一特定类的一个新运算符

C#运算符重载用法实例分析

C#运算符重载用法实例分析
希望本文所述对大家的C#程序设计有所帮助。
这篇文章主要给大家介绍了关于c如何给枚举类型增加一个描述特性的相关资料文中通过示例代码介绍的非常详细对大家的学习或者工作具有一定的参考学习价值需要的朋友们下面来一起学习学习吧
C#运 算 符 重 载 用 法 实 例 分 析
本文实例讲述了C#运算符重载用法。分享给大家供大家参考。具体分析如下:
public class Plane { public virtual double TopSpeed() { return 300.0D;} public static bool operator>(Plane one, Plane two) { return one.TopSpeed() > two.TopSpeed(); } public static bool operator<(Plane one, Plane two) { return one.TopSpeed() < two.TopSpeed(); } } class Jet : Pe TopSpeed() { return 900.0D; } public override string ToString() { return "I'm a Jet"; } } class Airport { [STAThread] static void Main(string[] args) { Plane plane = new Jet(); Console.WriteLine("plane's top speed: {0}",plane.TopSpeed()); Jet jet = new Jet(); Console.WriteLine("jet's top speed: {0}",jet.TopSpeed()); Console.WriteLine("Plane > Jet = {0}", plane > jet); Console.ReadLine(); } }

C_运算符重载_各类详细介绍

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; ▪}

运算符重载优秀课件

运算符重载优秀课件

定义一种简化旳复数类complex: class complex { public: double real,imag; complex(double r=0,double i=0) { real=r; imag=i;}
}; 若要把类complex旳两个对象com1和com2加在一起,下面旳语句
是不能实现旳: main() { complex com1(1.1,2.2),com2(3.3,4.4),total; total=com1+com2; //错误 //… return 0; }
第7章 运算符重载
7.1为何引入运算符重载
int sum_i; float sum_f; int i1=123, i2=456; float f1=3.45, f2=56.78; sum_i= i1+i2;//两个整数相加 cout<<"i1+i2 ="<< sum_i; sum_f= f1+f2; //两个浮点数相加 cout<<"f1+f2 ="<< sum_f; sum_f= i1+i2+f1; //进行类型转换后相加 cout<<"i1+i2+f1 ="<<sum_f;
. .* :: ?Sizeof() ?:不能重载
(3)运算符重载后,不能变化运算符操作数旳
个数、优先级、结合性、语法构造。
(4)不能定义新旳运算符
7.3.1 单目运算符“++”和“--”旳重载 在C++中,能够经过在运算符函数参数表中是否插 入关键字int来区别前缀和后缀这两种方式。 例如:重载单目运算符++
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

运算符重载综合实例
class MyComplex{ double Real;double Imag;
public:
//构造函数
MyComplex(const double &r=0.0,const double &i=0.0){Real=r;Imag=i;cout<<"Constructor !"<<endl;} //拷贝构造函数
MyComplex(const MyComplex &);
double GetReal(){return Real;}
double GetImag(){return Imag;}
//赋值运算符重载为成员函数
MyComplex operator=(const MyComplex &c);
//负数运算符重载为成员函数
MyComplex operator-();
//后缀加1,成员函数
MyComplex operator++(int);
//后缀减1,外部函数
friend MyComplex operator--(MyComplex &,int);
//加法,外部函数
friend MyComplex operator+(const MyComplex &, const MyComplex &);
//减法,成员函数
MyComplex operator-(const MyComplex &);
//加赋值,成员函数
MyComplex operator+=(const MyComplex &);
//比较,外部函数和
friend int operator==(const MyComplex &, const MyComplex &);
};
MyComplex operator--( MyComplex &c,int){MyComplex result(c.Real--,c.Imag--);
cout<<"operatorpostfix--"<<endl;return result;}
MyComplex operator+(const MyComplex &c1, const MyComplex &c2){
MyComplex result(c1.Real+c2.Real,c1.Imag+c2.Imag);
cout<<"operator+"<<endl;
return result;
}
int operator==(const MyComplex &c1, const MyComplex &c2){
cout<<"operator=="<<endl;
return (c1.Real==c2.Real)&&
(c1.Imag==c2.Imag);
}
MyComplex::MyComplex(const MyComplex &c){
Real=c.Real;Imag=c.Imag;cout<<"Copy Constructor !"<<endl;}
MyComplex MyComplex::operator=(const MyComplex &c){
Real=c.Real;Imag=c.Imag; cout<<"operator="<<endl;return *this;} MyComplex MyComplex::operator-(){Real=-Real;Imag=-Imag;
cout<<"operatorunary-"<<endl;return *this;}
MyComplex MyComplex::operator++(int){MyComplex result(Real++,Imag++);
cout<<"operatorpostfix++"<<endl; return result;}
MyComplex MyComplex::operator-(const MyComplex &c){
Real-=c.Real; Imag-=c.Imag;cout<<"operatorbinary-"<<endl;return *this;} MyComplex MyComplex::operator+=(const MyComplex &c){
Real+=c.Real; Imag+=c.Imag; cout<<"operator+="<<endl; return *this;}
void main(){
MyComplex a(10,20),b(11,21),e,*p;
MyComplex c(a);
MyComplex d=b;
d+=c++;
e=((a+b)-(c--))+(-d);
p=new MyComplex(21,22);
if(!p) return;
e+=(d==(*p));
if(p) delete p;
cout<<a.GetReal()<<"+j"<<a.GetImag()<<endl;
cout<<b.GetReal()<<"+j"<<b.GetImag()<<endl;
cout<<c.GetReal()<<"+j"<<c.GetImag()<<endl;
cout<<d.GetReal()<<"+j"<<d.GetImag()<<endl;
cout<<e.GetReal()<<"+j"<<e.GetImag()<<endl;
}
Constructor !
Constructor !
Constructor !
Copy Constructor !
Copy Constructor !
Constructor !
operatorpostfix++
Copy Constructor !
operator+=
Copy Constructor !
operatorunary-
Copy Constructor !
Constructor !
operatorpostfix--
Copy Constructor !
Constructor !
operator+
Copy Constructor ! operatorbinary- Copy Constructor ! Constructor ! operator+
Copy Constructor ! operator=
Copy Constructor ! Constructor ! operator== Constructor ! operator+=
Copy Constructor ! 10+j20
11+j21
10+j20
-21+j-41
-11+j-21。

相关文档
最新文档