Answer to Question #214433 in C++ for shayan abbasi

Question #214433

Write a program having class STRING, With data member which can store character array(Dynamically you can store). It should facility to initialize by parameterize constructor and concatenate STRING objects.


Ex:  STRING  obj1(“OOP”), obj2 (“IN KIIT”);


Obj1 = obj1 + obj2;


Then obj1 should have “ OOP IN KIIT”  string in it.


1
Expert's answer
2021-07-08T01:32:38-0400
#include <iostream>

using namespace std;
class STRING{
private:
	// store character array(Dynamically you can store).
	char* inputString; 
public:


	STRING(){
		this->inputString=new char[500];
	}
	


	STRING(char* inputString){
		this->inputString=inputString;
	}


	STRING operator+(STRING otherString){
		STRING newString;
		for(int i=0;i<strlen(inputString);i++){
			newString.inputString[i]=inputString[i];
		}
		newString.inputString[strlen(inputString)]=' ';
		int startPosition=strlen(inputString)+1;
		int index=0;
		for(int i=startPosition;i<startPosition+strlen(otherString.inputString);i++){
			newString.inputString[i]=otherString.inputString[index++];
		}
		newString.inputString[startPosition+strlen(otherString.inputString)]='\0';
		return newString;
	}
	void print(){
		cout<<this->inputString<<"\n\n";
	}
};
int main()
{
	STRING obj1("OOP"), obj2 ("IN KIIT");
	obj1 = obj1 + obj2;
	obj1.print();


	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
APPROVED BY CLIENTS