# 概念
多態性是指具有不同功能的函數可以用同一個函數名,可以用一個函數名調用不同內容的函數。
# 分類
* 靜態多態性
* 動態多態性
# 靜態多態性
## 概念
靜態多態性采用**靜態聯編技術**,是指在程序編譯階段就可確定的多態性,又稱為**編譯時的多態性**。它有兩種實現形式:**強制多態**和**重載多態**。
## 強制多態
* 將一種類型的值強制轉換成為另一種類型的值稱為**類型強制**,即強制多態.
* 在**基本數據類型**之間可以通過**顯式**的強制類型轉換實現類型強制。
### **例子**
~~~
#include <iostream>
using namespace std;
?
class A
{
? ?public:
? ?A(int x)
? {
? ? ? ?a=x; cout<<a<<“ ?constructor of class A is called!\n”;
? } //構造函數用于類型強制
? ?operator int()
? {
? ? ? ?cout<<a<<“ ?operator int() is called!\n”; return a;
? } //類型轉換成員函數用于類型強制
? ?private:
? ?int a;
};
?
void f(const A& x)
{
? ?cout<<"function f is called!\n";
}
?
int main( )
{
? ?A obj(1);
? ?f(2);
? ?obj=‘a’;
? ?int m=obj;
? ?return 0;
}
~~~
## 重載多態
重載實際上是給程序正文中相同作用域內的的同一個標識符賦予不同的操作語義。(參考函數重載)
### **例子**
~~~
#include <iostream.h>
using namespace std;
class Circle //圓形類
{
? ?public:
? ?Circle(double a=0.0, double b=0.0, double r=0.0)
? {
? ? ? ?x=a, y=b, radius=r;
? }
? ?double area( )
? {
? ? ? ?return 3.14159*radius*radius;
? }
? ?Circle & max(Circle &c)
? {
? ? ? ?//函數重載,求2個Circle類對象的最大值
? ? ? ?return area( )>c.area( )? *this : c;
? }
? ?friend ostream& operator << (ostream&, Circle&);
? ?//運算符重載,將流插入運算符'<<'重載為該類的友元函數
? ?private:
? ?double x,y;//圓心坐標
? ?double radius;//半徑
};
ostream& operator<<(ostream &output,Circle &c)
{
? ?output<<“center:(”<<c.x<<‘,’<<c.y<<“)\narea=”<<c.area( )<<endl;
? ?return output;
}
int ?main( )
{
? ?Circle c1(1, 2, 3),c2(4, 5, 6),c3;
? ?c3=c1.max(c2);
? ?cout<<c3; ? ?
? ?return 0;
}
~~~
# 動態多態性
## 概念
動態多態性采用**動態聯編技術**,是指在**程序運行階段**才能確定的多態性,又稱為**運行時的多態性**。它有兩種實現形式:**類型參數多態**和**包含多態**。
## 類型參數多態
**類型參數多態**即**模板機制**來實現類屬,這種方法可以直接將數據類型作為類的參數或函數的參數使用,從而可把功能上一致,而只是在參數類型上有差異的類或函數聲明為一個通用類或通用函數,即形成**類模板**或**函數模板**。
### **例子**
~~~
#include <iostream>
using namespace std;
?
template<class T> ? ? ? ? //聲明類模板
class Compare
{
? ?public:
? ?Compare(T a, T b):x(a), y(b) { }//定義函數模板
? ?T max( ) ? ? ? ? ? ? ? //定義函數模板
? {
? ? ? ?return (x>y)?x:y;
? }
? ?private:
? ?T x, y;
};
int main()
{
? ?Compare <int> c1(3,7); ?//定義對象c1,用于兩個整數的比較
? ?cout<<c1.max( ) << endl;
? ?Compare <float> c2(45.78,93.6);//定義對象c2,用于兩個浮點數的比較
? ?cout<<c2.max( ) <<endl;
? ?Compare <char> c3(’a’, ’A’); //定義對象c3,用于兩個字符的比較
? ?cout<<c3.max( ) <<endl;
? ?return 0;
}
~~~
## 包含多態
通過虛函數(virtual function)實現的。包含多態是“一個接口,多種方法”。
## 例子
~~~
#include <iostream>
using namespace std;
?
//聲明類Point
class Point
{
? ?public:
? ?Point(float=0,float=0);
? ?void setPoint(float,float);
? ?float getX() const
? {
? ? ? ?return x;
? }
? ?float getY() const
? {
? ? ? ?return y;
? }
? ?friend ostream & operator<<(ostream &,const Point &);
? ?protected:
? ?float x,y;
};
//定義Point類的成員函數
//Point的構造函數
Point::Point(float a,float b)
{
? ?x=a;y=b;
}
//設置x和y的坐標值
void Point::setPoint(float a,float b)
{
? ?x=a;y=b;
}
//輸出點的坐標
ostream & operator<<(ostream &output,const Point &p)
{
? ?output<<"["<<p.x<<","<<p.y<<"]"<<endl;
? ?return output;
}
int main()
{
? ?Point p(3.5,6.4);
? ?cout<<"x="<<p.getX()<<",y="<<p.getY()<<endl;
? ?p.setPoint(8.5,6.8);
? ?cout<<"p(new):"<<p<<endl;
? ?return 0;
}
~~~
- 介紹
- 編程設計語言
- 第一章 對C++的初步認識
- 1.2 最簡單的C++程序
- 1.3 C++對C的補充
- 1.3.1 return
- 1.3.2 輸入輸出流
- 1.3.3 putchar 和 getchar
- 1.3.4 用const定義常變量
- 1.3.5 函數原型聲明
- 1.3.6 內置函數
- 1.3.7 函數重載
- 1.3.8 函數模板
- 1.3.9 有默認值的參數
- 1.3.10 作用域
- 1.3.11 const修飾指針
- 1.3.12 引用
- 1.3.13 生命期
- 1.3.14 變量
- 1.3.15 字符串變量
- 第二章 類與對象
- 2.2 類的聲明和對象的定義
- 2.3 類的成員函數
- 第三章 關于類和對象的進一步討論
- 3.1 構造函數
- 3.1.1 對象的初始化
- 3.1.2 構造函數
- 3.2 析構函數
- 3.3調用析構函數和構造函數的順序
- 3.4 對象數組
- 3.5 對象指針
- 3.6 共享數據的保護
- 3.7 對象的建立與釋放
- 3.8 對象的賦值與復制
- 3.9 靜態成員
- 3.10 友元
- 3.11 類模板
- 第四章 運算符重載
- 數據類型轉換
- 運算符重載
- 重載流插入運算符和流提取運算符
- 第五章 繼承與派生
- 繼承與派生
- 第六章 多態性與虛函數
- 多態性
- 虛函數
- 純虛函數與抽象類