C++运算符重载例程详解
C程序设计运算符重载资料

• 在中只能重载单目运算符、双目运算符及不确定目数 运算符“()”
4.2.1 运算符重载为类的成员函数
• 通过该类的对象来调用运算符函数,由于对象本身将作为一 个操作数,因此要求第一个操作数的类型为用户自ቤተ መጻሕፍቲ ባይዱ义类, 参数表中的参数个数比操作数个数少一。
• 运算符重载的方法在实质上就是定义一个重载运算符 的函数,在执行被重载的运算符时,系统将调用此函 数实现相应的运算。运算符重载本质就是函数的重载。
• 重载运算符的函数的原型如下:
• 返回值类型运算符(形参表);
• 例如将“+”用于类的加法运算运算符重载的函数原 型可以为:
• ( 1, 2);
例4.2 通过运算符重载实现复数相加的示例。
例4.1 通过函数实现复数相加示例。 声明复数数
{
:
数据成员
;
实部
;
虚部
:
公有函数
( r = 0, i = 0): (r), (i){ } 构造函数
() ;
输出复数
( 1, 2);
复数加法
};
()
输出复数
{
( < 0) << << << "i" << ;
虚部为负
( 0) << << ;
虚部为0
<< << “+” << << “i” << ;
z1();
象的过程中调用输了出构z1造函数,语句将此临时对象作为函
介绍C#中的运算符重载.

介绍C#中的运算符重载.介绍 C# 中的运算符重载周融,2007 年 5 ⽉(C) 2001-2007 保留所有权利。
重载是⾯向对象中的⼀个重要概念,它是对象多态性的⼀种不完全体现,⼈们通常所说的重载,往往指的是函数的重载。
本⽂向读者介绍⼀种新的重载模型——运算符重载。
在本⽂中的内容:1、为什么需要运算符重载2、C# 运算符重载决策⽰例3、C# 运算符重载⼀览表4、结论为什么需要运算符重载函数的重载为⼀个对象上的相同⾏为提供不同的参数⽅式,这样,开发⼈员便可以使⽤这些不同的参数实现类似的功能。
⼀组函数重载决策⼀般实现的功能是相同的,例如对 Object 对象上的 ToString() ⽅法就有⼏个重载版本,虽然它们接受的参数不同,但却都表达同⼀个⾏为的最终结果。
参数的不同导致函数重载版本的签名不同,这样编译器很容易知道需要调⽤那⼀个重载版本。
这种技术给开发⼈员带来了⽅便。
现在我们试图对重载的定义进⾏推⼴。
先让我们看看最简单的例⼦,我们通常需要像这样声明并初始化⼀个值类型的变量:int digit = 5;string sayHello = "Hello, World";这⾥的“=”运算符,就是将右边的值传递给左边变量的赋值运算符。
这⾥,5 的类型为 int,“Hello, World”的类型为 string,这与左边被赋值的变量类型完全⼀致。
但对于上述的解释,我们还可以这样认为:5 的类型为 uint 或 byte,"Hello, World"的类型为 char[],那么如此⼀来,赋值运算左边和右边的类型就不在等同,那么编译器如何处理呢?有⼈会说,这就是“隐式类型转换”,这个答案确实很好,但隐式类型转换的规则已经被编译器确定,如果赋值运算的两端不遵循隐式类型转换规则,则需要显式类型转换,例如:char c = '2';string s = (string)c;int i = (int)c;这些显式类型转换并不适⽤于任何场合,也许⼈们希望在其⾃定义的类中也能⽤赋值、加减乘除等语法操作它们。
C++基础系列——运算符重载

C++基础系列——运算符重载1. 运算符重载简介所谓重载,就是赋予新的含义。
函数重载(Function Overloading)可以让⼀个函数名有多种功能,在不同情况下进⾏不同的操作。
同样运算符重载(Operator Overloading)可以让同⼀个运算符可以有不同的功能。
可以对 int、float、string 等不同类型数据进⾏操作<< 既是位移运算符,⼜可以配合 cout 向控制台输出数据也可以⾃定义运算符重载:class Complex{public:Complex();Complex(double real, double imag);Complex operator+(const Complex &a) const;void display() const;private:double m_real;double m_imag;};// ...// 实现运算符重载Complex Complex::operator+(const Complex &A) const{Complex B;B.m_real = this->m_real + A.m_real;B.m_imag = this -> m_imag + A.m_imag;return B;// return Complex(this->m_real + A.m_real, this->m_imag + A.m_imag);}int main(){Complex c1(4.3, 5.8);Complex c2(2.7, 3.7);Complex c3;c3 = c1 + c2; // 运算符重载c3.display();return 0;}运算结果7 + 9.5i运算符重载其实就是定义⼀个函数,在函数体内实现想要的功能,当⽤到该运算符时,编译器会⾃动调⽤这个函数,它本质上是函数重载。
第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++基础知识之运算符重载详解⽬录运算符重载⽅式⼀, 使⽤成员函数重载运算符需求:把⽜⾁换猪⾁, ⽺⾁换猪⾁⽅式⼆, 使⽤⾮成员函数【友元函数】重载运算符两种⽅式的区别两种⽅式的选择:总结运算符重载为什么要使⽤运算符重载-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指针。
vc 04 运算符重载

于是: 于是:
string x,y; x=x+y; //表示串 和串 合并,放入串 表示串x 表示串 和串y 合并,放入串x
这就使对基本类型的预定义运算符" 的含义在串类中被扩展 这就使对基本类型的预定义运算符"+"的含义在串类中被扩展 为对串类对象的合并操作,即运算符"+"被重载. 为对串类对象的合并操作,即运算符" 被重载. 被重载 运算符重载应注意的几个问题: 运算符重载应注意的几个问题: 1,哪些运算符可以重载? 哪些运算符可以重载? 中除了下面四种运算符外, 在C++ 中除了下面四种运算符外,系统预定义的运算符都 能被重载. 能被重载.
string x,y;
那么, 实现两个串的合并. 那么,可以使用函数strcat(x,y)实现两个串的合并. 如果在表示字符串的类对象x, 的特定环境下,运算符" 使 如果在表示字符串的类对象 ,y 的特定环境下,运算符"+"使 用于类对象,能被解释为串对象x 的合并, 用于类对象,能被解释为串对象 和y 的合并,则编程就更方便 了.
例如:下边是运算符重载的另一个例子,程序实现了复数的加 例如:下边是运算符重载的另一个例子, 和减运算. 和减运算.
#include <iostream.h> class Complex { public: Complex() { real=imag=0.0; } Complex(double r) { real=r; imag=0.0; } Complex(double r,double i) { real=r; imag=i; } Complex operator+(const Complex& c); Complex operator-(const Complex& c); friend void print(const Complex& c); private: double real,imag; };
c语言 重载 赋值运算符

c语言重载赋值运算符C语言作为一门高级编程语言,提供了丰富的功能以满足各种编程需求。
其中,运算符重载就是一项非常重要的特性。
运算符重载可以让程序员自定义已有运算符在新类型上的操作方式,使得语言更加灵活和强大。
本文将详细介绍C语言中赋值运算符的重载,包括重载的原理、步骤、方法以及注意事项。
1.C语言中的重载概念C语言中的运算符重载是指在已有的运算符上,根据运算对象的类型,赋予新的操作含义。
这种重载是基于类型的,不同类型之间的运算符重载有不同的处理方式。
运算符重载可以让原有运算符在特定类型上具有更符合语义的操作方式,例如对赋值运算符的重载可以让赋值操作更加直观。
2.赋值运算符的重载原理在C语言中,赋值运算符"="原本用于将右侧的值赋给左侧的变量。
当我们对赋值运算符进行重载时,实际上是将原有赋值操作转换为一个新的表达式,这个表达式中包含了重载后的赋值操作。
重载后的赋值运算符需要满足以下条件:- 重载后的赋值运算符仍为一个二元运算符。
- 重载后的赋值运算符的优先级和结合性与其他运算符保持一致。
- 重载后的赋值运算符需要考虑运算对象的类型,以实现正确的赋值操作。
3.重载赋值运算符的步骤与方法重载赋值运算符的步骤如下:- 定义一个函数,该函数的参数列表中包含一个或多个变量引用。
- 在函数体中,对传入的变量进行操作,以实现重载后的赋值操作。
- 使用函数返回值替换原赋值表达式中的右侧值。
以下是一个重载赋值运算符的示例:```c#include <iostream>class MyClass {public:void operator=(const MyClass& other) {// 实现重载后的赋值操作std::cout << "重载赋值运算符被调用" << std::endl;}};int main() {MyClass obj1;MyClass obj2;obj1 = obj2; // 调用重载后的赋值运算符return 0;}```4.重载赋值运算符的注意事项- 重载赋值运算符时,需要确保运算对象具有可赋值性。
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(); } }
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
运算符重载就是对一个已有的运算符赋予新的含义,使之实现新功能。
下面将通过简单的几个例程阐叙其用法。
例10.1 通过函数来实现复数相加。
#include <iostream>using namespace std;class Complex //定义Complex类{public:Complex( ){real=0;imag=0;} //定义构造函数Complex(double r,double i){real=r;imag=i;} //构造函数重载Complex complex_add(Complex &c2); //声明复数相加函数void display( ); //声明输出函数private:double real; //实部double imag; //虚部};Complex Complex::complex_add(Complex &c2){Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;}void Complex::display( ) //定义输出函数{cout<<″(″<<real<<″,″<<imag<<″i)″<<endl;}int main( ){Complex c1(3,4),c2(5,-10),c3; //定义3个复数对象c3=plex_add(c2); //调用复数相加函数cout<<″c1=″; c1.display( ); //输出c1的值cout<<″c2=″; c2.display( ); //输出c2的值cout<<″c1+c2=″; c3.display( ); //输出c3的值return 0;}例10.2 改写例10.1,重载运算符“+”,使之能用于两个复数相加。
#include <iostream>using namespace std;class Complex{public:Complex( ){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}Complex operator+(Complex &c2); //声明重载运算符的函数void display( );private:double real;double imag;};Complex Complex::operator+(Complex &c2) //定义重载运算符的函数{ Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;}void Complex∷display( ){ cout<<″(″<<real<<″,″<<imag<<″i)″<<endl;}int main( ){ Complex c1(3,4),c2(5,-10),c3;c3=c1+c2; //运算符+用于复数运算cout<<″c1=″;c1.display( );cout<<″c2=″;c2.display( );cout<<″c1+c2=″;c3.display( );return 0;}例10.3 将运算符“+”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为Complex类的友元函数。
#include <iostream>using namespace std;class Complex{public:Complex( ){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}friend Complex operator + (Complex &c1,Complex &c2);//重载函数作为友元函数void display( );private:double real;double imag;};Complex operator + (Complex &c1,Complex &c2)//定义作为友元函数的重载函数{return Complex(c1.real+c2.real, c1.imag+c2.imag);}//简洁的写法void Comple::display( ){cout<<″(″<<real<<″,″<<imag<<″i)″<<endl;}int main( ){Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<″c1=″; c1.display( );cout<<″c2=″; c2.display( );cout<<″c1+c2 =″; c3.display( );}例10.4 定义一个字符串类String,用来存放不定长的字符串,重载运算符“==”,“<”和“>”,用于两个字符串的等于、小于和大于的比较运算。
为了使读者便于理解程序,同时也使读者了解建立程序的步骤,下面分几步来介绍编程过程。
(1) 先建立一个String类:#include <iostream>using namespace std;class String{public:String( ){p=NULL;} //默认构造函数String(char *str); //构造函数void display( );private:char *p; //字符型指针,用于指向字符串};String∷String(char *str) //定义构造函数{p=str;} //使p指向实参字符串void String∷display( ) //输出p所指向的字符串{cout<<p;}int main( ){String string1(″Hello″),string2(″Book″);string1.display( );cout<<endl;string2.display( );return 0;}(2) 有了这个基础后,再增加其他必要的内容。
现在增加对运算符重载的部分。
为便于编写和调试,先重载一个运算符“>”。
程序如下:#include <iostream>#include <string>using namespace std;class String{public:String( ){p=NULL;}String(char *str);friend bool operator>(String &string1,String &string2);//声明运算符函数为友元函数void display( );private:char *p; //字符型指针,用于指向字符串};String∷String(char *str){p=str;}void String∷display( ) //输出p所指向的字符串{cout<<p;}bool operator>(String &string1,String &string2) //定义运算符重载函数{if(strcmp(string1.p,string2.p)>0)return true;else return false;}int main( ){String string1(″Hello″),string2(″Book″);cout<<(string1>string2)<<endl;}(3) 扩展到对3个运算符重载。
在String类体中声明3个成员函数:friend bool operator> (String &string1, String &string2);friend bool operator< (String &string1, String &string2);friend bool operator==(String &string1, String& string2);在类外分别定义3个运算符重载函数:bool operator>(String &string1,String &string2) //对运算符“>”重载{if(strcmp(string1.p,string2.p)>0)return true;elsereturn false;}bool operator<(String &string1,String &string2) //对运算符“<”重载{if(strcmp(string1.p,string2.p)<0)return true;elsereturn false;}bool operator==(String &string1,String &string2) //对运算符“==”重载{if(strcmp(string1.p,string2.p)==0)return true;elsereturn false;}再修改主函数:int main( ){String string1(″Hello″),string2(″Book″),string3(″Computer″); cout<<(string1>string2)<<endl; //比较结果应该为true cout<<(string1<string3)<<endl; //比较结果应该为falsecout<<(string1==string2)<<endl; //比较结果应该为false return 0;}(4) 再进一步修饰完善,使输出结果更直观。
下面给出最后的程序。
#include <iostream>using namespace std;class String{public:String( ){p=NULL;}String(char *str);friend bool operator>(String &string1,String &string2);friend bool operator<(String &string1,String &string2);friend bool operator==(String &string1,String &string2);void display( );private:char *p;};String∷String(char *str){p=str;}void String∷display( ) //输出p所指向的字符串{cout<<p;}bool operator>(String &string1,String &string2) {if(strcmp(string1.p,string2.p)>0)return true;elsereturn false;}bool operator<(String &string1,String &string2) {if(strcmp(string1.p,string2.p)<0)return true;elsereturn false;}bool operator==(String &string1,String &string2) {if(strcmp(string1.p,string2.p)==0)return true;elsereturn false;}void compare(String &string1,String &string2){if(operator>(string1,string2)==1){string1.display( );cout<<″>″;string2.display( );}elseif(operator<(string1,string2)==1){string1.display( );cout<<″<″;string2.display( );}elseif(operator==(string1,string2)==1){string1.display( );cout<<″=″;string2.display( );}cout<<endl;}int main( ){String string1(″Hello″),string2(″Book″),string3(″Computer″),string4(″Hello″);compare(string1,string2);compare(string2,string3);compare(string1,string4);return 0;}例10.5 有一个Time类,包含数据成员minute(分)和sec(秒),模拟秒表,每次走一秒,满60秒进一分钟,此时秒又从0开始算。