码迷,mamicode.com
首页 > 编程语言 > 详细

C++多态

时间:2019-03-04 20:55:47      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:img   stream   vat   加法   实现   成员函数   cloc   c++   inf   

多态:同样的消息作用于不同类型的对象产生不同的行为。
1,静态多态

实现:运算符重载。(from 学堂在线

重载为类的成员函数:

 1 //为时钟类重载单目运算符++和--
  #define _CRT_SECURE_NO_WARNINGS 2 3 #include <iostream> 4 5 using namespace std; 6 7 class Clock {//时钟类定义 8 public: 9 Clock(int hour = 0, int minute = 0, int second = 0); 10 void showTime() const; 11 //前置单目运算符重载 12 Clock& operator ++ (); 13 //后置单目运算符重载 14 Clock operator ++ (int); 15 private: 16 int hour, minute, second; 17 }; 18 19 Clock::Clock(int hour, int minute, int second) 20 { 21 if (0 <= hour && hour < 24 && 0 <= minute && minute < 60 22 && 0 <= second && second < 60) { 23 this->hour = hour; 24 this->minute = minute; 25 this->second = second; 26 } 27 else 28 cout << "Time error!" << endl; 29 }
30 void Clock::showTime() const 31 { //显示时间 32 cout << hour << ":" << minute << ":" << second << endl; 33 } 34 35 //前置返回当前对象,通过引用可以直接操作对象,所以是左值 36 Clock & Clock::operator ++ () 37 { 38 second++; 39 if (second >= 60) { 40 second -= 60; minute++; 41 if (minute >= 60) { 42 minute -= 60; hour = (hour + 1) % 24; 43 } 44 } 45 return *this; 46 } 47 48 //后置,返回临时局部对象,不能通过该值改变对象,所以是右值 49 Clock Clock::operator ++ (int) 50 { 51 //注意形参表中的整型参数 52 Clock old = *this; 53 ++(*this); //调用前置“++”运算符 54 return old; 55 } 56 57 58 int main() 59 { 60 Clock myClock(23, 59, 59); 61 cout << "First time output: "; 62 myClock.showTime(); 63 cout << "Show myClock++: "; 64 (myClock++).showTime(); 65 cout << "Show ++myClock: "; 66 (++myClock).showTime(); 67 68 return 0; 69 }

 

重载为非成员函数:

有些运算符不能重载为类的成员函数(二元运算符的左操作数不是对象,某些类不是自己设计的)。

1 //为复数类重载加减法和"<<"运算符
 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 class Complex {
 8 public:
 9     Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
10     friend Complex operator+(const Complex &c1, const Complex &c2);
11     friend Complex operator-(const Complex &c1, const Complex &c2);
12     friend ostream & operator<<(ostream &out, const Complex &c);
13 private:
14     double real;  //复数实部
15     double imag;  //复数虚部
16 };
17 
18 
19 Complex operator+(const Complex &c1, const Complex &c2)
20 {
21     return Complex(c1.real + c2.real, c1.imag + c2.imag);
22 }
23 Complex operator-(const Complex &c1, const Complex &c2)
24 {
25     return Complex(c1.real - c2.real, c1.imag - c2.imag);
26 }
27 
28 //实现级联输出
29 ostream & operator<<(ostream &out, const Complex &c)
30 {
31     out << "(" << c.real << ", " << c.imag << ")";
32     return out;
33 }
34 
35 int main()
36 {
37     Complex c1(5, 4), c2(2, 10), c3;
38     cout << "c1 = " << c1 << endl;
39     cout << "c2 = " << c2 << endl;
40     c3 = c1 - c2;   //使用重载运算符完成复数减法
41     cout << "c3 = c1 - c2 = " << c3 << endl;
42     c3 = c1 + c2;   //使用重载运算符完成复数加法
43     cout << "c3 = c1 + c2 = " << c3 << endl;
44 
45     return 0;
46 }

 

2,动态多态

实现:虚函数

 1 class Base {
 2 public:
 3     virtual void f();
 4     virtual void g();
 5 private:
 6     int i;
 7 };
 8 
 9 class Derived : public Base {
10 public:
11     virtual void f();  //覆盖Base::f
12     virtual void h();  //新增虚函数
13 private:
14     int j;
15 };

技术图片

 

C++多态

标签:img   stream   vat   加法   实现   成员函数   cloc   c++   inf   

原文地址:https://www.cnblogs.com/EIMadrigal/p/10472918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!