Search code examples
c++classinheritanceconstructorabstract-class

Create an instance of an object C++


I have recently picked up C++ but I am having problems in some areas as I am used to Java and I am not sure if I am going about things the right way.

I have a text file which lines are structured like this

C;101;1;1;4;10;
H;102;9;0;1;8;2
C;103;9;9;2;5;
C;104;0;9;3;8;

; being the delimeter

C is used to check if its a Crawler(H is used to check if it is a Hopper), it is then followed by the id, positions, directions(1-4), size, and alive(0/1)

I have an abstract Bug class then I have a Crawler and Hopper class which both inherit from the Bug class.

Here is my Bug class

class Bug
{
public:
    int id;
    pair<int, int> position;
    int direction;
    int size;
    bool alive;
    list<pair<int, int>> path;
    virtual void move() {}
    bool isWayBlocked() {}


    Bug(string type, int id, pair <int, int> position, int direction, int size, bool alive)
    {
        type = type;
        id = id;
        position = position;
        direction = direction;
        size = size;
        alive = alive;
    };
};

Here is the Crawler class

class Crawler : public Bug
{
public:
    Crawler(int id, pair <int, int> position, int direction, int size)
        : Bug("Crawler", id, position, direction, size, alive)
    {
    }



};

Here is an example of me trying to create an instance of a Crawler object:

Bug* bugPtr;
        if (bugType == "C") {
            bugPtr = new Crawler(bugID, { xPos,yPos }, direction, size);
            //Crawler* CrawlerBugPointer = new Crawler(bugID, { xPos,yPos }, direction, size);
        }

I am able to easily read from the text file and store the variables but when i start debugging all of the fields in the bugPtr say 0, I suspect that I have gone about things in completely the wrong way and have not built the Abstract class and Inheritor classes properly, could anyone point out what I am doing wrong? any help will be much appreciated.


Solution

  • The compiler should have generated a warning, "Expression with possibly no effect" (or similar), for every line in your constructor. Read the warnings - they're useful.

    Bug(string type, int id, pair <int, int> position, int direction, int size, bool alive)
        {
            this->type = type;
            this->id = id;
            this->position = position;
            this->direction = direction;
            this->size = size;
            this->alive = alive;
        };
    

    While C++ doesn't require the ridiculous use of this everywhere, in your case it's necessary since you've given the constructor parameters the same names as the fields. When you enter something like

    id = id;
    

    You're saying, "Find the variable with name id in this scope, and assign its value to itself." Which does nothing, which is why all of your fields were left at their default values. Saying this.id means, "Find the class field with name id and assign the value of the local id to it."