GNU   davin.50webs.com/research
Bringing to you notes for the ages

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Scruff the Cat       My Life Story          Smoking Cessation          Other Links      
Debugging Macros     String Class I     Linked List System I Java for C Programmers Naming Convention
    String Class II         How I use m4              Strings III                 Symmetrical I/O             Linked Lists II     
Run-Time Type Info   Virtual Methods      An Array System        Science & Religion            Submodes       
  Nested Packages      Memory Leaks    Garbage Collection      Internet & Poverty      What is Knowledge?
Limits of Evolution   Emacs Additions      Function Plotter           Romantic Love        The Next Big Thing
    Science Fiction     Faster Compilation Theory of Morality         Elisp Scoping               Elisp Advice      
  S.O.G.M. Pattern       Safe Properties         School Bullying          Charisma Control          Life and Death    
     Splitting Java          Multiple Ctors       Religious Beliefs         Conversation 1           Conversation 2    
   J.T.W. Language    Emacs Additions II      Build Counter             Relation Plotter          Lisp++ Language  
  Memory Leaks II   Super Constructors CRUD Implementation Order a Website Form There Is An Afterlife
More Occam's Razor C to Java Translator Theory of Morality II


A string class Free Stuff

Abstract

This article presents my first attempt at a C++ string class.

1. Overview

I learnt how program in C with an experience programming in Basic, and so I was both impressed and appalled by C's string handling. Impressed by its efficiency, but appalled by the amount of work required on the programmer's behalf to keep track of memory. Then I learnt Java and Lisp and was impressed how the burden of string memory management had been lifted from the programmer.


When I read C++ creator Bjarne Stroustrup's account of a string class in page 251 of his book The C++ Programming Language, second Edition, I was excited with the possibility that strings could be both easy to use and efficient in implementation. My String class is based on his example, with one extension: the ability to efficiently append text onto an existing string.


Consider the following Java program as an example of the ease of string manipulation:

class Test
{
   int a = {1,2,3,4,5,6,7,8,9,10};

   String compose_string()
   {
      String s = "";
      for (int i=0; i<a.length; i++)
      {
         s = s + a[i];
      }
      return s;
   }

   public static void main(String args)
   {
      System.out.println(compose_string());
   }
}

To show how my own string class can be used, here is the same example in C++ using my class:

#include "output.hh"

int A_SIZE = 10;
int a[A_SIZE] = {1,2,3,4,5,6,7,8,9,10};

String compose_string()
{
   String s = "";
   for (int i=0; i<A_SIZE; i++)
   {
      s << a[i];
   }
   return s;
}

int main(int argc, char** argv)
{
   cout << compose_string() << endl;
}

2. A Technical Note

I have avoided using C++'s I/O (Input/Output) classes, as part of my investigations was into the design of I/O classes in general. Instead I have written my own ones that are based on the C++ ones. My classes are built upon the relevant C classes.

3. The Sources

  • String class
  • Interface file:
output.hh
  • Implementation file:
output.cc
  • Global header file:
   gccprefs.hh
  • Some tester code:
   t-output.cc
  • The complete archive:
   string1.tar.gz


Back to Research Projects
This page has the following hit count:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked Lists II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter | Romantic Love | The Next Big Thing |
| Science Fiction | Faster Compilation | Theory of Morality | Elisp Scoping | Elisp Advice |
| S.O.G.M. Pattern | Safe Properties | School Bullying | Charisma Control | Life and Death |
| Splitting Java | Multiple Ctors | Religious Beliefs | Conversation 1 | Conversation 2 |
| J.T.W. Language | Emacs Additions II | Build Counter | Relation Plotter | Lisp++ Language |
| Memory Leaks II | Super Constructors | CRUD Implementation | Order a Website Form | There Is An Afterlife |
| More Occam's Razor | C to Java Translator | Theory of Morality II
Last modified: Sun Sep 25 16:11:29 NZDT 2016
Best viewed at 800x600 or above resolution.
© Copyright 1999-2016 Davin Pearson.
Please report any broken links to