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
#include <iostream>
#include <string>
using namespace std;
class String{
string s;
public:
String(){}
String(const string str){
s = str;
}
String operator+(const String &other){
return String(this->s + other.s);
}
void Display(){
cout<<s;
}
};
int main(){
String s, s2("Conc"), s3("atenated");
s.Display(); cout<<endl;
s2.Display(); cout<<endl;
s3.Display(); cout<<endl;
(s2 + s3).Display(); cout<<endl;
return 0;
}
Comments
Leave a comment