Beginner Help: Joining Multiple classes in multiple files?

Discussion in 'C++' started by JHenstay, Jan 11, 2004.

  1. JHenstay

    JHenstay Guest

    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
    JHenstay, Jan 11, 2004
    #1
    1. Advertising

  2. JHenstay wrote:
    > 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
    Jacques Labuschagne, Jan 11, 2004
    #2
    1. Advertising

  3. JHenstay

    JHenstay Guest

    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

    Jacques Labuschagne <> wrote in message news:<Qk6Mb.16704$>...
    > JHenstay wrote:
    > > 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
    JHenstay, Jan 11, 2004
    #3
  4. JHenstay

    osmium Guest

    JHenstay writes:

    > 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.
    osmium, Jan 11, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stuart
    Replies:
    3
    Views:
    438
    Joe Smith
    Aug 19, 2004
  2. Francesco S. Carta
    Replies:
    4
    Views:
    265
    Francesco S. Carta
    Jun 11, 2010
  3. joe
    Replies:
    10
    Views:
    404
  4. Daniel
    Replies:
    2
    Views:
    275
  5. Stuart

    Help: joining 5 LARGE text files

    Stuart, Aug 19, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    101
    Joe Smith
    Sep 16, 2004
Loading...

Share This Page