473,789 Members | 2,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Separating a class definition into header and source files

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:publi c Parent
{
public:
Something(int a_, double b_){a = a_;b=b_;}
int get_a()const{re turn a;}
int get_b()const{re turn b;}
private:
int a;
double b;
};

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

Something::Some thing(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

Jul 23 '05 #1
4 4184
ur first example is not even legal code........... try this link

http://www.simplesql.org/java2cpp.html

Jul 23 '05 #2
ra************* @gmail.com wrote:
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::Some thing(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::Some thing(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.
Jul 23 '05 #3

<ra************ *@gmail.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
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:publi c Parent
{
public:
Something(int a_, double b_){a = a_;b=b_;}
int get_a()const{re turn a;}
int get_b()const{re turn b;}
private:
int a;
double b;
};

I want to convert it into the following header and source files:
something.h:
class Something:publi c 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(SOMETH ING_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::Some thing(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::Some thing(int a_, double b_) : Parent(), 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

Jul 23 '05 #4

"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message >
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.


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

:-)

-Howard

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4652
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
28
2023
by: Steven T. Hatton | last post by:
I will assume many people reading this would never create anything similar to the example below. So let me preface this with _*IF*_ you were in a situation where you had to chose between using #includes or forward declaring each class in diamond.h, which would you choose? Why? If there is something fundamentally wrong with the way I've approached the structure of this example, I am interested to know. As for preferences and tastes, I...
12
3257
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always private. I have placed "friend void CA::Run_A(void)" in CMain Class. CMain::Run() function attempts to execute CA::Run_A(), but compiler shows an error saying that it is the violation to access private function. I don't understand why because friend...
7
12968
by: A_StClaire_ | last post by:
hi, I'm working on a project spanning five .cpp files. each file was used to define a class. the first has my Main and an #include for each of the other files. problem is my third file needs to access the class defined in my second file and I can't figure out how to work this right. if I use an #include in my third file, my Main gives me a compile-time class redefinition error. if I don't, the third file can't "see" the second
11
1478
by: Egbert Nierop \(MVP for IIS\) | last post by:
Hi, Why do I need to define a function as static (inside a .h file) to avoid the following linker error? CSession error LNK2005: "void __stdcall AllocString(unsigned short * *,unsigned int)" (?AllocString@@YGXPAPAGI@Z) already defined in stdafx.obj I know that the MSDN talks about this, but not why it is needed. ms-help://MS.MSDNQTR.2005APR.1033/vccore/html/LNK2005.htm
1
1179
by: AnkitAsDeveloper [Ankit] | last post by:
As all we know, in order to remove cyclic includes in C++ we seperate the declarations and definitions of classs and it's member in two files Header (*.h) and source files (*.cpp). This is not a problem for C# as there is no concept of include. I faced problems for seperating declarations and definitions for class when it contains properties. This can also be done in context of struct/structures. Here is how we can. Class A
2
1687
by: tony | last post by:
Hello! I'm trying to build a class library which has a class called AvestaPlantFunc. In this project building a class libray exist a class called AvestaPlantFunc. In this class is there a method called IsBottomMixValid. Code not relevant for the question has been removed. This method IsBottomMixValid has one parameter and the type for this parameter is MeltPracDataGmix meaning passing class reference MeltPracDataGmix.
14
2203
by: aaragon | last post by:
Hi everyone, I've been writing some code and so far I have all the code written in the .h files in order to avoid the linker errors. I'm using templates. I wanted to move the implementations to the .cpp files. After some reading, I found that the only way to do this is to add the actual template instantiations in the .cpp file. But, how do you do that if you're not working with built-in types? For example, a template class might be,
15
7873
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.