Skip to content

Latest commit

 

History

History
51 lines (39 loc) · 832 Bytes

027-继承.md

File metadata and controls

51 lines (39 loc) · 832 Bytes

027-继承

和Java中的概念基本相同,看下怎么写就行

#include <iostream>

class Entity {
public:
    float mX = 0.0f;
    float mY = 0.0f;

    void move(float dx, float dy) {
        mX += dx;
        mY += dy;
    }

    void printPosition() {
        std::cout << mX << "," << mY << endl;
    }
};

class Player : public Entity {
public:
    const char *mName = "Player";

    void printName() {
        std::cout << mName << std::endl;
    }
};

int main() {
    Player player;
    player.printName();
    player.move(5, 8);
    player.printPosition();
    return 0;
}
F:\Projects\ClionProjects\test\cmake-build-debug-cygwin\test.exe
Player
5,8

进程已结束,退出代码为 0

https://www.bilibili.com/video/BV1Yf4y1e7t6