Class Help - Cannot define data members

I'm playing around with some basic C++ and I'm having trouble with a class I'm writing. Just about everything works fine, but when I create the Cat class the constructors don't define the data members and it is just empty values where they should be.

Here is my code:

Cat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "stdafx.h"
#include <string>
using namespace std;

#ifndef CAT
#define CAT

class Cat
{
public:
	Cat(string name, int age):
		m_Name(name),
		m_Age(age) {};
    string GetName() const { return m_Name; }
    int GetAge() const { return m_Age; }
	void Meow() const { cout << "Meow!"; }
private:
    string m_Name;
    int m_Age;      
};
#endif 


main.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "stdafx.h"
#include <iostream>
#include <string>
#include "cat.cpp"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    // Create 2 cats
    Cat jack("Jack", 1);
    Cat max("Max", 1);
    
    // Have each meow
    jack.GetName();
    cout << " says ";
    jack.Meow();
    cout << endl;
    max.GetName();
    cout << " says ";
    max.Meow();
    cout << "\n\n";
    
    // Have each cat say their names and ages
    cout << "Hi, my name is ";
    jack.GetName();
    cout << " and I am ";
    jack.GetAge();
    cout << " years old!" << endl;
    cout << "Hi, my name is ";
    max.GetName();
    cout << " and I am ";
    max.GetAge();
    cout << " years old!\n\n";
    
    // Have each cat meow again and say goodbye
    jack.GetName();
    cout << " says ";
    jack.Meow();
    cout << " and goodbye.\n";
    max.GetName();
    cout << " says ";
    max.Meow();
    cout << " and goodbye.\n\n";

    system("pause");
    return 0;   
}


Thanks in advance.
but when I create the Cat class the constructors don't define the data members and it is just empty values where they should be.
You mean initialize.

the functions GetName() and GetAge() just return the values as they're supposed to do. So they don't print them to the screen. To print them you need cout like so:
1
2
    cout << "Hi, my name is ";
    cout << jack.GetName();

Thanks. I guess I changed the code earlier so it would just cout on the function and forgot to change it.
Topic archived. No new replies allowed.