Hi i just wanted to ask whether we cud use #include<string> and declare a data type called 'string' eg. string ch;? and where is "using namespace std" used?

Hi i just wanted to ask whether we cud use #include<string> and declare a data type called 'string' eg. string ch;? and where is "using namespace std" used?

The Standard Template Directory (or Definitions... or Defined types...) is a namespace with defined C and C++ types/functions/objects.

Without the "using namespace std" you would have to make calls to the std's objects in this way--

//without the using namespace std

std::cout << "Making a call to the cout and endl objects from namespace std" << std::endl;

--and about your first question, I'm not sure if I'm answering your question correctly when I say this, but yes you can declare a variable of type String in that way... however I'd suggest that you use the call to the constructor of the string object in this way--

#include <string>

string ch = "MyString";

Yes, you can, but you can't use using namespace std; , i.e.,

#include <string>

std::string str = "whatever";
class string {
public:
  std::string str;
};
string q;

I think :P

Thanx guys for helpin me out.

Yes, you can, but you can't use using namespace std; , i.e., And you can even do the using namespace std; stuff.

#include <string>

std::string str = "whatever";
class string {
public:
  std::string str;
};
string q;

I think :P

That won't work because the compiler will get confused about whether you want std::string or the string class you wrote. But you can do it if you declare your own namespace

#include <string>
using namespace std:

namespace MyNamespace
{
     class string
     {
     private:
         std::string mystring;
     public:
         string() {};
         void setString(std::string str) { mystring = str; }
     };
};



int main()
{
    MyNamespace::string mystring;

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.