L
Luis
hey guys, my assignment is to write a string class. below is all of my code.
When i try to overload the += operator, my program crashes every single
time. could some one check my code, and see what I am doing wrong please?
Thank You.
P.s. i would appreciate a promt response, i was due today, but if i finish
it soon my teacher might accept it. also if you have any ideas on how to
compare two c-strings using lexiographic ordeing please let me know.
#include <iostream>
#include "MyString.h"
#include <fstream>
#include <cassert>
#include <string>
using namespace std; //introduces namespace std
int main ( void )
{
MyString a("rr");
MyString b("aaa");
a+=b;
cout<<a;
return 0;
}
MyString::MyString()
{
string = "";
}
MyString::MyString(const char *inString)
{
string = new char[strlen(inString)+1];
strcpy(string,inString);
}
MyString::~MyString()
{
delete []string;
}
ostream& operator<<(ostream &out,const MyString &outString)
{
out<<outString.string;
return out;
}
MyString MyString:: operator=(MyString &right)
{
if (this != &right)
{
delete []string;
string = new char[strlen(right.string)+1];
strcpy(string,right.string);
}
return *this;
}
MyString::MyString(MyString &right)
{
string = new char[strlen(right.string)+1];
strcpy(string,right.string);
}
char& MyString:perator[](int index)
{
assert(index>=0 && index < strlen(string));
return string[index];
}
char MyString:perator[](int index)const
{
assert(index>=0 && index < strlen(string));
return string[index];
}
char *operator+(const MyString left,const MyString right)
{
char *temp;
temp = new char[strlen(left.string)+strlen(right.string)+1];
temp = strcat(left.string,right.string);
return temp;
}
istream& operator>>(istream& in,MyString &inString)
{
char temp[128];
delete [] inString.string;
in>>temp;
strcpy(inString.string,temp);
return in;
}
void MyString::read(istream &inString,char delimit)
{
char temp[128];
delete []string;
inString.getline(temp,127,delimit);
strcpy(string,temp);
}
char MyString:: operator+=(const MyString right)
{
MyString temp;
temp = *this;
delete []temp.string;
temp.string = new char[strlen(temp.string)+strlen(right.string)+1];
strcat(temp.string,right.string);
strcpy(string,temp.string);
return temp.string;
}
/* these are not working yet
bool operator<(const MyString left,const MyString right)
{
if(strcmp(left.string,right.string)<0)
return true;
}
bool operator>(const MyString left,const MyString right)
{
if(strcmp(left.string,right.string)>0)
return true;
}
*/
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
using namespace std;
class MyString {
public:
//Constructors, Copy, and Destrctors
MyString(); //one of four
MyString(const char *inString); //two of four
MyString operator=(MyString &right); //three of four
MyString(MyString &right); //four of four
~MyString(); //destructor
//input/output
friend ostream& operator<<(ostream& out,const MyString &outString);
friend istream& operator>>(istream& in,MyString &inString);
void read(istream& inString,char delimit);
//Overloaded brackets
char& operator[](int index);
char operator[](int index)const;
//overloaded '+' & +=
char* operator+=(const MyString right);
friend char* operator+(const MyString left,const MyString right);
//Comparison operators
/*
friend bool operator<(const MyString left,const MyString right);
friend bool operator>(const MyString left,const MyString right);
*/
private:
operator>(const MyString left,const MyString right);
*/
};
#endif
When i try to overload the += operator, my program crashes every single
time. could some one check my code, and see what I am doing wrong please?
Thank You.
P.s. i would appreciate a promt response, i was due today, but if i finish
it soon my teacher might accept it. also if you have any ideas on how to
compare two c-strings using lexiographic ordeing please let me know.
#include <iostream>
#include "MyString.h"
#include <fstream>
#include <cassert>
#include <string>
using namespace std; //introduces namespace std
int main ( void )
{
MyString a("rr");
MyString b("aaa");
a+=b;
cout<<a;
return 0;
}
MyString::MyString()
{
string = "";
}
MyString::MyString(const char *inString)
{
string = new char[strlen(inString)+1];
strcpy(string,inString);
}
MyString::~MyString()
{
delete []string;
}
ostream& operator<<(ostream &out,const MyString &outString)
{
out<<outString.string;
return out;
}
MyString MyString:: operator=(MyString &right)
{
if (this != &right)
{
delete []string;
string = new char[strlen(right.string)+1];
strcpy(string,right.string);
}
return *this;
}
MyString::MyString(MyString &right)
{
string = new char[strlen(right.string)+1];
strcpy(string,right.string);
}
char& MyString:perator[](int index)
{
assert(index>=0 && index < strlen(string));
return string[index];
}
char MyString:perator[](int index)const
{
assert(index>=0 && index < strlen(string));
return string[index];
}
char *operator+(const MyString left,const MyString right)
{
char *temp;
temp = new char[strlen(left.string)+strlen(right.string)+1];
temp = strcat(left.string,right.string);
return temp;
}
istream& operator>>(istream& in,MyString &inString)
{
char temp[128];
delete [] inString.string;
in>>temp;
strcpy(inString.string,temp);
return in;
}
void MyString::read(istream &inString,char delimit)
{
char temp[128];
delete []string;
inString.getline(temp,127,delimit);
strcpy(string,temp);
}
char MyString:: operator+=(const MyString right)
{
MyString temp;
temp = *this;
delete []temp.string;
temp.string = new char[strlen(temp.string)+strlen(right.string)+1];
strcat(temp.string,right.string);
strcpy(string,temp.string);
return temp.string;
}
/* these are not working yet
bool operator<(const MyString left,const MyString right)
{
if(strcmp(left.string,right.string)<0)
return true;
}
bool operator>(const MyString left,const MyString right)
{
if(strcmp(left.string,right.string)>0)
return true;
}
*/
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
using namespace std;
class MyString {
public:
//Constructors, Copy, and Destrctors
MyString(); //one of four
MyString(const char *inString); //two of four
MyString operator=(MyString &right); //three of four
MyString(MyString &right); //four of four
~MyString(); //destructor
//input/output
friend ostream& operator<<(ostream& out,const MyString &outString);
friend istream& operator>>(istream& in,MyString &inString);
void read(istream& inString,char delimit);
//Overloaded brackets
char& operator[](int index);
char operator[](int index)const;
//overloaded '+' & +=
char* operator+=(const MyString right);
friend char* operator+(const MyString left,const MyString right);
//Comparison operators
/*
friend bool operator<(const MyString left,const MyString right);
friend bool operator>(const MyString left,const MyString right);
*/
private:
operator>(const MyString left,const MyString right);
*/
};
#endif