Answer to Question #193317 in C++ for Sankalp

Question #193317

Define a class string. Use different constructors and do the following [20 marks]

 - Create un-initialized string objects

 - Create objects with string constants

 - Concatenate two strings

 - Display desired strings


1
Expert's answer
2021-05-15T17:15:51-0400
#include <iostream>
#include <string>
using namespace std;




class String {
private:
	string str;
public:	
	String(){}	
	String(string str){
		this->str=str;
	}	
	~String(){}
	
	string concatenate(const String& other) {
		return str.append(other.str);
	}
	string getString(){
		return str;
	}
};




int main(){
	//Create unitialized string objects.
	String* string_1=new String();
	String* string_2=new String();
	//Create objects with string constants
	String* string1=new String("I like ");
	String* string2=new String("programming");
	//Concatenate two strings
	string string3=string1->concatenate(*string2);
	//Display desired strings
	cout<<"string 1: "<<string1->getString()<<"\n\n";
	cout<<"string 2: "<<string2->getString()<<"\n\n";
	cout<<"string 3: "<<string3<<"\n\n";


	delete string_1;
	delete string_2;
	delete string1;
	delete string2;


	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog