473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Where to place definations and declerations ?

Created a class , which is declared within a namespace .

namespace mySpace {
class X {
/* members */
}

This class task is to be used by another class Y , 'using namespace
mySpace' directive.

I have currently placed the above code containing class X in a header
file x.h and included in y.c. But not sure as to where the
declarations and definations should be placed ? Do i place the
declarations in x.h and definations in x.c , and then just include x.h
.. Not sure about the big picture.

thanks in advance,
vivekian

Nov 25 '05 #1
5 1339

You can write the definitions of the class X in the x.h file itself.

Any way it's a good practice to have the declerations in the .h file
and the definitions in the .cpp/.c file.

Ex.:
------------------------------------------------------------------------------------
x.h
******************
namespace mySpace {
class X {
/* declerations of members */
};
}
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
y.cpp
***************************
namespace mySpace{
class X {
/* definitions of members */
};
using namespace mySpace;
class y{
/* Use the objects of class X*/
};
------------------------------------------------------------------------------------

Hope this can be useful.

Nov 25 '05 #2
vivekian wrote:
Created a class , which is declared within a namespace .

namespace mySpace {
class X {
/* members */
}

This class task is to be used by another class Y , 'using namespace
mySpace' directive.

I have currently placed the above code containing class X in a header
file x.h and included in y.c. But not sure as to where the
declarations and definations should be placed ? Do i place the
declarations in x.h and definations in x.c , and then just include x.h
. Not sure about the big picture.


Yes, declare the public interface in x.h, and provide the
implementation in x.c(pp). Then,

a. forward-declare X in y.h and include x.h in y.c if X is used in the
interface of Y, or
b. include x.h in y.h if X is used in the implementation of Y

In either case, I recommend scoping X with mySpace (i.e., specifying
mySpace::X rather than 'using namespace mySpace') unless Y is also in
the mySpace namespace. This way you avoid various symbol collision and
name-lookup problems that may arise. /david

Nov 25 '05 #3
Ed
in x.h
namespace mySpace {
class X {
public:
void funcX(void);
};
}

in x.cpp
#include "x.h"
namespace mySpace {
void X::funcX(void) {
// do something here.
}
}

in y.h
class Y {
public:
void funcY(void);
};

in y.cpp
#include "y.h"
#include "x.h"
void Y::funcY(void) {
mySpace::X x;
x.funcX();
}

Hope this helps. Ed

Nov 25 '05 #4

"vivekian" <vi********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
| Created a class , which is declared within a namespace .
|
| namespace mySpace {
| class X {
| /* members */
| }
|
| This class task is to be used by another class Y , 'using namespace
| mySpace' directive.
|
| I have currently placed the above code containing class X in a header
| file x.h and included in y.c. But not sure as to where the
| declarations and definations should be placed ? Do i place the
| declarations in x.h and definations in x.c , and then just include x.h
| . Not sure about the big picture.
|
| thanks in advance,
| vivekian
|

You don't have to name the files MySpace_X.* but i'ld recommend it in
the case you plan in the future to declare another class X in another
namespace.

// myspace_x.h
#ifndef MYSPACE_X_H_
#define MYSPACE_X_H_ // include guard

namespace MySpace {

class X
{
int m_x;
public:
X();
X(int x);
~X();
/* Member Functions */
int getn() const;
};

} // namespace

#endif // include guard MYSPACE_X_H_
___
// myspace_x.cpp
#include "myspace_x.h"
using MySpace::X; // using directive

X::X() : m_x(0)
{
}

X::X(int n) : m_x(n)
{
}

X::~X()
{
}

int X::getn() const
{
return m_x;
}
___

// test.cpp
#include "myspace_x.h"
#include <iostream>
#include <ostream>

int main()
{
MySpace::X x;
MySpace::X y(99);

std::cout << "x = " << x.getn() << std::endl;
std::cout << "y = " << y.getn() << std::endl;

return 0;
}

/*
x = 0
y = 99
*/


Nov 25 '05 #5
Thanks to everyone . Much clear now as to how things work :)

Nov 25 '05 #6

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

Similar topics

6
by: Snake | last post by:
Hey, where can I ask about little and big endian stuffs? here or another newsgroup?! I just need help in representing string c = "place" and long b = 0x11223344 in a 16 bit word memory in the...
3
by: Roberto Castro | last post by:
Hello! I have been assigned for the first time an adp Access project and so far I have managed to make the changes needed for some requirements. However, I am struggling to find the place where...
13
by: Thomas Zhu | last post by:
Hello, I know the difference between the two definations. But I do not know where are they in the memory. would someone tell me ? char s={"good", "morning"}; // at stack? char *t = {"good",...
10
by: vwd2005eeb | last post by:
Visual Web Developer 2005 Express Edition Beta I did Build | Build Web site, where are the DLLs? I was expecting to see maybe one dll for each Web page in a directory (maybe still "bin") under...
13
by: Viken Karaguesian | last post by:
Hello everyone, Can anyone recommend a good online site to learn PHP? The W3Schools website is quite lacking - leaves much to be desired. I'm sure there are many places, but which ones are good?...
10
by: Paul Cheetham | last post by:
Hi, I am developing an application that needs to store some machine-specific settings. The application is going to be published on the network in order to keep the clients on the latest version....
10
by: Mike9900 | last post by:
Hello, I would like to store application expiration date in a file and store that file in a secure place, so the application can access the file for all the users on that computer. ...
7
by: microgolf | last post by:
Hey, i have a question. i'm about to run an multilangual (if that even is a word) site and i would like to adjust the language of the site depending on what region the user lives... is there a...
2
by: Reggie | last post by:
Hi and TIA! I have a class file located in my root directory with all me web pages. I call/use the class and it works fine. I have no imports statements aspx or codebehind. My question is why? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.