LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-17-2007, 03:54 PM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
namespace vs classes in C++


In C++,

1 - What's the difference between a namespace and a class? A namespace replaces the class or vice-versa?

2 - If i've a class defined inside a namespace, in a header file, how should i write the methods to implement in the cpp file?

For example:

test.h
Code:
namespace Test{

class Test {

private:
int i;

public:
void inc(int i);// do i++;
};

}
In the cpp file, how i write the methods?

3 - Namespace and class can have the same name? If not, is there a rule to define namespace and classes names (for example, namespaces should always be written in capitalize letters, or the classes should always start with a capitalize letter)?


Thanks,
Pedro
 
Old 02-17-2007, 06:41 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
1 - What's the difference between a namespace and a class?
They are very different in fact a class is an object whilst a namespace restricts scope and therefore stops the global namespace (yes its also a namespace)and others getting polluted.
Quote:
2 - If i've a class defined inside a namespace, in a header file, how should i write the methods to implement in the cpp file?
Using your example of the header,although changing the namespace identifier.
Code:
namespace TEST
{
	class Test 
	{
	private:
		int m_i;
	public:
		void inc(int i);// do i++;
	};
}
Then in the cpp file
Code:
void TEST::Test::inc(int i)
{
	m_i += i;
}

or 
using namespace TEST;

void Test::inc(int i)
{
	m_i += i;
}

or
namespace TEST
{
	void Test::inc(int i)
	{
		m_i += i;
	}
}
Quote:
3 - Namespace and class can have the same name? If not, is there a rule to define namespace and classes names (for example, namespaces should always be written in capitalize letters, or the classes should always start with a capitalize letter)?
There are no hard and fast rules, above you see the rules that I use(which does not make them correct); these are to start a class name with a capital and capitalise namespace identifiers. From using the STL you will see this is not the method they use in fact the use small casing for both namespaces and classes/templates others use Hungarian notation. I borrow some aspects from Hungarian such as m_ for member variables, but not the rest like prefixed type identifiers such as pc_someChar(pointer to char named someChar). The reason I personally do not like all these prefixes is due to when types need to change in versions of codes all the variables need new prefixes.

Last edited by dmail; 02-17-2007 at 06:48 PM.
 
Old 02-17-2007, 10:38 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I look at a class as a description of what a variable will look like, you can create many variables which are based on the class description, each will have their own unique state. The class will describe each of the different data values it will have, along with the different functions that can operate upon this data.

In OO speak objects are created based on the class, The state of an object is defined by the value of the member variables that are held within the object, member functions can be used to adjust the value of these member variables, hence changing the state of each object.

I think of a namespace as a convenient mechanism of holding related functions and occasionally variables together. It can help to ensuring that there will not me a class of function names. Now let's assume that you have written a library that sends data along a raw socket, it will probably have a function called send. Now I might see that library and think wonderful I want to use that in my latest network management program, everything is going fine until I hook into my program another library that sends email messages, that also have a function called send. Without namespaces I'd be stuck, thankfully you would of used names paces so I can clearly instruct the compiler which send function to use, RAW_SOCKET::send() or EMAIL::send(). Neither of the aforementioned libraries use classes, but they use namespaces to ensure that the labels used in their (to borrow a Java term) package can be uniquely identified.
 
Old 02-18-2007, 03:08 AM   #4
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Thanks for your reply
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Namespace Question joelhop Programming 3 08-23-2006 10:28 AM
C++ Vector Class without Namespace sadarax Programming 6 02-02-2006 05:20 PM
Simple C++ Namespace Question kevin_cpp Programming 11 05-17-2005 05:31 PM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM
Understanding Namespace? nny0000 Programming 2 12-22-2003 08:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration