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.
#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;
}
Comments
Leave a comment