namespace with classes and objects in c++ [closed]
namespace with classes and objects in c++ [closed]
#include <iostream>
#include <string>
using namespace std;
namespace string
class string
string a;
public:
string()
string ( const string *q ): a(*q)
string (string &r):a(r.a)
string (const std::string &_a):a(_a)
~demo ()
void show ()
cout << a;
void change (const std::string &_a)
a = _a;
;
using namespace string;
int main ()
demo s1;
demo s2("Hello");
demo s3(s2);
s1.show();
s2.show();
s3.show();
s2.change("Java");
s2.show();
s3.show();
Desired OUTPUT is Hello Hello Java Hello
I want To define a namespace i.e. string. And in there is a header file in c++ i.e.. string both have same name
so how can i code the program to avoid nameclash between these two same names in c++.
This question appears to be off-topic. The users who voted to close gave this specific reason:
string
std
Your code is very hard to read due to the variable amount of indentation and having an empty line everywhere. Please consider formatting it nicely.
– Felix
Sep 8 '18 at 9:51
2 Answers
2
Seems like your professor wants you to understand following things,
using namespace std;
Tried to compile your program. Not sure all your intentions are correct in following successfully compiled solution. Check it.
#include <iostream>
#include <string>
// Do not pollute the global namespace with using namespace.
//using namespace std;
namespace string
class string
std::string a; // Assume you need to store a string inside your class.
public:
string() ;
string (const std::string *q ) : a(*q)
string (string &r):a(r.a)
string (const std::string &_a):a(_a)
~string()
void show ()
std::cout << a;
void change (const std::string &_a)
a = _a;
; // ~class end
; //~namespace end
int main ()
// Use full namespace qualified ID everywhere
string::string s1;
string::string s2("Hello");
string::string s3(s2);
s1.show();
s2.show();
s3.show();
s2.change("Java");
s2.show();
s3.show();
Do not use this straightaway. Learn following things.
using namespace std
Output : onlinegdb.com/ry1tcm-dQ
– PraAnj
Sep 8 '18 at 10:55
thanks you very much PraAnj ...
– Akkiê Thakur
Sep 8 '18 at 11:03
Don't take only the compiled program. Learn the concepts well.
– PraAnj
Sep 8 '18 at 11:06
I am Wisely understanding these concepts....
– Akkiê Thakur
Sep 9 '18 at 4:59
string
using namespace std
cout
std::cout
I know using string throws error .... but I have to do it this way for assignment..that's why i was asking it.
– Akkiê Thakur
Sep 8 '18 at 9:50
Should your namespace name also be string?
– P.W
Sep 8 '18 at 10:01
yes my namespace name also is string.
– Akkiê Thakur
Sep 8 '18 at 10:04
Then whenever you want to use your own string class, you should use
string::string
. When you want to use the C++ library class, you should use std::string
. See an example here: cpp.sh/442e7– P.W
Sep 8 '18 at 10:08
string::string
std::string
but in my program i am not receiving any arguements from user...
– Akkiê Thakur
Sep 8 '18 at 10:24
Don't name it
string
and don't introduce the entirestd
namespace in the current scope.– Ron
Sep 8 '18 at 9:37