-
Beginning Classes
I started learning c++ a few weeks ago, and now I got to the chapter with Basic Classes. I kind of understood the whole chapter, but when I tried to make my own program using classes, it didn't work. So I tried to copy one from the book. It doesn't work also. Can someone tell me whats wrong with this program?
Ps. I just started learning c++, so please don't use those advanced methods. Thank You.
Here's the program:
#include <iostream.h>
#include <stdlib.h>
class cat
{
public: // begins public section
int getAge();
void setAge(int age);
void meow();
private: // begins private section
int itsAge();
};
// GetAge, public accesor function
// returns value of itsAge member
int cat::getAge()
{
return itsAge;
}
// definition of setAge, public
// accesor dunction
// returns sets itsAge member
void cat::setAge(int age)
{
// set member variable its age to
// value passed in by parameter age
itsAge = age;
}
//definition of Meow method
// returns : void
// parameters : none
// action : Prints "meow" to screen
void cat::meow()
{
cout << "Meow.\n";
}
// Create a cat, set its age, have it
// meow, tell us its age, then move again.
int main()
{
cat Frisky;
Frisky.setAge(5);
Frisky.meow();
cout << "Frisky is a cat who is " ;
cout << Frisky.getAge() << " years old.\n";
Frisky.meow();
system("PAUSE");
return 0;
}
-
Change this to:It's not a function, it's only a variable. Also, you should prefix member variables in some way for readability, although Bruce Eckel (author of the wonderful Thinking in C++, available online) disagrees. In this case though, he can go and boil his head :)
I have the convention of m_iAge for things like this (m_ for member, i for int).
-
Oh BTW, use [code][/code] tags to preserve the indentation which you *did* use, didn't you ;)
-
You should separate the code into a .cpp file and a .h file.
Also add a basic constructor cat().
When you write this in main, "cat Frisky;" you're using a constructor which you havn't included. So C++ uses a default
constructor.
The header file cat.h
class cat
{
public:
cat(); // Simple constructor
cat(int age); // Another constructor
int getAge();
void setAge(int age);
void meow();
private:
int itsAge;
};
The implementation file cat.cpp
#include "cat.h"
// The constroctor
cat::cat()
{ // The constructor lets you set initial values.
itsAge = 0;
}
//Another constructor
cat::cat(int age)
{
itsAge = age; // In main() you can write "Cat Felix(5);"
}
int cat::getAge()
{
return itsAge;
}
void cat::setAge(int age)
{
itsAge = age;
}
void cat::meow()
{
cout << "Meow.\n";
}
And finally your main() function in its own file main.cpp
#include <iostream.h>
#include <stdlib.h>
int main()
{
cat Frisky;
cat Felix(4);
Frisky.setAge(5);
Frisky.meow();
Felix.meow();
cout << "Frisky is a cat who is " ;
cout << Frisky.getAge() << " years old.\n";
cout << "Felix is a cat who is " ;
cout << Felix.getAge() << " years old.\n";
system("PAUSE");
return 0;
}
//Joachim
PS: If you're using a non microsoft compiler such as GNU you should change the .cpp extension to .cc instead
-
You can still use .cpp for gcc.
-
I hate separating my files into .h and .cpp. If i do, i cant add the cpp implementation files to my project, and its a PAIN to go an open one if i need it. So, i just put the implementation right into the header. No problems.
Z.
-
Why can't you add the .cpp files to your project? It's bad form to include code in a header file (immoral, not illegal).
-
Because then MS tries to compile them, and then tells me that "Blah Blah Blah function already has a body...", or some kind of redefinition error.
So, im immoral, whats yer point =)?
Z.
-
I have never had that problem :confused:
If you use header guarding, and put all the implementation into the .cpp file then there shouldn't be anything wrong.
And yep, you're immoral :D j/k
-
I have a question, how is this:
Code:
class Cat
{
public:
int getAge() {return age;}
private:
int age;
};
different from this:
Code:
class Cat
{
public:
int getAge();
private:
int age;
};
int Cat::getAge()
{
return age;
}
?
-
The first one has everything inlined into the definition (this is different from compiler inlining). The object code generated in both cases will be identical. The second way just means you can split into a .h/.cpp pair to prevent needless recompilation of unchanged code.