Beginner Help: Joining Multiple classes in multiple files?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JHenstay

    Beginner Help: Joining Multiple classes in multiple files?

    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
  • Jacques Labuschagne

    #2
    Re: Beginner Help: Joining Multiple classes in multiple files?

    JHenstay wrote:[color=blue]
    > 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.[/color]

    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

    Comment

    • osmium

      #3
      Re: Beginner Help: Joining Multiple classes in multiple files?

      JHenstay writes:
      [color=blue]
      > 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.[/color]
      <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.


      Comment

      • JHenstay

        #4
        Re: Beginner Help: Joining Multiple classes in multiple files?

        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 <jacques@clawsh rimp.com> wrote in message news:<Qk6Mb.167 04$ws.1950693@n ews02.tsnz.net> ...[color=blue]
        > JHenstay wrote:[color=green]
        > > 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.[/color]
        >
        > 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[/color]

        Comment

        Working...