r/cpp_questions • • 25d ago

OPEN Is this code clean OOP?

Hey so I'm more new to OOP, virtual func's, and inheritance. I was wondering if my code was clean enough for basically a template to look back on later in the future when my tiny brain forgets what virtual func, and inheritance is or well forgets how to code them.

#include <string>
#include <iostream>


struct Vector2 {
    int x, y;
};


class Entity {
    private:
        Vector2 pos;
        Vector2 size;
        std::string entityType;


    public:


    Entity(Vector2 e_pos, Vector2 e_size, const std::string& e_type) : pos(e_pos), size(e_size), entityType(e_type) {}


    Vector2 getPos() const {
        return pos;
    }


    Vector2 getSize() const {
        return size;
    }

    virtual void print() const {
        std::cout << "Entity: " << entityType << '\n';
    }


    virtual ~Entity() = default;
};


class Player : public Entity {
    private:
        std::string name;


    public:
        Player(const std::string& p_name, Vector2 startPOS, Vector2 p_size) 
            : Entity(startPOS, p_size, "player"), name(p_name) {}
};


class Orc : public Entity {
    private:
        std::string weapon;

    public:
        Orc(const std::string& o_weapon, Vector2 startPOS, Vector2 o_size) 
            : Entity(startPOS, o_size, "orc"), weapon(o_weapon) {}


    void print() const override {
        std::cout << weapon << '\n';
    }
};


int main() {
    Vector2 startPos = { 100, 100 };
    Vector2 size = { 64, 64 };


    std::string hero = "hero";
    std::string orcWep = "HeroSlayer100";


    Player player(hero, { 20, 30 }, size);
    Orc orc(orcWep, startPos, size);



    player.print();
    orc.print();

    return 0;
}#include <string>
#include <iostream>


struct Vector2 {
    int x, y;
};


class Entity {
    private:
        Vector2 pos;
        Vector2 size;
        std::string entityType;


    public:


    Entity(Vector2 e_pos, Vector2 e_size, const std::string& e_type) : pos(e_pos), size(e_size), entityType(e_type) {}


    Vector2 getPos() const {
        return pos;
    }


    Vector2 getSize() const {
        return size;
    }

    virtual void print() const {
        std::cout << "Entity: " << entityType << '\n';
    }


    virtual ~Entity() = default;
};


class Player : public Entity {
    private:
        std::string name;


    public:
        Player(const std::string& p_name, Vector2 startPOS, Vector2 p_size) 
            : Entity(startPOS, p_size, "player"), name(p_name) {}
};


class Orc : public Entity {
    private:
        std::string weapon;

    public:
        Orc(const std::string& o_weapon, Vector2 startPOS, Vector2 o_size) 
            : Entity(startPOS, o_size, "orc"), weapon(o_weapon) {}


    void print() const override {
        std::cout << weapon << '\n';
    }
};


int main() {
    Vector2 startPos = { 100, 100 };
    Vector2 size = { 64, 64 };


    std::string hero = "hero";
    std::string orcWep = "HeroSlayer100";


    Player player(hero, { 20, 30 }, size);
    Orc orc(orcWep, startPos, size);



    player.print();
    orc.print();

    return 0;
}

if there are any tips you have let me know!

0 Upvotes

27 comments sorted by

View all comments

-1

u/bert8128 25d ago

Define “clean”.

3

u/RedNorthener101 25d ago

Like easy to read, maintain and understand by any dev?

2

u/Hour_Sell3547 25d ago

Well, there are some specifics that we usually mean by "clean code" in SWE. Hence the confusion.

2

u/bert8128 25d ago edited 25d ago

I’ve no objection to the code. Doesn’t do much yet though - it’s too small to judge. I might prefer print to take a stream by reference - seems a bit harsh to demand that it has to go to cout. Or perhaps just expose the type name by const ref. As someone else said, you probably should be using an enum for the type. And look up the rule of 5 - in this case I would say you need the others, but I abide by the rule of 5 (and also swap, and spaceship/ equality) always, it just removes arguments.

Clean has some overtones. See this https://amzn.eu/d/06NmYxCF and this https://youtu.be/czr1L3JocOI?is=yaJzED-wXHcd_JML (can’t find the original video).

1

u/Total-Box-5169 22d ago

Academic exercises are not enough to properly understand why and when to use software engineering concepts. You badly need to work on a personal project that is big and complex enough to apply that knowledge.
As is your code has too much data hard coded and there is no separation between presentation layer and data layer.
Toy code and small projects allows you to get away with many awful things that would destroy large or more complex projects. Many people fail to understand that and only learn the hard way.