Home-made string class constructor

This is a discussion on Home-made string class constructor within the C++ Programming forums, part of the General Programming Boards category; I have to construct a class for strings. C++ already has one, but my assignment is a home-made version. I ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    Question Home-made string class constructor

    I have to construct a class for strings. C++ already has one, but my assignment is a home-made version. I am very lost as to how to make the constructor work. I have toyed with a few things, but I was given a header file to follow and I have no clue what to do. Any help would be appreciated.

    Code:
    class String
    {
      private:
    
        unsigned Capacity;  // Number of memory locations reserved
        unsigned Length;    // Number of memory locations in use
        char * Mem;         // Pointer to memory to hold characters
    
      public:
    
        // Construct empty string
        //
        String()
        {
          Mem = NULL;
          Capacity = 0;
          Length = 0;
        }
    I am clueless as to what to do. The constructor seems to be giving me the most trouble, I am not even sure where to start. All the other parts of the assignment seem possible, but the constructor is confusing me with the Capacity, Length, and * Mem.

    Please help with comments, explanations, example code, or anything you think that would be helpful.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Other than the fact that you've already written it, what's confusing to you? The point of a constructor is to make sure each member variable has a meaningful value. For instance, if a char * was passed into a(nother) constructor, you could use the data pointed to to determine the length, allocate some memory into Mem and copy the string over, and set the capacity appropriately.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,165
    This looks suspiciously like "Home-made" String Class constructor.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    1
    Quote Originally Posted by laserlight View Post
    This looks suspiciously like "Home-made" String Class constructor.
    And another one: http://www.daniweb.com/forums/thread180981.html

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Tsk, tsk. Even posted here after having got several responses on the other forums.
    How low can you sink...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. how get string vector from class in file?
    By tord in forum C++ Programming
    Replies: 3
    Last Post: 06-17-2005, 10:58 AM
  4. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 11:43 AM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21