当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

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

资源类别:文库,文档格式:PDF,文档页数:57,文件大小:1.72MB,团购合买
10.1 什么是运算符重载 10.2 运算符重载的方法 10.3 重载运算符的规则 10.4 运算符重载函数作为类成员函数和友元函数 10.5 重载双目运算符 10.6 重载单目运算符 10.7 重载流插入运算符和流提取运算符 10.8 不同类型数据间的转换
点击下载完整版文档(PDF)

第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

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共57页,可试读19页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有