C++ Programming using function overloading, classes, and string concepts. class
ID: 3840612 • Letter: C
Question
C++ Programming using function overloading, classes, and string concepts.
class String {
friend String operator+ (________________________); // concatenation operator
friend bool operator= = (_______________________); // comparison operator
friend ostream & operator<< (_______________________________); // insertion
public:
String (const char * = “”); // String default Constructor. It initializes the
// string object to null or to the characters of a c-string
String (_____________________________); // copy constructor
~String ( ); // Class destructor
String & Operator= (_____________) // overloading the assignment operator
String operator ( ) (int i , int n); // returns a string object which contains the
// c-string that begins at i and of length n.
private:
int length; // number of characters in the string (without the NULL).
char * pchars; // pointer to the array that stores the string
// characters. The array of characters is terminated
// by the NULL character.
};
1. Complete the declaration of the String class
2. Implement the default constructor
3. Implement the copy constructor of the class
4. Overload the assignment operator “=” as a member function of the String class
5. Overload the operator “( )” which takes, as shown below, an index i and a length n and returns a string object that stores c-string that starts at i and of length n.
6. Overload the insertion operator “<<” as a friend function for the String class
7. Overload the string comparison operator “= =” as a friend function. That is, the comparison of two String objects should return the Boolean true, if the two objects are the same, false otherwise.
8. Overload the string concatenation operator “+” as a friend function. That is, the concatenation of two String objects should result in appending the right-hand-side string object to the “left-hand-side” String object and storing the result in a new string object that is returned by the operator function.
9. Implement an appropriate driver program that test the developed String class. Your program should test all of the implemented functions. In your testing, you may use the following string objects.
S: the empty string
S1 = {“ABC”}
S3 = {“DEFG”}
As an example of testing, the following sample code can be used. Of course, more test cases are needed and should be used.
void main ( ) {
String S;
cout << S; // prints out the following statement: This string is empty.
String S1 (“ABC”);
cout << S1; // prints out ABC
String S2 (S1);
cout << S2; // prints out ABC
String S3 = “DEFG”;
cout << (S1 + S3) << endl;
}
Explanation / Answer
//String.h
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class String
{
int length;
char *pchars;
friend String operator+(String s1,String s2);
friend bool operator==(String s1,String s2);
friend ostream &operator<<(ostream &out, String s);
public:
String();
String(char *s);
String(String &s);
~String();
String &operator=(char *s);
String operator()(int i, int n);
};
---------------------
//String.cpp
#include"String.h"
String::String()
{
pchars = "";
length = 0;
}
String::String(char *s)
{
pchars = (char*)malloc(strlen(s)+1);
strcpy(pchars , s);
length = strlen(pchars);
}
String::~String()
{
if (pchars != "")
delete pchars;
}
String::String(String &s)
{
pchars = (char*)malloc(length + 1);
strcpy(pchars, s.pchars);
}
String &String::operator=(char *s)
{
String str(s);
return str;
}
String String::operator()(int i, int n)
{
char *s;
int j = 0;
s = (char*)malloc(n);
for (int i = 0; i < n; i++)
{
s[j++] = pchars[i];
}
s[j] = '';
String str(s);
return str;
}
String operator+(String s1,String s2)
{
strcat(s1.pchars, s2.pchars);
String str(s1.pchars);
return str;
}
bool operator==(String s1, String s2)
{
if (strcmp(s1.pchars, s2.pchars) == 0)
return true;
else
return false;
}
ostream &operator<<(ostream &out, String s)
{
if (s.pchars != "")
out << s.pchars << endl;
return out;
}
----------------------
//main.cpp
#include"String.h"
int main()
{
String S;
cout<<S;
String S1("ABC");
cout << S1;
String S2(S1);
cout << S2;
String S3 = "DEFG";
cout << (S1 + S3) << endl;
}
------------------------------
//output
ABC
ABC
ABCDEFG
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.