We were given this assignment as a way to practice pointers, operator overloading, etc. Essentially, we created our own string class with several different functions within it.
String1030.h
// String1030.h
//@author Lupus
// Header for class that will handle an implementation
// of strings.
#ifndef STRING1030_H
#define STRING1030_H
#include<iostream>
using std::ostream;
using std::istream;
using std::endl;
using std::cerr;
class String1030
{
public:
String1030(const char *buf=0);
String1030(const String1030& oldstring);
~String1030();
String1030& operator=(const String1030& right);
char& operator[](int index);
int getSize(void) const;
void setSize(int newsize);
const char *getString();
void setString(const char *carray);
private:
char *buffer;
int mysize;
};
#endif
String1030.cpp
//String1030.cpp
//@author Lupus
//March 27th, 2013
#include "String1030.h"
//Basic constructors
String1030::String1030(const char *buf): mysize(0)
{
setString(buf);
}
String1030::String1030(const String1030& oldstring): mysize(0)
{
if(oldstring.getSize() <= 0)
{
setSize(0);
}
else
{
setSize(oldstring.getSize());
for(int i = 0; i < (mysize+1); i++)
{
buffer[i] = oldstring.buffer[i];
}
}
}
//Reallocates the memory.
String1030::~String1030()
{
delete [] buffer;
}
//Overloads the = operator, causing one string to be set to another.
String1030& String1030::operator=(const String1030& right)
{
if(!(&right == this))
{
if(right.getSize() <= 0)
{
setSize(0);
}
else
{
setSize(right.getSize());
for(int i = 0; i < (mysize+1); i++)
{
buffer[i] = right.buffer[i];
}
}
}
return *this;
}
//Deals with operator overloading of the [] operator.
char& String1030::operator[](int index)
{
if(!(index < 0 || index >= mysize))
{
return buffer[index];
}
else return buffer[0];
}
//Simple accessor function for size.
int String1030::getSize(void) const
{
return mysize;
}
//Resetting size of a string.
void String1030::setSize(int newsize)
{
if(!(newsize < 0))
{
if(buffer != NULL)
{
mysize = newsize;
buffer = new char[mysize+1];
}
else
{
mysize = newsize;
buffer = new char[mysize+1];
}
}
}
//A simple accessor function to return the string.
const char* String1030::getString()
{
return buffer;
}
//Handles setting a new string.
void String1030::setString(const char *carray)
{
int index(0);
if(carray != 0)
{
while(*(carray + index) != '\0')
{
index++;
}
mysize = index;
buffer = new char[mysize+1];
for(int i = 0; i < (mysize+1); i++)
{
buffer[i] = carray[i];
}
}
}
Example test file (only thing given to us in this assignment, so I do not take credit for this code):
// StringTest.cpp
// Testing program for the String1030 class.
#include<iostream>
#include "String1030.h"
using std::cout;
using std::cin;
int main()
{
// check the constructors
String1030 s("My string");
String1030 t(s);
String1030 x;
char in_buf[256];
cout << "S size(): " << s.getSize() << endl;
cout << "T size(): " << t.getSize() << endl;
cout << "X size(): " << x.getSize() << endl;
for(int i=0;i<t.getSize();i++)
cout << t[i];
cout << endl;
s[2]='5';
for(int i=0;i<s.getSize();i++)
cout << s[i];
cout << endl;
// check the assignment operator
x=s;
cout << "X: " << x.getString() << endl;
// check the size reset.
x.setSize(30);
cin >> in_buf;
x.setString(in_buf);
cout << "\nx: " << x.getString() << endl;
//more checks on resize
//set to a negative value, nothing should change
s.setSize(-8);
cout << "S size(): " << s.getSize() << endl;
//set to 0, should be 0
s.setSize(0);
cout << "S size(): " << s.getSize() << endl;
//read into the 0 length array should NOT have an error
//and should NOT transfer any characters. Output should not
//have any errors either.
cin >> in_buf;
s.setString(in_buf);
cout << "S after cin>>: " << s.getString() << endl;
//reset to something larger than 0
s.setSize(10);
cout << "S size(): " << s.getSize() << endl;
//read should work now
cin >> in_buf;
s.setString(in_buf);
cout << "S after cin>>: " << s.getString() << endl;
//now the assignment return value
x=t=s;
cout << "T: " << t.getString() << endl;
cout << "X: " << x.getString() << endl;
return 0;
}
It was interesting having to essentially build our own version of the string class. This was probably the hardest assignment of the semester. We had been introduced to pointers literally that day, and had yet to learn operator overloading. Definitely learned a lot from grinding through this though.
Lupus