I am making a string class and I am having an issue with overloading the followi
ID: 3592578 • Letter: I
Question
I am making a string class and I am having an issue with overloading the following operators "<", "+=", and "==". How should I overload these operators properly?
#ifndef STRING_H
#define STRING_H
// String.h
#include <iostream>
using namespace std;
class String {
private:
int size;
char *buffer;
public:
String();//done
String(const String &);//done
String(const char *);//done
String & operator =(const String &rhs);//done
~String(void);//done
int length();//done
char & operator[](int i);//done
char output();//done
friend bool operator==(const String &, const String &); //done
friend bool operator<=(const String &, const String &);
friend bool operator<(const String &, const String &);
String operator+=(const String &);
friend ostream & operator<<(ostream &, const String &);//done
};
#endif
--------------------------------------------------------------------------
// String_test.cpp
#include "String.h"
#include <cassert>
#include <cstring>
#include <iostream>
using namespace std;
int main(){
String s1 = "abc";
String s2 = "def";
String s3("hi"); // s3 == "hi"
assert(s3.length() == 2);
s1+=s2;
assert(s1 == "abcdef");
if(s1 < s2){
cout<< "S1 less than s2";
}
else {
cout<<"not less than";
}
return 0;
}
String::String(){
size = 0;
buffer =0;
}
String::String(const String &op){
int size = op.size;
buffer = new char[size];
for( int i=0; i < size; i++){
buffer[i] = op.buffer[i];
}
}
String::String(const char *op){
int size = strlen(op);
buffer = new char[size];
for( int i=0; i< size; i++){
buffer[i] = op[i];
}
}
String& String::operator =(const String &rhs){
if(this != &rhs){
if(rhs.size != size){
delete [] buffer;
buffer = 0;
buffer= new char[rhs.size];
size = rhs.size;
}
}
return *this;
}
String::~String(){
delete [] buffer;
}
int String::length(){
int i = 0;
while(buffer[i] != '')
{i++;}
size = i;
return i;
}
char & String::operator[](int x){
return buffer[x];
}
bool operator==(const String &s, const String &t)
{
if(s.size != t.size){
return false;
}
else
for(int i=0; i<s.size; i++)
{
if(s.buffer[i] != t.buffer[i])
return false;
}
}
bool operator<=(const String &s, const String &t)
{
for(int i=0; i < s.size && i < t.size; i++){
if((s.buffer[i] == t.buffer[i] && s.size == t.size)|| s.buffer[i] <t.buffer[i] )
return true;
}
}
bool operator<(const String &s, const String &t)
{
if(s.size > t.size || s.size == t.size ){
return false;
}
}
String String:: operator +=(const String &source){
int temp_size = size + source.size;
char *temp;
temp = new char[temp_size];
int i,j;
for(i=0; i< size; i++)
temp[i]= buffer[i];
for(j=0; j<source.size; j++)
temp[i++] = source.buffer[j];
delete [] buffer;
size = temp_size;
buffer = temp;
return *this;
}
ostream& operator<<(ostream& os, const String& obj)
{
return os;
}
char String::output(){
for(int i=0; i < size; i++){
cout<<buffer[i];
}
}
Explanation / Answer
bool operator<(const String &s, const String &t)
{
return strcmp(s.buffer, t.buffer) < 0;
}
bool operator==(const String &s, const String &t)
{
return strcmp(s.buffer, t.buffer) == 0;
}
String String:: operator +=(const String &source){
char *new_contents = new char[(size+ str.size) + 1];
strcpy(new_contents, buffer);
strcpy(new_contents, str.buffer);
new_contents[(size + str.size)] = '';
delete [] contents;
buffer = new_contents;
size = (size+ str.size);
return *this;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.