include using namespace std 1fndef STRING H define STRING H

#include using namespace std; #1fndef STRING H #define STRING H class String char str[100]; String(char \"input il/ Constructor for initialising string void printstring(); public: String operator+(String s); d: #endif

Solution

Here is the code for String.h:

#include <iostream>
using namespace std;
#ifndef STRING_H
#define STRING_H
class String
{  
char str[100];   //Creates a character array of size 100.
public:
   String(char *input = \"\");   //Creates a constructor which initializes the character pointer to empty string.
  
   void printstring();           //Declares a method printstring which takes no parameters and returns nothing.
   String operator+(String s);   //Declares an overloaded operator which takes a String object as input, and returns a String object as output.
};
#endif

And the code for Account.cpp is:

#include <iostream>
using namespace std;
using std::string;
#include \"String.h\"

String String::operator+(String s)   //Operator overloads the String class.
{
String temp(\"\");           //Creates an object of type temp, with empty string.
strcpy(temp.str, str);   //Copies the character array str to the newly created object temp.
strcat(temp.str, \" \");   //Appends space to the newly created object temp.
strcat(temp.str, s.str);   //Appends the character array of instance variable s to the object temp.str.
return(temp);               //Returns the object temp.
}
void String::printstring()       //Method to print the character array str[] of the instance variable.
{
cout<<\"Output String after concatenation\ \";   //Prints this text.
cout<<str;                                   //Then the character array.
}

And the code for main() is:

#include <iostream>
#include <string>
using namespace std;
#include \"String.h\"
int main()
{
String s1(\"happy\"), s2(\"birthday\"), s3(\"to you\"), s4;   //Creates 4 objects of type String, 3 of them initialized with specified values.
s4 = s1 + s2 + s3;   //Calls the operator overloading over a string class for the given objects.
s4.printstring();       //Calls the method printstring() for the object s4.
return 0;
}

Finally, the expected output for this code is:

happy birthday to you

 #include using namespace std; #1fndef STRING H #define STRING H class String char str[100]; String(char \
 #include using namespace std; #1fndef STRING H #define STRING H class String char str[100]; String(char \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site