20-CS-122-001 Computer Science II Spring 2012
A String Class

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev      Next      All Lectures      Source

//A String Class 
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <string.h>
using namespace std;

class String {
   int length;
   char *buf;

 public:
   String();
   String(char *s);
   String(char c, int n);
   void set(int index, char newchar);
   char get(int index);
   int  getLength() { return length; }
   void append(char *addition);
   String *operator=(String &other);
   String *copy(String *other);
   String *bcopy(String *other);   
   void display();
   ~String();
};

String::String() {
   buf = 0;
   length = 0;
}

String::String(char *s) {
   length = strlen(s);
   buf = new char[length+1];
   strcpy(buf, s);
}

String::String(char c, int n) {
   length = n;
   buf = new char[length+1];
   memset(buf, c, length);
   buf[length] = 0;
}

void String::set(int index, char newchar) {
   if (index > 0 && index <= length) buf[index-1] = newchar;
}
      
char String::get(int index) {
   if (index > 0 && index <= length) return buf[index-1];
   else return 0;
}

void String::append(char *addition) {
   char *temp;

   length += strlen(addition);
   temp = new char[length+1];
   sprintf(temp, "%s%s", buf, addition);
   delete buf;
   buf = temp;
}

String *String::operator=(String &other) {
   length = other.length;
   delete buf;
   buf = new char[length+1];
   strcpy(buf, other.buf);
   return this;
}

String *String::copy(String *other) {
   length = other->length;
   delete buf;
   buf = new char[length+1];
   strcpy(buf, other->buf);
   return this;
}

String *String::bcopy(String *other) {
   length = other->length;
   char *tmpbuf = new char[length+1];
   strcpy(tmpbuf, other->buf);
   delete buf;
   buf = tmpbuf;
   return this;
}

void String::display() {
   cout << "The string: " << buf << '\n';
}

String::~String() {
   delete buf;
}

int main() {
   String mystring("This is a string");
   String ostring("");
   String *a = new String("");
   String *b = new String("");
   String *c = new String("Hello There");
   a = b = c;
   
   String  d("");
   String  e("");
   String  f("Hello There");

   mystring.set(1,'t');
   mystring.display();
   ostring = mystring;
   ostring.display();
   mystring.append(".  And now an appendage.");
   mystring.display();
   ostring.display();
   
   cout << "------\n";
   a->display();
   b->display();
   c->display();
   cout << "------\n";
   a->set(1,'*');
   b->set(2,'-');
   a->display();
   b->display();
   c->display();
   
   cout << "======\n";
   a->copy(b->copy(c));
   a->display();
   b->display();
   c->display();
   cout << "------\n";
   a->set(1,'*');
   b->set(2,'-');
   a->display();
   b->display();
   c->display();

   a = new String("");
   b = new String("");
   c = new String("Hello There");
   a = b = c;
   
   cout << "++++++\n";
   a->display();
   b->display();
   c->display();
   cout << "------\n";
   a->set(1,'*');
   b->set(2,'-');
   a->display();
   b->display();
   c->display();
   
   a = new String("");
   b = new String("");
   c = new String("Hello There");
   a = b = c;
	
   cout << "======\n";
   a->bcopy(b->bcopy(c));
   a->display();
   b->display();
   c->display();
   cout << "------\n";
   a->set(1,'*');
   b->set(2,'-');
   a->display();
   b->display();
   c->display();

   cout << "A:" << a << "\n";
	
   cout << "done\n";
}

// Sample Output:
//   The string: this is a string
//   The string: this is a string
//   The string: this is a string.  And now an appendage.
//   The string: this is a string
//   ------
//   The string: Hello There
//   The string: Hello There
//   The string: Hello There
//   ------
//   The string: *-llo There
//   The string: *-llo There
//   The string: *-llo There
//   ======
//   The string: 0@0@o
//   The string: 0@0@o
//   The string: 0@0@o
//   ------
//   The string: *-@0@o
//   The string: *-@0@o
//   The string: *-@0@o
//   ++++++
//   The string: Hello There
//   The string: Hello There
//   The string: Hello There
//   ------
//   The string: *-llo There
//   The string: *-llo There
//   The string: *-llo There
//   ======
//   The string: Hello There
//   The string: Hello There
//   The string: Hello There
//   ------
//   The string: *-llo There
//   The string: *-llo There
//   The string: *-llo There
//   A:0x804aca0
//   done