川北医学院:《C++程序设计》课程教学资源(课件讲稿)第10章 运算符重载

第10章运算符重载 10.1什么是运算符重载 10.2运算符重载的方法 10.3重载运算符的规则 10.4运算符重载函数作为类成员函数和友元函 数 10.5 重载双目运算符 10.6重载单目运算符 10.7重载流插入运算符和流提取运算符 10.8不同类型数据间的转换 2017年4月26日12时 HO 第10章运算符重载 5分 BACK NEX
HOME 10.1 什么是运算符重载 10.2 运算符重载的方法 10.3 重载运算符的规则 10.4 运算符重载函数作为类成员函数和友元函 数 10.5 重载双目运算符 10.6 重载单目运算符 10.7 重载流插入运算符和流提取运算符 10.8 不同类型数据间的转换 2017年4月26日12时 15分 第10章 运算符重载 2

10.1什么是运算符重载 Operator Overloading Operator overloading allows the programmer to define versions of the predefined operators for operands of class type. Operator overloading is just "syntactic sugar,"which means it is simply another way for you to make a function call. The difference is that the arguments for this function don't appear inside parentheses,but instead they surround or are next to characters you've always thought of as immutable operators. 2017年4月26日12时 第10章运算符重载 3 BACK NEX
HOME • Operator overloading allows the programmer to define versions of the predefined operators for operands of class type. • Operator overloading is just “syntactic sugar, ” which means it is simply another way for you to make a function call. • The difference is that the arguments for this function don’t appear inside parentheses, but instead they surround or are next to characters you’ve always thought of as immutable operators. 2017年4月26日12时 15分 第10章 运算符重载 3

Syntax Defining an overloaded operator is like defining a function,but the name of that function is operator@,in which represents the operator that's being overloaded. Return type Name (parameters list ) Return type operator@(parameters list ) 2017年4月26日12时 H0平5务 第10章运算符重载 4 BACK NEXT
HOME • Defining an overloaded operator is like defining a function, but the name of that function is operator@, in which @ represents the operator that’s being overloaded. • Return type Name (parameters list ); • Return type operator@ (parameters list ); 2017年4月26日12时 15分 第10章 运算符重载 4

Syntax The number of arguments depends on two factors: 1.Whether it's a unary operator (one argument)or a binary operator (two arguments). 2.Whether the operator is defined as a global function (one argument for unary,two for binary)or a member function (zero arguments for unary,one for binary-the object becomes the left-hand argument). 2017年4月26日12时 H05务 第10章运算符重载 5 BACK NEXT
HOME • The number of arguments depends on two factors: • 1. Whether it’s a unary operator (one argument) or a binary operator (two arguments). • 2. Whether the operator is defined as a global function (one argument for unary, two for binary) or a member function (zero arguments for unary, one for binary – the object becomes the left-hand argument). 2017年4月26日12时 15分 第10章 运算符重载 5

例10.1通过函数来实现复数相加 #include using namespace std; class Complex public: Complex(){real=0;imag-0;) Complex(double r,double i)freal=r;imag=i;} Complex complex_add(Complex &c2); void display(); /声明输出函数 private: double real; /实部 double imag; ∥虚部 2017年4月26日12时 H0务 第10章运算符重载 6 BACK NEXT
HOME #include using namespace std; class 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; //虚部 }; 2017年4月26日12时 15分 第10章 运算符重载 6

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;) 2017年4月26日12时 H0平5务 第10章运算符重载 BACK NEXT
HOME 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;} 2017年4月26日12时 15分 第10章 运算符重载 7

int main() Complex c1(3,4),c2(5,-10),c3; /定义3个复数对象 c3=c1.complex_add(c2); /调用复数相加函数 cout<<"c1=";c1.display(); cout<<"c2=";c2.display(); cout<<"c1+c2=";c3.display(); return 0; 2017年4月26日12时 第10章运算符重载 8 H0座务 BACK NEXT
HOME int main( ) { Complex c1(3,4),c2(5,-10),c3; //定义3个复数对象 c3=c1.complex_add(c2); //调用复数相加函数 cout<<"c1="; c1.display( ); cout<<"c2="; c2.display( ); cout<<"c1+c2="; c3.display( ); return 0; } 2017年4月26日12时 15分 第10章 运算符重载 8

10.2运算符重载的方法 Method of Operator Overloading 重载运算符的函数一般格式如下: 函数类型operator运算符名称(形参表列) {对运算符的重载处理} 将“+”用于Complex类(复数)的加法运算: Complex operator+(Complex&c1,Complex&c2); 2017年4月26日12时 9 H0务 第10章运算符重载 BACK NEXT
HOME 重载运算符的函数一般格式如下: 函数类型 operator 运算符名称 (形参表列) { 对运算符的重载处理 } 将“+”用于Complex类(复数)的加法运算: Complex operator+ (Complex& c1,Complex& c2); 2017年4月26日12时 15分 第10章 运算符重载 9

例10.2运算符重载函数作为类成员函数 #include using namespace std; class Complex public: Complex()real=0;imag=0;} Complex(double r,double i)freal=r;imag=i;} Complex operator+(Complex &c2); void display(); private: double real; double imag; 2017年4月26日12时 H0务 第10章运算符重载 10 BACK NEXT
HOME #include 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; }; 2017年4月26日12时 15分 第10章 运算符重载 10

ComplexComplex:operator+(Complex &c2) Complex c; c.real=real+c2.real; c.imag-imag+c2.imag; return c; //Complex Complex:operator +(Complex &c2) /freturn Complex(real+c2.real,imag+c2.imag);} void Complex:display() cout<<"("<<real<","<<imag<<"i)"<<endl;} 2017年4月26日12时 0店务 第10章运算符重载 11 BACK NEXT
HOME Complex Complex::operator+(Complex &c2) { Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c; } //Complex Complex::operator + (Complex &c2) //{return Complex(real+c2.real, imag+c2.imag);} void Complex::display( ) { cout<<"("<<real<<" , "<<imag<<"i)"<<endl;} 2017年4月26日12时 15分 第10章 运算符重载 11
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第8章 类和对象.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第6章 指针.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第5章 数组 Arrays.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第4章 函数与预处理.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第3章 程序设计初步.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第2章 数据类型与表达式 Data Types & Expression.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第1章 C++的初步知识(主讲:祝元仲)C++ Programming.pdf
- 同济大学:《Visual Basic程序设计简明教程》配套PPT课件讲稿(第三版)第10章 数据库应用基础.ppt
- 同济大学:《Visual Basic程序设计简明教程》配套PPT课件讲稿(第三版)第7章 用户界面设计、第8章 数据文件、第9章 图形操作.ppt
- 同济大学:《Visual Basic程序设计简明教程》配套PPT课件讲稿(第三版)第4章 VB控制结构、第5章 数组和自定义类型、第6章 过程.ppt
- 同济大学:《Visual Basic程序设计简明教程》配套PPT课件讲稿(第三版)第1章 Visual Basic程序设计概述、第2章 VB可视化编程基础、第3章 VB语言基础.ppt
- 甘肃农业大学:《VB程序设计基础》课程教学资源(PPT讲稿)C语言概述.ppt
- 甘肃农业大学:《VB程序设计基础》课程教学资源(作业习题)VB习题1.pdf
- 甘肃农业大学:《VB程序设计基础》课程教学资源(授课教案)第一章 VB概述.doc
- 甘肃农业大学:《VB程序设计基础》课程教学资源(教学大纲)Programming of Visual Basic.pdf
- Connectivity Analysis in Wireless Networks with Correlated Mobility and Cluster Scalability.pdf
- Asymptotic Analysis on Content Placement and Retrieval in MANETs.pdf
- Are we connected? Optimal Determination of Source-destination Connectivity in Random Networks.pdf
- Mobility Weakens the Distinction between Multicast and Unicast.pptx
- Mobility Weakens the Distinction between Multicast and Unicast.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第11章 继承与派生.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第12章 多态性与虚函数 Polymorphism & Virtual Functions.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第9章 关于类和对象的进一步讨论.pdf
- 川北医学院:《单片机原理》课程教学资源(教学大纲)单片机原理与应用技术 Monolithic principle and application technology.pdf
- 川北医学院:《单片机原理》课程教学资源(考试大纲).pdf
- 川北医学院:《单片机原理》课程教学资源(教案)生物医学工程专业.pdf
- 川北医学院:《单片机原理》课程教学资源(讲稿,共八章)生物医学工程专业.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第1章 基础知识.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第2章 单片机的结构原理与简单应用.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第3章 8051指令系统.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第4章 汇编语言程序设计.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第5章 中断系统.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第6章 定时器/计数器.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第7章 并行扩展技术.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第8章 串行通信.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第9章 串行扩展技术.pdf
- 烟台理工学院:《Python程序设计》课程教学资源(教学大纲)理论课教学大纲(自动化和机器人工程专业大一本科、人工智能专业大二本科).docx
- 烟台理工学院:《Python程序设计》课程教学资源(教学大纲)Course Design of Python(人工智能专业本科大二).doc
- 烟台理工学院:《人工智能编程技术》课程教学资源(教学大纲)Course Design of artificial intelligence program technology.doc
- 烟台理工学院:《人工智能原理》课程教学资源(教学大纲)Principles of Artificial Intelligence(人工智能专业本科大二).doc