Beginner Help: Joining Multiple classes in multiple files?

J

JHenstay

I've been doing quite alot of reading on C++ and classes, however,
everything I read just talks about the code itself and not the
location of the code.

My question is, what if you want to seperate classes into their own
CPP files, what changes are needed to the files and how do you
actually compile and build an executable.

For instance, let's say I have the following all in one file, it would
compile and link into an executable fine:

------------START contents of main.cpp START---------------------

#include <iostream.h>

class Animal
{ public:
int GetAge() { return Age;}
void SetAge(int iNewAge) {Age=iNewAge;}
private:
int Age;
};

class Cat : public Animal
{
public:
int GetLives() { return Lives;}
void SetLives(int iNewLife) {Lives=iNewLife;}
void Die() {Lives--;}
private:
int Lives;
};



int main (void)
{
Cat* Frisky = new Cat;
Frisky->SetAge(3);
Frisky->SetLives(9);

cout << "Frisky is " << Frisky->GetAge() << " years old!" << endl;
cout << "Frisky has " << Frisky->GetLives() << " lives left!" <<
endl;
cout << "Kill Frisky!" << endl;
Frisky->Die();
cout << "Frisky now has " << Frisky->GetLives() << " lives left!" <<
endl;

delete Frisky;
}

--------------END contents of main.cpp END-----------------------


Now, how do I break that up so that I have 3 files so I have:

Animal.cpp (Contains the Animal class)
Cat.cpp (Contains the Cat Class)
Main.cpp (Contains the main function of the code)

What changes do I need to make to each block of code and then do I
compile each cpp file seperately and run some kind of link command to
link all of the OBJ files together?

Any insight into how you do this would be greatly appreciated..

-JH
 
J

Jacques Labuschagne

JHenstay said:
I've been doing quite alot of reading on C++ and classes, however,
everything I read just talks about the code itself and not the
location of the code.

The basic idea is this:

// file: animal.hpp (header)
class Animal{
public:
int GetAge();
void SetAge();
private:
int Age;
};

// file: animal.cpp (implementation)
int Animal::GetAge(){
return Age;
}
void Animal::SetAge(int iNewAge){
Age = iNewAge;
}

// file: main.cpp
#include "animal.hpp"

int main(){
Animal a;
}

If you want the cats and dogs to be able to use each other, things
become a little more tricky. There's probably a section on "include
guards" in the FAQ for when you get to that point.

HTH,
Jacques
 
J

JHenstay

Jacques,
Thanks for the reply! The example I gave I think is slightly too
simple because I can get that to work fine. After I posted my message
I went back and tried some other things and the bizarre thing is, I
can get the code to compile/link fine under the free command line
Borland Compiler, but Visual C++ threw all sorts of errors. So I
posted a message to the Visual C++ group with my code asking for more
specific help.

Thanks again!

-JH
 
O

osmium

JHenstay said:
I've been doing quite alot of reading on C++ and classes, however,
everything I read just talks about the code itself and not the
location of the code.

My question is, what if you want to seperate classes into their own
CPP files, what changes are needed to the files and how do you
actually compile and build an executable.
<snip>

Most of what you want to know is specific to the mechanics of a particular
compiler. A command line oriented compiler uses "make" files, a GUI
oriented compilers may use something called a "project". Try re-posting in
a more specific group. Using one of the two hot words above might help
find something in google advanced groups so you can refine your question
before posting again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top