Separating a class definition into header and source files

Discussion in 'C++' started by rajkirangrandhi, Mar 18, 2005.

  1. Hi,
    I have several classes written in Java that I am trying to convert into
    C++. They are fairly simple classes, no gui stuff etc, just some math
    routines. I was able to do it without any problem. However I wish to
    separate the definitions of the member functions from the class body
    and put it in a separate file. I was wondering if there is some utility
    which does that. Assuming I have the following class:

    class Something:public Parent
    {
    public:
    Something(int a_, double b_){a = a_;b=b_;}
    int get_a()const{return a;}
    int get_b()const{return b;}
    private:
    int a;
    double b;
    };

    I want to convert it into the following header and source files:
    something.h:
    class Something:public Parent
    {
    public:
    Something(int a_, double b_);
    int get_a()const;
    int get_b()const;
    private:
    int a;
    double b;
    };
    something.cpp:

    Something::Something(int a_, double b_){a = a_;b=b_;}
    int Something::get_a()const{return a;}
    int Something::get_b()const{return b;}

    Is there some way to do this?
    Thanks,
    Rajkiran
     
    rajkirangrandhi, Mar 18, 2005
    #1
    1. Advertisements

  2. rajkirangrandhi

    rajkumar Guest

    rajkumar, Mar 18, 2005
    #2
    1. Advertisements

  3. That may be a good idea.
    A text editor.
    #ifndef GUARD_SOMETHING_H
    #define GUARD_SOMETHING_H 1

    #include "parent.h"

    #ifdef EFINE_INLINE
    // This separates implementation from interface.
    inline
    Something::Something(int aa, double bb): a(aa), b(bb) { }
    inline
    int Something::get_a(void) const { return a; }
    inline
    double Something::get_b(void) const { return b; }
    #endif//EFINE_INLINE

    #endif//GUARD_SOMETHING_H
    // This implements *external* function definitions.
    #undef EFINE_INLINE
    #include "something.h"
    #include "something.h"
    #include <iostream>

    int main(int argc, char* argv[]) {
    const
    Something s(13, 33.0);
    std::cout << "s.get_a() = "
    << s.get_a() << std::endl;
    return 0;
    }

    You might compile it like this:
    s.get_a() = 13

    while developing, testing and debugging an application
    then recompile it like this:
    just before you release and distribute the application.
     
    E. Robert Tisdale, Mar 18, 2005
    #3
  4. rajkirangrandhi

    codigo Guest

    Best not to use a "utility". Learn the language instead.
    Provide include guards for your declaration header.
    I'm guessing that the "Parent" base class is declared in parent.h
    ---------------------------- something.h --
    #if !defined(SOMETHING_H_)
    #define SOMETHING_H_

    #include "parent.h"

    class Something : public Parent
    {
    public:
    Something(int a_, double b_);
    int get_a() const;
    int get_b() const;
    private:
    int a;
    double b;
    };

    #endif // (SOMETHING_H_)
    --------------------------------
    Base and members a, b are initialized in an initialization list. A feature
    not found in Java.
    ------------------------------ something.cpp ----
    #include "something.h"

    Something::Something(int a_, double b_) : Parent(), a(a_), b(b_)
    {
    }

    int Something::get_a() const
    {
    return a;
    }

    int Something::get_b() const
    {
    return b;
    }

    -------------------------------------------------
     
    codigo, Mar 18, 2005
    #4
  5. rajkirangrandhi

    Howard Guest

    Then again, he might *not* compile it like that. Especially if he has VC++,
    or CodeWarrior, or...

    :)

    -Howard
     
    Howard, Mar 18, 2005
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.