1

So I am working on this assignment but my professor does not word the instructions very clearly.

I am supposed to have two .cpp files and one header file. One of the .cpp files has the main function and includes the header file. It displays a simple output and then creates an object called "Monster." So in the main .cpp file I call the default constructor and this is where I get confused from the instructions. Is the constructor and destructor supposed to be located in the header file or the other .cpp file?

My code so far is this:

Main.cpp

#include <iostream>
#include <Monster.h>
using namespace std;

int main()
{
    cout << "I am going to make a monster!\n";
    Monster boggy = Monster();
}

Monster.cpp

#include <iostream>
#include <Monster.h>
using namespace std;

class Monster
{
    Monster()
    {
        cout << "A monster is born!\n;
    }
    ~Monster()
    {
        cout << "A monster is destroyed!\n;
    }
};

Monster.h

class Monster
{
};
3
  • 1
    That entire class Monster in Monster.cpp doesn't belong there; It belongs in the header. If you want to put the member function implementation in the .cpp file you can do so, and your text should show you the syntax to do it (which also requires removing those member function bodies from the header once you make the previously mentioned move). Unrelated, Monster boggy = Monster(); is pointless. Just use Monster boggy; Commented Apr 19, 2018 at 3:55
  • You need to use #include <Monster.h> for your own header files. Commented Apr 19, 2018 at 4:00
  • @WhozCraig Unfortunately I have to use Monster boggy = Monster(); even though it is pointless. So I moved the info from the Monster.cpp to the Monster.h Commented Apr 19, 2018 at 21:38

1 Answer 1

4

Convention has it that your constructor/destructor are declared in your Monster.h file, and defined in your Monster.cpp file. Take for example a recent programming assignment I had involving a Sorted class:

Sorted.h

class Sorted {
public:
    Sorted();
    ~Sorted();
};

Sorted.cpp

#include "Sorted.h"

Sorted::Sorted() {
    // constructor code goes here
}

Sorted::~Sorted() {
    // destructor code goes here
}

You can think of it like your header file almost being directly inserted at the top of the corresponding .cpp file. All your methods and instance variables are declared in the .h file, and then told exactly what to do in the .cpp file.

Sign up to request clarification or add additional context in comments.

2 Comments

So I fixed my Monster.cpp and Monster.h as you described but in my main I have an error that says Monster::Monster() is inaccessible
Disregard that last comment. Making the header public as you did fixed it. Sorry I missed that detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.