Thread: Home-made string class constructor

  1. #1
    Registered User
    Join Date
    Mar 2009

    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.

    class String
        unsigned Capacity;  // Number of memory locations reserved
        unsigned Length;    // Number of memory locations in use
        char * Mem;         // Pointer to memory to hold characters
        // Construct empty 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
    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
    This looks suspiciously like "Home-made" String Class constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2008
    Quote Originally Posted by laserlight View Post
    This looks suspiciously like "Home-made" String Class constructor.
    And another one:

  5. #5
    Join Date
    Oct 2007
    Inside my computer
    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, 02: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, 09:58 AM
  4. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM