当前位置:   article > 正文

C++多态例子:virtual、override、final_virtual final

virtual final

1. 简单的虚函数例子:virtual、override、final

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
using namespace std;

class Animal {
public :
    Animal(string name):name(name){}
    virtual void run() {
        cout << "I don't kown how to run" << endl;
    }
private:
    string name;
};

class Cat : public Animal {
public :
    Cat(string name):Animal(name){}
    void run() override final{
        cout << "I can run with four legs" << endl;
    }
};

class People : public Animal {
public :
    People(string name):Animal(name){}
    void run() override final{
        cout << "I can run with two legs" << endl;
    }
};

class Bat : public Animal {
public :
    Bat(string name) : Animal(name){}
    void run() override final{
        cout << "I can fly" << endl;
    }
};

int main(){
    #define MAX_N 10
    srand(time(0));
    Animal **zoo = new Animal*[MAX_N];
    for (int i = 0; i < MAX_N; i++) {
        switch(rand() % 3) {
            case 0: zoo[i] = new Cat("cat"); break;
            case 1: zoo[i] = new People("people"); break;
            case 2: zoo[i] = new Bat("bat"); break;
        }
    }
    for (int i = 0; i < MAX_N; i++) zoo[i]->run();
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

运行结果

I can run with four legs
I can run with four legs
I can fly
I can fly
I can fly
I can run with four legs
I can run with four legs
I can run with two legs
I can fly
I can run with two legs

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2. 思考虚函数表

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
using namespace std;

class A {
public :
    virtual void say() {
        cout << "Class A say" << endl;
    }
    virtual void run() {
        cout << "Class A run" << endl;
    }
};

class B : public A {
public :
    void say() override final{
        cout << "Class B say" << endl;
    }
};

class C : public A {
public :
    void run() override final{
        cout << "Class C run" << endl;
    }
};

#define TEST(a) test(a, #a)
void test(A &a, string class_name) {
    cout << "Object " << class_name << endl;
    a.say();
    a.run();
    cout << "===========" << endl << endl;
    return;
}

int main(){
    A a;
    B b;
    C c;
    TEST(a);
    TEST(b);
    TEST(c);
	//注意思考此处
    ((void **)(&b))[0] = ((void **)(&a))[0];
    TEST(b);
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

运行结果

Object a
Class A say
Class A run
===========

Object b
Class B say
Class A run
===========

Object c
Class A say
Class C run
===========

Object b
Class A say
Class A run
===========


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号