What's the difference between namespaces & classes? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In C++13/4 environment, what's the difference between a namespace and a class?
The way I see it;
namespace foo
{
int a : 4;
int b : 4;
}
and
class bar
{
public:
int a = 0;
int b = 0;
}
is the same thing...
Yes, they're accessed different as such;
namespace....
foo::a=20;
foo::b=30;
class....
bar alpha;
alpha.a ...
alpha.b ...
But in general, what's the advantage of one over the other?

There are plenty of differences. A namespace is a grouping mechanism for names, nothing more or less. On the other hand, classes:
Are types.
Can be instantiated.
Can be derived from.
Can have public, protected, and private members.
Can have virtual functions.
And so on.
If you find yourself wondering whether you should use a namespace or a class, then you are probably just looking for a way to control the scope of names---so the choice is clear: use a namespace.

Namespaces allow you to group entities into having a local scope rather than global scope. This is common with the standard library, std, for instance.
An example group of names may be streams such as cout and cin.
Without using a namespace, you have to define the namespace scope std.
std::cout << "Hello, world!";
With a namespace, you have revealed the scope once and no longer need to declare it again.
using namespace std;
cout << "Hello, world!";
However, in the latter case, you could not use another variable named cout that is user-defined or contained within another library.

Related

about const keyword as a parameter in function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I still don't understand what is the difference between these two code i mean if we put const
keyword in front of data type parameter in function what happened to my our code
#include <iostream>
using namespace std;
void function1(const int x);
int main(){
};
And if we don't put const keyword what happened to our code?
#include <iostream>
using namespace std;
void function2( int x);
int main(){
};
The const keyword prevents a variable from being modified.
It mostly used to prevent an object that is passed by reference from being modified. It can also prevent mistakes at compile time like writing = instead of == in boolean statements.
In your case, the variable is passed by value and not by reference. In both function declarations, the variable inside the function is a copy of the variable you pass to that function. This means that even if you change a variable that is passed by value, its value will only change inside the scope of that function.
Relate to this article about scoping.

Proper typedef location in C++ [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I would like to ask the proper location of typedef in C++.
Version1 : typedef outside class
typedef std::pair<std::string, int> StrIntPair;
typedef std::vector<StrIntPair> StrIntPairVec;
class MyData
{
public:
MyData(){};
~MyData(){};
private:
void addInfo(const StrIntPair &info)
{
infoVec.push_back(info);
}
StrIntPair info;
StrIntPairVec infoVec;
};
Version2 : typedef inside class public
class MyData
{
public:
MyData(){};
~MyData(){};
typedef std::pair<std::string, int> StrIntPair;
typedef std::vector<StrIntPair> StrIntPairVec;
private:
void addInfo(const StrIntPair &info)
{
infoVec.push_back(info);
}
StrIntPair info;
StrIntPairVec infoVec;
};
Version3 : typedef inside class private
class MyData
{
public:
MyData(){};
~MyData(){};
private:
typedef std::pair<std::string, int> StrIntPair;
typedef std::vector<StrIntPair> StrIntPairVec;
void addInfo(const StrIntPair &info)
{
infoVec.push_back(info);
}
StrIntPair info;
StrIntPairVec infoVec;
};
Which version is the best practice?
This depends on where you use the type alias. I'd advice you to
Put them outside of the class if you use them across classes and/or functions and the meaning of the alias is not exclusively related to the class.
Define them as public class type aliases if client code outside of the class needs to access them (e.g. to initialize the object or to store an aliased return value of a member function) but the alias is related to the class. The alias then becomes a part of the class interface.
Define them as private class type aliases when you use them exclusively inside the class, e.g. some utility data structure that is annoying to type out all the time when passing it across member functions.
The compiler will only enforce scopes of aliases that are too narrow (e.g. you use a type alias defined in the private section of your class outside of that class) and won't complain if you choose an unnecessarily permissive scope (e.g. you publicly declare the alias, but use it only in the class implementation). Hence, strive to choose the narrowest scope possible, which is in line with hiding implementation details.
As a side note, you might want to consider declaring your type aliases with using StrIntPair = std::pair<std::string, int>;, see Item 9 in Effective Modern C++. This has no influence on the above, though.
The question is about logical namespace of those names. With abstract naming like StrIntPair, StrIntPairVec and MyData there are no answers. Answers come when the things have meaning.
Lets take exactly same data structures but name them NickAndId, Friends and Player.
Now the question if to put NickAndId inside Player is about if it is specific to player. Can other entities like NonPlayerCharacter or Creature also have nickname and id expressed as same pair? Possibly. Then it should be outside.
Same question should be asked about Friends. Likely the NonPlayerCharacter and Creature can have nickname and id but do not have friends? Then it makes sense to put the type inside of Player as Player::Friends.
Finally, the types that are made private are meant only for usage by implementation details. That should be used when the name makes perfect sense in algorithms used inside of class but availability of those outside is unneeded or even worse, confusing. For example NonPlayerCharacter can react with some replicas to some states whose values are also internal to that NPC. Keeping that in sorted vector Reactions makes perfect sense inside of class. Access to ReplicaInState and Reactions from outside can be confusing.

C++ difference between : , :: operators [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I new to programming. In stack overflow i couldn't see difference between : & :: is mentioned. Could anyone can explain in detail it helps to beginner learners like me. Thank you.
So you would use :: when you're defining/using methods from a class, so like for example
class foo{
public:
int bar;
int hi(int x);
int func(); // static member function
Foo(int num): bar(num) {}; // use of a colon, initialization list
};
int foo::hi(int x){
//define the function
}
Also if you have static member functions, you can just call those whenever through using foo::func(). You can find more about static member functions online.
The single colon is for member initialization list (you can look this topic up online) where you can initialization member variables in the construction of your class.
You can also find single colon used in polymorphism, when you derive a class from a base class. You can find more information about c++ polymorphism online.

When to use struct or class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am working on OOPs and using C++. I have one class accessing object of other class/struct.
struct data
{
int a;
int b;
string str;
} sd;
class format
{
int x;
void show()
{
cout << data.a << endl;
}
};
which one is best to use here class or struct?
First of all, it's struct, not strut.
Second, you cannot access member a like you do, data.a, but rather sd.a, because you need to access it on an instance, not on the name of the struct.
For the detailed differences between class and struct see this SO question and its two best rated answers.
I use this convention:
A struct only have members that it make sense to manipulate directly
A class may have complicated rules for assigning members
This somewhat fits well with the default accessibility rules. But as said before in this thread, the choice depends on convention.
that depends on your requirement the only difference in struct and class is in struct all members are public by default and private in case of class

Mixing global functions and class member functions C++ [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Is it bad to call global functions from member functions of a class? I mean is this ok...
class MyClass
{
void print_numb();
};
int get_numb()
{
return 10;
}
void MyClass::print_numb()
{
cout << get_numb() << endl;
}
If get_numb() is only used by a source file implementing MyClass then I'd put it in an anonymous namespace in that source file:
namespace /*no name here means the namespace is anonymous*/ {
int get_numb()
{
return 10;
}
}
That hides it away. I prefer that to a static private function in the class since it reduces the amount of stuff in the class declaration.
It is absolutely OK from the technical point of view: free-standing functions, global and static, are part of the language. There is no reason not to use them.
It is also OK stylistically: the Standard C++ Library provides free-standing functions, so the designers of the language were definitely OK with the idea of mixing member and non-member functions.
Of course you should take advantage of the C++ features that let you isolate your functions from functions in the libraries to which you link - by reducing their visibility to a single translation unit, or by placing them in a namespace.