Classes in separate files in C++?

I have copied this out of a book. I'm just not sure what to add in the main.cpp source file to make it run though.

I know that class declarations go in the .h file and implementations go in the .cpp file. What would I need to write in main.cpp?

I've tried lots of different things but I'm just getting so many error messages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 // cat.h
#ifndef ____2_cat_implementation__Cat__
#define ____2_cat_implementation__Cat__

#include <iostream>
using namespace std;
class Cat
{
public:
Cat (int initialAge);
~Cat();
int GetAge() { return itsAge;}              
void SetAge (int age) { itsAge = age;}      
void Meow() { cout << "Meow.\n";}          
private: int itsAge;
};

#endif /* defined(____2_cat_implementation__Cat__) */ 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;

Cat::Cat(int initialAge) 
{
itsAge = initialAge;
}

Cat::~Cat() 
{

}

int main()
{
    Cat Frisky(5);
    Frisky.Meow();
    cout << "Frisky is a cat who is ";
    cout << Frisky.GetAge() << " years old.\n";
    Frisky.Meow();
    Frisky.SetAge(7);
    cout << "Now Frisky is " ;
    cout << Frisky.GetAge() << " years old.\n";
    return 0;
}
You almost have it - the main() function could go in a main.cpp file, or a file named after the application name.

However try not to put inline functions in the header file. The definitions for getAge, setAge & Meow should also go in cat.cpp

Hope this helps

Edit:

IMO, you should add a default constructor that initialises ItsAge to something. What happens if getAage is called without setAge or the other constructor being done first?
Last edited on
Thanks. I didn't create the inline functions called that. They just appeared there when I created a new class and I wasn't really sure if I needed them or not.
> What happens if getAage is called without setAge or the other constructor being done first?
¿how is that possible?


> I've tried lots of different things but I'm just getting so many error messages.
If you get an error, you are doing something wrong.
If you don't say what error you've got, it's hard to help.

By the way, you should not using namespace in headers, it pollutes everything.
The definitions for getAge, setAge & Meow should also go in cat.cpp http://www.hqew.net

Last edited on
@ne555

>
What happens if getAage is called without setAge or the other constructor being done first?
¿how is that possible?


I found this, the reply about half way down by nobar seemed the best answer for me.

http://stackoverflow.com/questions/563221/is-there-an-implicit-default-constructor-in-c



When the object is created like this:

Cat Frisky;

That calls the implicit default constructor, which doesn't initialise any member variables. So the member variable values are not defined.

However, If the object is created like this:

Cat Frisky();

Apparently the member variables are zero initialised.

Even better advice:

Scott Meyers, Effective C++, item 4 wrote:

The best way to deal with this seemingly indeterminate state of affairs is to always initialise your objects before you use them.


@anyone

Scott also recommends always providing your own copy constructor, copy assignment operator, and destructor - even if they are empty. The reasons why are a whole new kettle of krawldads.

I would strongly recommend Scott's book, but it would be quite tough for a beginner, because one needs to understand all the different aspects of C++ first, otherwise much of it won't make any sense.

My point is that there is no default constructor in `Cat', so Cat Frisky; will not compile.
Also, Cat Frisky(); is a function declaration.

> Scott also recommends always providing your own copy constructor, copy assignment operator,
> and destructor - even if they are empty
It would be really weird for the copy constructor or copy assignment to be empty.
It is error prone to define the copy constructor that does the same as the one the compiler provides (till c++11 where you could simply = default)

¿where did you get that idea?
Last edited on
Topic archived. No new replies allowed.