c++多态性 运算符重载
来源:互联网 发布:黑客帝国3矩阵革命 mp4 编辑:程序博客网 时间:2024/06/05 03:54
多态是指同样的消息被不同类型的对象接收时导致不同的行为。
: 重载多态
强制多态
包含多态
参数多态
运算符重载
因为c++中的运算符操作对象只适合基本数据类型,所以对用户自定义的类型如类进行运算符重载后使用。
: 重载为类的非静态成员函数和重载为非成员函数。
语法形式如下:
返回类型 operator 运算符(形参表){ 函数体}当重载为非成员函数时,有时需要访问的运算符参数涉及类的私有成员,可以把该函数的函数声明为该类的友元函数。
重载复数类:
重载为两类的区别:1.形参个数区别
2.当重载为函数成员时,左操作数必须是与函数返回类型相应的自定义类型;而重载为非函数成员时左右操作数任何类型都可以。
重载为非成员函数
#include<iostream>using namespace std;class complax{public: complax(double r= 0, double i= 0): real(r), imag(i){} friend complax operator+(complax &a, complax &b); friend complax operator-(complax &a, complax &b); friend ostream &operator<<(ostream &out, complax &c);private: double real, imag;};complax operator+(complax &a, complax &b) //无friend{ return complax(a.real+b.real, a.imag+b.imag);}complax operator-(complax &a, complax &b){ return complax(a.real-b.real, a.imag-b.imag);}ostream &operator<<(ostream &out, complax &c){ out<<c.real<<"+"<<c.imag<<"i"; return out;}int main(){ complax a(3, 4), b(5, 6), c; cout<<a<<endl; cout<<b<<endl; c = a+ b; cout<<c<<endl; c = a-b; cout<<c<<endl; return 0;}
重载为成员函数
#include<iostream>using namespace std;class complax{public: complax(double r= 0, double i= 0): real(r), imag(i){} complax operator+( complax &b); complax operator-( complax &b); friend ostream &operator<<(ostream &out, complax &c);private: double real, imag;};complax complax :: operator+(complax &b){ return complax(real+b.real, imag+b.imag);}complax complax ::operator-(complax &b) //当不是友元函数是函数定义要这样写 返回值类型 作用域 函数名{函数体}{ return complax(real-b.real, imag-b.imag);}ostream &operator<<(ostream &out, complax &c){ out<<c.real<<"+"<<c.imag<<"i"; return out;}int main(){ complax a(3, 4), b(5, 6), c; cout<<a<<endl; cout<<b<<endl; c = a+ b; cout<<c<<endl; c = a-b; cout<<c<<endl; return 0;}
单目运算符的前置与后置
#include<iostream>using namespace std;class Time{public: Time(int hour , int minute , int second); void showTime()const; Time& operator++(); Time operator++(int);private: int hour, minute, second;};Time ::Time(int hour, int minute, int second){ if(hour >= 0&&hour<= 24&&minute>=0&&minute<60&&second>=0&&second<60) { this->hour = hour; this->minute = minute; //相当于hour= h。就是将形参赋值给类里的成员 this->second = second; } else cout<<"Time is error!"<<endl;}void Time::showTime() const{ cout<<hour<<":"<<minute<<":"<<second<<endl;}Time &Time::operator++(){ second++; if(second>=60) { second-=60; minute++; if(minute>=60) { minute-=60; hour =(hour+1)%24; } } return *this;}Time Time::operator++(int){ Time ord = *this; ++(*this); return ord;}int main(){ Time T(23, 59, 59); cout<<"Frist time output"<<endl; T.showTime(); cout<<"show T++"<<endl; (T++).showTime(); cout<<"show ++T"<<endl; (++T).showTime(); return 0;}
阅读全文
0 0
- 多态性-运算符重载
- c++多态性 运算符重载
- 【C++】三大特征之多态性(运算符重载)
- C++ 多态性(2) 运算符重载
- 多态性之运算符重载(二)
- C/C++ 初学简单笔记 —4.1— 多态性 运算符重载
- C++-运算符重载
- [C++]重载运算符
- C#:运算符重载
- c++-++运算符重载
- C++:重载运算符
- 运算符重载(C++)
- [c++]运算符重载
- 【C++】运算符重载
- C++--------------------------------------------运算符重载
- C#:运算符重载
- C++:运算符重载
- C++:运算符重载
- DataFrame使用Series的boolean值做下标过滤数据
- spark(15)-Spark Shuffle(corse24)
- PDO封装
- Leetcode020 Valid Parentheses
- 求一个整数有几 位; 第一行输入一个整数(int)N(如2563) 第二行输出该整数N是个几位数(4).
- c++多态性 运算符重载
- Utility Capacity on Demand(服务器按需扩容技术)
- Kubernes 部署 flannel + etcd
- #angular 国际化
- 文章标题
- 烧录手机image镜像
- Android 硬解码MediaCodec配合SurfaceView的踏坑之旅
- linux下安装jdk与tomcat
- python爬取新浪财经的股票信息