Question
You are to implement a MyString class which is our own limited implementation of the std:: string Include comment Header file and test (main) file
You are to implement a MyString class which is our own limited implementation of the std:: string
Include comment
Header file and test (main) file are given in below, code for mystring.cpp.
Here is header file
mystring.h
/* MyString class */
#ifndef MyString_H
#define MyString_H
#include
using namespace std;
class MyString {
private:
char* str;
int len;
public:
MyString();
MyString(const char* s);
MyString(MyString& s);
~MyString();
friend ostream& operator <<(ostream& os, MyString& s); // Prints string
MyString& operator=(MyString& s); //Copy assignment
MyString& operator+(MyString& s); // Creates a new string by concantenating input string
};
#endif
Here is main file, the test file
testMyString.cpp
/* Test for MyString class */
#include
#include "mystring.h"
using namespace std;
int main()
{
char greeting[] = "Hello World!";
MyString str1(greeting); // Tests constructor
cout << str1 << endl; // Tests << operator. Should print Hello World!
char bye[] = "Goodbye World!";
MyString str2(bye);
cout << str2 << endl; // Should print Goodbye World!
MyString str3{str2}; // Tests copy constructor
cout << str3 << endl; // Should print Hello World!
str3 = str1; // Tests copy assignment operator
cout << str3 << endl; // Should print Goodbye World!
str3 = str1 + str2; // Tests + operator
cout << str3 << endl; // Should print Hello World!Goodbye World!
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started