Answer to Question #192884 in C++ for Sankalp

Question #192884

Define a class string. Use different constructors and do the following [20marks]- Create un-initialized string objects- Create objects with string constants- Concatenate two strings- Display desired strings

1
Expert's answer
2021-05-17T07:46:56-0400
#include <iostream>
#include <string>
using namespace std;




class String {
private:
	string inputString;
public:	
	String(){}	
	String(string inputString){
		this->inputString=inputString;
	}	
	
	void appendOtherString(const String& otherString) {
		inputString.append(otherString.inputString);
	}
	string getString(){
		return inputString;
	}
};




int main(){
	//Create unitialized string objects.
	String str1;
	String str2;
	//Create objects with string constants
	String string1("Test ");
	cout<<"String 1: "<<string1.getString()<<"\n\n";
	String string2("strings");
	//Concatenate two strings
	string1.appendOtherString(string2);
	//Display desired strings
	cout<<"String 2: "<<string2.getString()<<"\n\n";
	cout<<"String 1 append String 2 : "<<string1.getString()<<"\n\n";

	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