Skip to content

Latest commit

 

History

History
44 lines (34 loc) · 654 Bytes

Inheritance.md

File metadata and controls

44 lines (34 loc) · 654 Bytes

Inheritance

Inheritance in C++ Video

Inheritance avoids code duplication

#include <iostream>

class Entity
{
public
    float X, Y;

    void Move(float xa, float ya)
    {
        X += xa;
        Y += ya;
    }
};

class Player : public Entity
{
    const char* Name;

    void PrintName()
    {
        std::cout << Name << std::endl;
    }
};

int main()
{
    Player player;
    player.PrintName();
    player.Move(5f, 5f);
    player.X = 2f;

    std::cin.get();
}

Player is a superset of Entity. Entity is a subset of Player.