C/C++中多態(tài)性詳解及其作用介紹
概述
多態(tài)性 (polymorphism) 是面向?qū)ο蟪绦蛟O(shè)計的一個重要特征. 利用多態(tài)性擴展設(shè)計和實現(xiàn)一個易于擴展的系統(tǒng).

C++ 中多態(tài)性:
- 同一函數(shù)名可以實現(xiàn)不同的功能
- 用一個函數(shù)名調(diào)用不同內(nèi)容的函數(shù)完成不同的工作
靜態(tài)多態(tài)
靜態(tài)多態(tài) (static polymorphism) 是通過函數(shù)的重載實現(xiàn)的, 包括函數(shù)的重載和運算符重載. 在程序編譯時系統(tǒng)就能覺得調(diào)用哪個函數(shù).
函數(shù)重載
int main() {
cout << max(1,2) << endl;
cout << max(1.2, 2.3) << endl;
return 0;
}
int max(int a, int b) {
return (a > b) ? a:b;
}
double max(double a, double b){
return (a > b) ? a:b;
}
輸出結(jié)果:
2
2.3
運算符重載
int main() {
Complex c1(2, 4), c2(6, 10);
c1 = c1 + c2;
c1.display();
return 0;
}
Complex Complex::operator+(Complex &c) {
return Complex(real + c.real, imag + c.imag);
}
輸出結(jié)果:
(8, 14i)
動態(tài)多態(tài)
動態(tài)多態(tài) (dynamic polymorphism) 是在程序運行中才動態(tài)地確定操作所針對的對象.
非動態(tài)
Person 類:
#ifndef PROJECT6_PERSON_H
#define PROJECT6_PERSON_H
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name; // 姓名
char gender; // 性別
public:
Person(string n, char g) : name(n), gender(g) {}
void display() {
cout << "name: " << name << endl;
cout << "gender: " << gender << endl;
}
};
#endif //PROJECT6_PERSON_H
Teacher 類:
#ifndef PROJECT6_TEACHER_H
#define PROJECT6_TEACHER_H
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;
class Teacher : public Person {
private:
string title; // 頭銜
public:
Teacher(string n, char g, string t) : Person(n, g), title(t) {}
void display() {
Person::display();
cout << "title: " << title << endl;
}
};
#endif //PROJECT6_TEACHER_H
main:
#include <iostream>
#include "Person.h"
#include "Teacher.h"
int main() {
// 創(chuàng)建對象
Person p1("王叔叔", 'm'), *pt; // 指針類型為
Teacher t1("王老師", 'f', "教導(dǎo)主任");
pt = &p1;
pt->display();
pt = &t1;
pt->display();
return 0;
}
輸出結(jié)果:
name: 王叔叔
gender: m
name: 王老師
gender: f
我們可以發(fā)現(xiàn) Teacher 對象的頭銜并沒有輸出, 因為 pt 指針的類型是 Person, 調(diào)用的是 Person 的display()函數(shù).
動態(tài)
我們把show()函數(shù)聲明為虛函數(shù).
Person 類:
#ifndef PROJECT6_PERSON_H
#define PROJECT6_PERSON_H
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name; // 姓名
char gender; // 性別
public:
Person(string n, char g) : name(n), gender(g) {}
virtual void display() {
cout << "name: " << name << endl;
cout << "gender: " << gender << endl;
}
};
#endif //PROJECT6_PERSON_H
main:
#include <iostream>
#include "Person.h"
#include "Teacher.h"
int main() {
// 創(chuàng)建對象
Person p1("王叔叔", 'm'), *pt; // 指針類型為
Teacher t1("王老師", 'f', "教導(dǎo)主任");
pt = &p1;
pt->display();
pt = &t1;
pt->display();
return 0;
}
輸出結(jié)果:
name: 王叔叔
gender: m
name: 王老師
gender: f
title: 教導(dǎo)主任
到此這篇關(guān)于C/C++中多態(tài)性詳解及其作用介紹的文章就介紹到這了,更多相關(guān)C++多態(tài)性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++分析構(gòu)造函數(shù)與析造函數(shù)的特點梳理
本文對類的構(gòu)造函數(shù)和析構(gòu)函數(shù)進(jìn)行總結(jié),主要包括了構(gòu)造函數(shù)的初始化、重載、使用參數(shù)和默認(rèn)參數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù),希望能幫助讀者在程序開發(fā)中更好的理解類,屬于C/C++基礎(chǔ)2022-05-05

