Answer to Question #193712 in C++ for Sankalp

Question #193712

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-15T07:45:35-0400
#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;
}

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