Constructor in a class calling upon variables

278 views Asked by At

I've looked up all sorts of guides and tutorials on classes and constructors, but so far it hasn't made sense to me how to implement the combination of both into my program. I feel like some massive logic block is evading me. I would be incredibly thankful if anyone out there could explain in human language how should the constructor fill in the variables for my function. As in not just how to make it do what I want it to do, but why does it make sense to the program? I started studying this year. Thank you. This is a code to be compiled on a GBA emulator although the problem I have is purely from the perspective of C++. I have outlined the additional comments for this post in the program in bold. How I understand what I'm trying to do so far is:

I create a constructor which then gets the variable values for the function within in it from the main loop later in the program where I initialize the class objects for the first time and then in the part where I redraw the box for the movement I simply call upon the class objects which should have their initial values stored from the constructor.

#include <stdint.h>
#include <stdlib.h>
#include "gba.h"

// A class with variables.
class CHARBOX
{
    public:
    int init_;
    int str_;
    int health_;
    int posx_;
    int posy_;
    int width_;
    int height_;
    int colour_; // someone advised me to name my class variables 
                 // with a special symbol attached.

    public:
// This is probably the part where I have not done things right. When this is 
// compiling, there is an error saying that there is no matching function to 
// call CHARBOX. Which I don`t understand completely.
        // Constructor.
    CHARBOX(int posx, int posy, int width, int height, int colour)
    {
        DrawBox(posx, posy, width, height, colour);
    }

    // Drawing functions.
    void DrawBox(int posx_, int posy_, int width_, int height_, int colour_)
    {
        for (int x = posx_; x < posx_ + width_; x++)
        {
            for (int y = posy_; y < posy_ + height_; y++)
            {
                PlotPixel8(x, y, colour_);
            }
        }
    }
};

// The entry point.
int main()
{
    // Put the display into bitmap mode 4, and enable background 2.
    REG_DISPCNT = MODE4 | BG2_ENABLE;

    // Defining some colour palettes.
    SetPaletteBG(1, RGB(90,0,0));
    SetPaletteBG(2, RGB(0,90,0));
    SetPaletteBG(3, RGB(0,0,90));
    SetPaletteBG(4, RGB(90,90,0));
    SetPaletteBG(5, RGB(90,0,90));

//Here is where the objects get initialized and the constructor is called.
        // Draw the player at a starting location.
    CHARBOX player(10, 24, 6, 8, 1);

    // Draw the enemy at a starting location.
    CHARBOX enemy(80, 24, 6, 8, 2);

// main loop.
    while (true);
    {
        // Clear screen and paint background.
        ClearScreen8(1);

        // Flip buffers to smoothen the drawing.
        void FlipBuffers();

        // Redraw the player.
        if ((REG_KEYINPUT & KEY_LEFT) == 0)
        {
            player.DrawBox(); // This is where the object gets called 
                                      // again to be redrawn.
            posx_--;
        }

        if ((REG_KEYINPUT & KEY_RIGHT) == 0)
        {
            player.DrawBox();
            posx_++;
        }

        if ((REG_KEYINPUT & KEY_UP) == 0)
        {
            player.DrawBox();
            posy_--;
        }

        if ((REG_KEYINPUT & KEY_DOWN) == 0)
        {
            player.DrawBox();
            posy_++;
        }
    WaitVSync();
    }
return 0;
}
1

There are 1 answers

8
Alok Save On BEST ANSWER

You need to use a member initialization list to initialize your class members:

CHARBOX(int posx, int posy, int width, int height, int colour):posx_(posx),posy_(posy),width_(width),height_(height), colour_(colour)
{

}

Good Read:
What is this weird colon-member (" : ") syntax in the constructor?

someone advised me to name my class variables with a special symbol attached.

That is so that you can distinguish between the member variable name and the passed function argument name. It is not a necessity you can simply choose different names and it should be just fine.

why does it make sense to the program?

Constructor in C++ is a special member function which gets called whenever an object of the class is created. The purpose of the constructor is to provide an opportunity to properly initialize the members of the object. for e.x: width_, height_ etc in your case.

Once the object is constructed, the class members are assumed to be in valid and determinate state so that they can be used by the program. In your case unless you initialize the members in the constructor they will have indeterminate values i.e: any random values. You don't really want a member function to get the width_ and it to return a garbage value. So you need to initialize them.