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:






Don't name it string and don't introduce the entire std namespace in the current scope.

– Ron
Sep 8 '18 at 9:37


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

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế