1
#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++.

Akash
  • 425
  • 2
  • 7
  • 21
  • 3
    Don't name it `string` and don't introduce the entire `std` namespace in the current scope. – Ron Sep 08 '18 at 09:37
  • 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 08 '18 at 09:51

2 Answers2

3

Seems like your professor wants you to understand following things,

  1. Namespaces, what is the purpose of it.
  2. Do not pollute your namespace. (using namespace std;)
  3. Different kind of constructors.

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.

  1. Namespace pollution - using namespace std in headers are bad.
  2. Purpose of namespaces.
PraAnj
  • 899
  • 1
  • 10
  • 27
0
  1. Avoid using names that are too generic like string for your classes, functions and variables.
  2. Avoid using namespace std when you include the headers. Instead use the specific functions of that namespace (e.g. cout) like this std::cout wherever required.
P.W
  • 26,289
  • 6
  • 39
  • 76
  • I know using string throws error .... but I have to do it this way for assignment..that's why i was asking it. – Akash Sep 08 '18 at 09:50
  • Should your namespace name also be string? – P.W Sep 08 '18 at 10:01
  • yes my namespace name also is string. – Akash Sep 08 '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: http://cpp.sh/442e7 – P.W Sep 08 '18 at 10:08
  • but in my program i am not receiving any arguements from user... – Akash Sep 08 '18 at 10:24
  • That's just an example to show the use of string from two different namespaces. You can adapt it as per your requirement. – P.W Sep 08 '18 at 10:30