In: Computer Science
C++ programming
You are to implement a MyString class which is our own limited implementation of the std:: string
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 <iostream>
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 <iostream>
#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;
}
/* MyString class */
//mystring.h
#ifndef MyString_H
#define MyString_H
#include <iostream>
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
//testMyString.cpp
/* Test for MyString class */
#include <iostream>
#include<stdlib.h>
#include "mystring.h"
using namespace std;
ostream& operator <<(ostream& os, MyString&
s)
{
//print string and length of a string
os << "String: "<<s.str<<endl;
os<<"Length: "<<s.len<<endl;
return os;
}
MyString ::MyString ()
{
//allocate memory for the string
str = (char *)malloc(sizeof(char) * 100);
len = 0;
}
MyString:: MyString(const char *s)
{
//copy contents of char *s to the str
int length = string(s).length();;
str = (char *)malloc(sizeof(char) * length);
int pos = 0;
while(s[pos]!='\0')
{
str[pos] = s[pos];
pos++;
}
str[pos] = '\0';
len = length;
}
MyString:: MyString (MyString &s)
{
//copy contents of s.str to the str
len = s.len;
str = (char *)malloc(sizeof(char) * len);
int pos = 0;
while(s.str[pos]!='\0')
{
str[pos] = s.str[pos];
pos++;
}
str[pos] = '\0';
}
MyString& MyString:: operator=(MyString& s)
{
//copy contents of s.str and s.len to the str and len
respectively
len = s.len;
str = (char *)malloc(sizeof(char) * len);
int pos = 0;
while(s.str[pos]!='\0')
{
str[pos] = s.str[pos];
pos++;
}
str[pos] = '\0';
return *this;
}
MyString& MyString:: operator+(MyString& s)
{
//add string
int pos = 0;
while(s.str[pos]!='\0')
{
str[pos + len] = s.str[pos];
pos++;
}
str[pos +len] = '\0';
len += s.len;
return *this;
}
MyString:: ~MyString()
{
cout<<this<<" Object
Destroyed"<<endl;
}
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;
}