|
-
Jun 28th, 2001, 06:16 PM
#1
Thread Starter
Member
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;
}
-
Jun 28th, 2001, 06:25 PM
#2
Monday Morning Lunatic
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).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 28th, 2001, 06:30 PM
#3
Monday Morning Lunatic
Oh BTW, use [code][/code] tags to preserve the indentation which you *did* use, didn't you
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 2nd, 2001, 05:37 AM
#4
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
-
Jul 2nd, 2001, 05:39 AM
#5
Monday Morning Lunatic
You can still use .cpp for gcc.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 2nd, 2001, 02:14 PM
#6
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.
-
Jul 2nd, 2001, 03:34 PM
#7
Monday Morning Lunatic
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).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 2nd, 2001, 03:49 PM
#8
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.
-
Jul 2nd, 2001, 03:58 PM
#9
Monday Morning Lunatic
I have never had that problem 
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 j/k
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 2nd, 2001, 04:23 PM
#10
Fanatic Member
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;
}
?
Alcohol & calculus don't mix.
Never drink & derive.
-
Jul 2nd, 2001, 04:27 PM
#11
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|