Why my program doesnt print ?

#include <iostream>
using namespace std;

class String {
	string s;
public:
	String(string ss): s(ss) {}
	string substr(int pos = 0, int n = string::npos)
	{
		string str;
		int j = 0;
		for(int i = pos; i <= n; i++)
		{
			str[j] = s[i];
			j++;
		}
		return str;
	}
};

int main()
{
	String test("i wanna be a milionar");
	string obj = test.substr(3,6);
	cout << obj;
}

I'm not sure what trying to accomplish but you have String and string. If your using std::string in your class then you should include the header file and maybe call your version of string something more unique like maybe My_String.

>>string substr(int pos = 0, int n = string::npos)

That's not likely to work. string::npos does NOT mean the end of the string but rather the largest possible value that can be assigned in a size_t variable. See this link for more details.

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.