Separating a class definition into header and source files

R

rajkirangrandhi

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
 
E

E. Robert Tisdale

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
from the class body separate the definitions of the member functions

That may be a good idea.
and put it in a separate file.
Why?

I was wondering if there is some utility which does that.

A text editor.
Assuming I have the following class:

class Something: public Parent {
private:
int a;
double b;
public:
Something(int aa, double bb): a(aa), b(bb) { }
int get_a(void) const { return a; }
double get_b(void) const { return b; }
};

I want to convert it into the following header and source files:
something.h:

#ifndef GUARD_SOMETHING_H
#define GUARD_SOMETHING_H 1

#include "parent.h"
class Something: public Parent {
private:
int a;
double b;
public:
Something(int a_, double b_);
int get_a(void) const;
double get_b(void) const;
};


#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
something.cpp:

// This implements *external* function definitions.
#undef EFINE_INLINE
#include "something.h"
Something::Something(int aa, double bb): a(aa), b(bb) { }
int Something::get_a(void) const { return a; }
double Something::get_b(void) const { return b; }

Is there some way to do this?
Yes.

cat main.cpp
#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:
g++ -Wall -ansi -pedantic -O2 -o main main.cpp something.cpp
./main
s.get_a() = 13

while developing, testing and debugging an application
then recompile it like this:
g++ -DEFINE_INLINE -Wall -ansi -pedantic -O2 \ -o main main.cpp something.cpp
./main s.get_a() = 13

just before you release and distribute the application.
 
C

codigo

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;
};

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_)
--------------------------------
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;}

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;
}

-------------------------------------------------
 
H

Howard

E. Robert Tisdale said:
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.

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

:)

-Howard
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top