0

So I was thinking about trying to program the tetris game in c++ with the sfml and I thought about how I could implement the game and realized it could be a could exercice on inheritance. I thought I will have:

-an abstract base class that will represent a tetromino (shapes puzzle pieces in tetris) : its called Tetromino_Base

-classes that'll inherit from the base class to represent a specific tetromino, since there are different shapes like squares, bars... etc.

-an interface class called Tetromino which is the type the user will manipulate.

As I go I realized that my class interface needs access to the abstract class members, hence I made a friend declaration for it but I thought: is it good practice to do so? Make a friend declaration your interface class in the base class?

Here's the main part of the code:

class Tetromino;

class Tetromino_Base {
    friend class Tetromino;
public:
    virtual ~Tetromino_Base() {};
protected:
    float m_x, m_y;
    sf::Texture m_texture;
    sf::VertexArray m_vertices;

    virtual bool create(const sf::Vector2f&, const float&, //position of the object, scale of the object
    const std::string&, const sf::Vector2f&, const float&) = 0; 
    //file name for the texture, position in the texture, scale of the texture
};

class Square : public Tetromino_Base {
public:
    ~Square() {}
private:
    bool create(const sf::Vector2f&, const float&, 
    const std::string&, const sf::Vector2f&, const float&);
};

class Tetromino : public sf::Drawable {
public:
    Tetromino(int tetroCode = 0);

    bool create(const sf::Vector2f& sorigins, const float& ssc, 
    const std::string& fileName, const sf::Vector2f& torigins, const float& tsc){
        //access needed here
        return p->create(sorigins,ssc,fileName,torigins,tsc);
    }

private:
    Tetromino(Tetromino_Base* ptr) : p (ptr) {}
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
        //access needed here
        states.texture = &(p->m_texture);
        target.draw(p->m_vertices, states); 
    }
    Ptr<Tetromino_Base> p;
};
grybouilli
  • 185
  • 2
  • 11
  • Why not make it public? Abstract class with no methods to be used does not make much sense. – Yksisarvinen Jun 28 '18 at 10:55
  • 2
    C++ isn't Java. Your abstract base class is your "interface class." – Mike Borkland Jun 28 '18 at 10:57
  • I inspired myself from the implementation of the book accelerated c++: I wanna be able to make a tetromino object regardless of what kind of tetromino it is allowing declaration like: Tetromino square(1); (1 being the code for the square shape) which is why I make an interface class aside of the abstract class I guess – grybouilli Jun 28 '18 at 11:03
  • If there's a good reason you want to use shape codes (use enums btw) instead of simply creating an object of type `Square`, then I suppose this is not bad. I'd personally avoid friend class (they are not very welcome in C++, it's very strong coupling of two classes) and make everything in Base class public. Or perhaps add methods to Base for accessing members (or even better to draw the shape already, without exposing members). – Yksisarvinen Jun 28 '18 at 11:18
  • I wanna be able to generate a random shape which is why I need to create a Tetromino instead of a Square or a Bar, etc. I'm gonna try with the enums and probably gonna make draw part of the base, and probably gonna make create function public. That I can cancel the friend declaration – grybouilli Jun 28 '18 at 11:26

0 Answers0