当前位置:文档之家› 函数重载和运算符重载

函数重载和运算符重载

本章内容第四章
本章内容
类中成员函数
class print{不要将不同功能的函数定义为重载函数,以
本章内容
问题
例:字符串处理
//
C++
s1 = "Hello";
s2 = "world";
string s3;
s3 = s1; //
cout<<s3<<endl;
s3 = s1 + s2; //
cout<<s3<<endl;
运算符函数
运算符函数的调用形式
//例4_4 complex complex::operator void complex::display()
c.imag=c2.imag+imag;
return complex(c.real,c.imag); }
c.imag=imag-c2.imag;return complex(c.real,c.imag);}
如果需要重载一个运算符,使之能够用于//例4_5 以复数
本章内容complex operator +(complex c1,complex c2)
//例4_6 浅拷贝void main()
浅拷贝策略:逐个成员依次拷贝
Person:: Person(char* pN)
{ cout<<"
完成简单的一一对应的复制的拷贝构造函数称 拷贝构造函数的调用 默认的赋值运算符 例:
class B{多态性概述
以weight为例,实现如何重载++运算符//前增量++x
//例4_10 重载运算符++(友元函数)44
4.5.1 重载运算符++和—
CVector::CVector(const int& CVector::operator[](unsigned int& operator[](unsigned CVector& operator=(const CVector&v);CVector& operator=(int ~CVector(void) { delete[]elem; }
插入运算符<<
//例4_12
#include <iostream.h>
class Complex{// 复数类作用:例4_13 //例4_14
当一个运算符表达式出现时,编译器将按下面的顺例4_15
本章上机题目:class String。

相关主题