亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C/C++中多態(tài)性詳解及其作用介紹

 更新時(shí)間:2021年09月06日 15:50:50   作者:我是小白呀  
這篇文章主要介紹了C/C++中多態(tài)性(polymorphism)詳解及其作用介紹,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

概述

多態(tài)性 (polymorphism) 是面向?qū)ο蟪绦蛟O(shè)計(jì)的一個(gè)重要特征. 利用多態(tài)性擴(kuò)展設(shè)計(jì)和實(shí)現(xiàn)一個(gè)易于擴(kuò)展的系統(tǒng).

在這里插入圖片描述

C++ 中多態(tài)性:

  • 同一函數(shù)名可以實(shí)現(xiàn)不同的功能
  • 用一個(gè)函數(shù)名調(diào)用不同內(nèi)容的函數(shù)完成不同的工作

靜態(tài)多態(tài)

靜態(tài)多態(tài) (static polymorphism) 是通過(guò)函數(shù)的重載實(shí)現(xiàn)的, 包括函數(shù)的重載和運(yùn)算符重載. 在程序編譯時(shí)系統(tǒng)就能覺(jué)得調(diào)用哪個(gè)函數(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

運(yùn)算符重載

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)

動(dòng)態(tài)多態(tài)

動(dòng)態(tài)多態(tài) (dynamic polymorphism) 是在程序運(yùn)行中才動(dòng)態(tài)地確定操作所針對(duì)的對(duì)象.

非動(dòng)態(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)建對(duì)象
    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 對(duì)象的頭銜并沒(méi)有輸出, 因?yàn)?pt 指針的類型是 Person, 調(diào)用的是 Person 的display()函數(shù).

動(dòng)態(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)建對(duì)象
    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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論