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

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;
}
This article has been dead for over six months. Start a new discussion instead.