Can't figure out how to implement classes in codeblocks (Learning from Sams Teach yourself C++ one hour a day)

Hi everyone,

I'm new to this forum, so I thought I should introduce myself in order for you to better understand my situation. I'm a finance student spending my vacation trying to learn C++. To this occasion I bought a copy of Sams "Teach Yourself C++ in one hour a day (6th ed.)", where I am currently at Lesson 10: Classes and objects. So as you might guess, all my knowledge about programming is limited to the contents of those previous chapters from that book. Also, I am using Code::Blocks 10.05 for all the excercises in the book.

My Problem:
Throughout the book I try to write and run all the sample programs (or listings as they are referred to in the book) in order to get used to how code is written. In this chapter (Lesson: 10) there are two listings (10.5 and 10.6) where one is supposed to call a class, and it is really poorly described how to execute this in practice.

Input

Listing 10.5 - Cat Class Declaration in Cat.h

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


Listing 10.6 Cat implementation in Cat.cpp
#include "Cat.h"

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

Cat::~Cat()
{
}

int main()
{
Cat Frisky(5);
Frisky.Meow();
std::cout << "Frisky is a cat who is ";
std::cout << Frisky.GetAge() << " years old.\n";
Frisky.Meow();
Frisky.SetAge(7);
std::cout << "Now Frisky is ";
std::cout << Frisky.GetAge() << " years old.\n"
return 0;
}

Output

Meow.
Frisky is a cat who is 5 years old.
Meow.
Now Frisky is 7 years old.


So I guess my question really is: How do i create those two programs in codeblocks, and make them interact in the way that produces the desired output listed above? I have tried to find answers to this on the web, but the explanations are to general for my limited understanding of the subject matter.

Hope to hear from some of you, and I look forward to spending more time here as I am hellbent on becoming proficient in this fascinating subject over time.

Best regards,
Loken
The problem may be in your perception, these are NOT two seperate programs. They are only two seperate files and when you compile them you will get one program.

Before being compiled the two files MUST be in the same folder, you should have all of your projects organized into their own folders by the way, then you enter the data pretty much how you have written here, save then compile and run. Are you getting an error message? If so please copy and paste it in your next post.
You definitely shouldn't be reading that book. It's not just bad, it's beyond bad.
See here for a thread about books:
http://www.cplusplus.com/forum/lounge/44380/

Another two good books that aren't listed in that thread are "The C++ Primer" and "Accelerated C++".
@Computergeek01: Hi, thanks for the reply! I think I understand what you mean, but I am not familiar enough with these concepts to execute it. What I have done so far for all the exercises is to create them as seperate projects in respective folders, and this have worked out so far. What I am wondering is specifically what to do, what file to create it as etc. Would really appreciate help with this part.

@Athar: Thanks for the reply! I have actually been pretty pleased with this book so far, but I suppose I'm not really qualified enough to make an informed judgement. :p What specifically are the pittfalls with the book I am using? Are any of those books in your link readable for a person with zero programming experience (before this)?

Best regards,
Loken
There are some problems with the book.
An important component of C++ is the Standard Template Library (STL) which contains various container types (strings, vectors, etc.) and a set of standard algorithms for generic programming. You should be learning to use it starting from day one, however the only mention of STL is at the end of lesson 23, where the author spares a few lines saying that there's "the STL which is an alternative (sic) to keeping on reinventing the wheel [...] and it's even free!" Yeah, so much for the STL in a book that claims to teach C++.

The other parts of the C++ standard library other than iostream are also left out. std::swap? Apparently he hasn't heard of it, as he keeps defining his own variant in various code examples.

The bool type? Nope. We keep seeing typedef int BOOL; and enum BOOL {FALSE,TRUE}; in the examples.

The author also keeps using bad examples and/or does not explain them. Somewhere around lesson 10 there's a code example where he mentions that it's possible to "allocate class member member variables using new and delete in the destructor" then gives a different version of a previously used Cat class which now allocates its age and weight dynamically.
With that, he apparently things the subject is sufficiently explained and moves on. He neither mentions that the example is nonsense nor does he explain in which situations it should be used - things like this constantly lead to questions like "what's the point of new" in this and other forums.

While we're at it: he says "one has to check the return value of new for NULL every time" and promptly does just that in his next code example. Of course, that's the only time where he actually does it and in any case, it ignores the fact that new does not return 0 on failure, but throws a std::bad_alloc exception.


Now to be fair, I have an older version of the book, so in theory he could have improved the book in later editions.
But I don't have too much hope for that, at least the code example you posted with good old Frisky is just like what I remember.


Are any of those books in your link readable for a person with zero programming experience (before this)?

Not entirely sure about all the books, but the C++ Primer should be suitable in any case.
@Athar: Thanks for your reply! I think our books are somewhat different, this is the one I have purchased:

http://www.amazon.com/Sams-Teach-Yourself-One-Hour/dp/0672329417

Two of the books five total parts seems to concern STL. Can you take a look at the table of contents and tell me if there still exists hope if I should continue with this book? :p

I am a student and can't really afford too many books additional to those I have to purchase for my courses.

Best regards,
Loken
It certainly does look better than the previous editions.
It seems to actually cover the STL and while looking through the first chapter on Amazon, I saw that bool was added to the types - and he now actually mentions that the sizes listed in the table depend on the platform and compiler used instead of just declaring them as facts.
So I suppose there is hope yet, but can't say for sure until I get my hands on an actual copy.

If money is a problem, you could take a look at Thinking in C++, which is available for free. However, it assumes some previous programming experience, so I'm not sure if it's easy enough to understand if you have no prior experience.
Figured it out! :D Thank you for your assistance :)
Topic archived. No new replies allowed.