473,466 Members | 1,298 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

.cpp question

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.

Aug 17 '07 #1
8 1393
Roger wrote:
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.
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.

Aug 17 '07 #2
"Roger" <ro******@gmail.comwrote in message
news:11*********************@j4g2000prf.googlegrou ps.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.

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.
Aug 17 '07 #3
On 17 Aug., 02:26, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Roger" <roger...@gmail.comwrote in message

news:11*********************@j4g2000prf.googlegrou ps.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.

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.

Aug 17 '07 #4
On Aug 16, 5:26 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Roger" <roger...@gmail.comwrote in message

news:11*********************@j4g2000prf.googlegrou ps.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.

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

Thank you. :)

Aug 17 '07 #5
On Aug 16, 5:51 pm, kamistr...@gmx.de wrote:
On 17 Aug., 02:26, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Roger" <roger...@gmail.comwrote in message
news:11*********************@j4g2000prf.googlegrou ps.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.
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.
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.

Aug 17 '07 #6
ka********@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.
Aug 17 '07 #7
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_na me);
}
puts("\n\n_________\npress <enterto 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!

;^)

Aug 17 '07 #8
Jim Langston wrote:
"Roger" <ro******@gmail.comwrote in message
news:11*********************@j4g2000prf.googlegrou ps.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
Aug 17 '07 #9

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

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...

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.