answersLogoWhite

0


Best Answer

There's no need to create a class to do this as std::string already does this and a lot more besides. The following class will do exactly as you've asked, but it's really just a wrapper for a std::string and achieves absolutely nothing.

#include<iostream>

class string{

public:

string(std::string s=""):m_str(s){}

string(const string& s):m_str(s.m_str){}

const std::string& get_string(){return(m_str);}

private:

std::string m_str;

};

int main()

{

string name1;

string name2("minu");

string name3(name2);

std::cout << "name1 = '" << name1.get_string().c_str() << "'" << std::endl;

std::cout << "name2 = '" << name2.get_string().c_str() << "'" << std::endl;

std::cout << "name3 = '" << name3.get_string().c_str() << "'" << std::endl;

return(0);

}

Output:

name1 = ''

name2 = 'minu'

name3 = 'minu'

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Declare a class called string - it must have constructors which allow definitions of objects in the following form - string name1 string name2minu string name3name2 in cpp?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions