Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

.cpp question

2 views
Skip to first unread message

Roger

unread,
Aug 16, 2007, 8:14:57 PM8/16/07
to
Ok, I know in Java we can seperate class files so that we could use
them later on.

I am wondering if I could do the same for C++... Perhaps have a
main.cpp and lets say for example function.cpp...

I know you can do this with header files from what I have read.

If it can be done, can anyone show/tell me? Thanks.

Gianni Mariani

unread,
Aug 16, 2007, 8:24:49 PM8/16/07
to

Yes, it can be done, every compiler I know of lets you do this.

Look at the pre-processor directive -
'#include <thing>'
or
'#include "file"'

Look up the use of "header file".

Almost every open-source CPP application/library will have lots of
examples of how to define interfaces in header files and separate the
code into a library or multiple files.

Jim Langston

unread,
Aug 16, 2007, 8:26:35 PM8/16/07
to
"Roger" <roge...@gmail.com> wrote in message
news:1187309697....@j4g2000prf.googlegroups.com...

That's the way it's normally done.

MyClass.h
class MyClass
{
public:
int Foo()
bool Bar()
}

MyClass.cpp
int MyClass::Foo()
{
return 1;
}

bool MyClass::Bar()
{
return true;
}

MyProg.cpp
#include "MyClass.h"

int main
{
MyClass MyInstance;
}

Just make sure you compile MyClass.cpp along with MyProg.cpp and have the
linker link the resulting object files.


kamis...@gmx.de

unread,
Aug 16, 2007, 8:51:59 PM8/16/07
to
On 17 Aug., 02:26, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "Roger" <roger...@gmail.com> wrote in message

Furthermore headerfiles are often intersected with some additional pre-
processor directives:

MyClass.h:

#IFNDEF MYCLASS_H
#DEFINE MYCLASS_H

class MyClass
{
public:
int Foo()
bool Bar()

}

#ENDIF

using these directives prevents that the declaration of your class is
read by your compiler multiple times,
if you are including the header file in more than one file in your
project, so you won`t get an error.

Roger

unread,
Aug 16, 2007, 8:57:50 PM8/16/07
to
On Aug 16, 5:26 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "Roger" <roger...@gmail.com> wrote in message

Thanks! I have a bit more understanding of this now. I think with a
little more practicing, I will get it.

Thank you. :)

Roger

unread,
Aug 16, 2007, 9:00:43 PM8/16/07
to

I took a look at some source code of various projects, and I did see
that those additional preprocessor directives. :) Thanks for the heads
up.

red floyd

unread,
Aug 16, 2007, 9:29:15 PM8/16/07
to
kamis...@gmx.de wrote:

> Furthermore headerfiles are often intersected with some additional pre-
> processor directives:
>
> MyClass.h:
>
> #IFNDEF MYCLASS_H

#ifndef MYCLASS_H
> #DEFINE MYCLASS_H
#define MYCLASS_H


>
> class MyClass
> {
> public:
> int Foo()
> bool Bar()
>
> }
>
> #ENDIF

#endif
>

#ifndef, #define, and #endif are *LOWER CASE*

Their usage in this case is known as "include guards". Google for it,
or search this group, or both.

Chris Thomasson

unread,
Aug 16, 2007, 10:28:53 PM8/16/07
to
Here is a minimalist approach for fairly fine-grain abstraction of different
versions of ones pre-arranged libraries:

----------------
#include <cstdio>


/* per-api-child version 001
____________________________________________________*/
namespace yourlibsys_root {
namespace api_parent {
namespace api_child {
namespace v001 {
class handle;
namespace constant {
static char const c_lib_name[] = "api_child::v001";
}
class handle {};
}}}}


/* per-api-child version 002
____________________________________________________*/
namespace yourlibsys_root {
namespace api_parent {
namespace api_child {
namespace v002 {
class handle;
namespace constant {
static char const c_lib_name[] = "api_child::v002";
}
class handle {};
}}}}


/* abstract selected master api version children
____________________________________________________*/
namespace yourlib {
namespace api_parent {
// switch version numbers for simplistic abstraction
namespace api_child = yourlibsys_root::
api_parent::api_child::v002;
//api_parent::api_child::v001;
}
}


/* Entry
____________________________________________________*/
int main(void) {
{
printf("%s\n", yourlib::api_parent::api_child::constant::c_lib_name);
}
puts("\n\n_________\npress <enter> to exit...\n");
return getchar();
}


-------------------

separate your api namespace into macro and micro sections and use aliasing
or 'using namespace' directives to facilitate all the abstraction detail.
Force things into a macro api to micro-api flow and you can have fine-grain
control over your overall build environment wrt selecting abstractions on a
per-api_child/api_micro basis. You can even resort to using some macros to
give the _user_ the "ability to mutate your libraries versioning
detection/abstraction/reaction infrastructure"... You can use namespace
tricks to provide yourself with a fairly coherent build system with the
standard; well, might at least!

;^)

Robert Bauck Hamar

unread,
Aug 17, 2007, 3:08:53 AM8/17/07
to
Jim Langston wrote:
> "Roger" <roge...@gmail.com> wrote in message
> news:1187309697....@j4g2000prf.googlegroups.com...
>> Ok, I know in Java we can seperate class files so that we could use
>> them later on.
>>
>> I am wondering if I could do the same for C++... Perhaps have a
>> main.cpp and lets say for example function.cpp...
>>
>> I know you can do this with header files from what I have read.
>>
>> If it can be done, can anyone show/tell me? Thanks.
>
> That's the way it's normally done.

To be picky, there are some errors. Also, I demonstrate how to create a
namespace.

>
> MyClass.h

//include guards:
#ifndef MYCLASS_H
#define MYCLASS_H

//Put the library in a namespace
namespace MyLib {

> class MyClass
> {
> public:
> int Foo()

; // added the missing ;
> bool Bar()
; // ditto;
> }
; // and here too.

} // end of namespace

#endif //end include guard

>
> MyClass.cpp

#include "MyClass.h"

namespace MyLib {

> int MyClass::Foo()
> {
> return 1;
> }
>
> bool MyClass::Bar()
> {
> return true;
> }

} // end namespace

>
> MyProg.cpp
> #include "MyClass.h"
>
> int main
> {
> MyClass MyInstance;

with the addition of namespaces, this will be:
MyLib::MyClass MyInstance;

> }
>
> Just make sure you compile MyClass.cpp along with MyProg.cpp and have the
> linker link the resulting object files.

How to actually do this, however, is not topical in this group.

--
rbh

0 new messages