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.
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.
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.
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.
Thanks! I have a bit more understanding of this now. I think with a
little more practicing, I will get it.
Thank you. :)
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.
> 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.
----------------
#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!
;^)
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