0

How the Pong.cpp file know the implementation of the functions in Bat.cpp if it doesn't include Bat.cpp? Is that magic? I want to know what's happening? I am coming from Python programming language and its obvious in Python classes because their implementation of functions come with the signatures.

Here are all the files I am working with:

Bat.h

    #pragma once
    #include <SFML/Graphics.hpp>
    using namespace sf;
    class Bat
    {
    private:
        Vector2f m_Position;
        // A RectangleShape object
        RectangleShape m_Shape;
        float m_Speed = 1000.0f;
        bool m_MovingRight = false;
        bool m_MovingLeft = false;
    public:
        Bat(float startX, float startY);
        FloatRect getPosition();
        RectangleShape getShape();
        void moveLeft();
        void moveRight();
        void stopLeft();
        void stopRight();
        void update(Time dt);
    };

Bat.cpp

    #include "Bat.h"
    
    // This is the constructor and it is called when we create an object
    Bat::Bat(float startX, float startY)
    {
        m_Position.x = startX;
        m_Position.y = startY;
    
        m_Shape.setSize(sf::Vector2f(100, 5));
        m_Shape.setPosition(m_Position);
    }
    FloatRect Bat::getPosition()
    {
        return m_Shape.getGlobalBounds();
    }
    RectangleShape Bat::getShape()
    {
        return m_Shape;
    }
    void Bat::moveLeft()
    {
        m_MovingLeft = true;
    }
    void Bat::moveRight()
    {
        m_MovingRight = true;
    }
    void Bat::stopLeft()
    {
        m_MovingLeft = false;
    }
    void Bat::stopRight()
    {
        m_MovingRight = false;
    }
    void Bat::update(Time dt)
    {
        if (m_MovingLeft)
        {
            m_Position.x -= m_Speed * dt.asSeconds();
        }
        if (m_MovingRight)
        {
            m_Position.x += m_Speed * dt.asSeconds();
        }
        m_Shape.setPosition(m_Position);
    }

Pong.cpp

#include "Bat.h"
#include <sstream>
#include <cstdlib>
#include <SFML/Graphics.hpp>
int main()
{
    // Create a video mode object
    VideoMode vm(1920, 1080);
    // Create and open a window for the game
    RenderWindow window(vm, "Pong", Style::Fullscreen);
    int score = 0;
    int lives = 3;
    
    // Create a bat at the bottom center of the screen
    Bat bat(1920 / 2, 1080 - 20);
    // We will add a ball in the next chapter
    // Create a Text object called HUD
    Text hud;
    // A cool retro-style font
    Font font;
    font.loadFromFile("fonts/DS-DIGI.ttf");
    // Set the font to our retro-style
    hud.setFont(font);
    // Make it nice and big
    hud.setCharacterSize(75);
    // Choose a color
    hud.setFillColor(Color::White);
    hud.setPosition(20, 20);
    // Here is our clock for timing everything
    Clock clock;
    while (window.isOpen())
    {
        /*
        Handle the player input
        ****************************
        ****************************
        ****************************
        */
        /*
        Update the bat, the ball and the HUD
        *****************************
        *****************************
        *****************************
        */
        
        
        /*
        Draw the bat, the ball and the HUD
        *****************************
        *****************************
        *****************************
        */
        
    }
    return 0;
}
Đạt Phạm
  • 13
  • 1
  • 7
  • *"Is that magic?"* There's no magic. The linker links the references in object files. The compiler only needs to have a **declaration** for the function. Additionally, source files(.cpp files) should not be included(just in case you were thinking of including them) – Jason Jul 21 '22 at 08:52
  • 1
    `Pong.cpp` doesn't need to know the implementation of the functions to call the functions, the declarations of the functions are enough for the compiler to know that the functions are implemented (defined) in some [*translation unit*](https://en.wikipedia.org/wiki/Translation_unit_(programming)) somewhere. It's then the *linkers* job to put all the definitions together. All in all, since `Pong.cpp` only need the function declarations (not definitions) then it's okay to only `#include` the header file `Bat.h`. – Some programmer dude Jul 21 '22 at 08:55
  • @DatPhm See the process(3rd step in particular) described here: [compilation & linking process C++](https://stackoverflow.com/a/51488203/12002570) – Jason Jul 21 '22 at 08:58

0 Answers0