C++ Help

Discussion in 'OT Technology' started by Kieffer87, Jan 25, 2007.

  1. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    So this is our assignment and I am really having some trouble figuring out wtf I need to do here. I know how to setup an array class but im not sure of how to setup a string class. Also I am unsure of how to go about overloading the operators and teh assignment and comparison. Any help is greatly appreciated. I tryed going to the teacher but he was of no help, only thing he offered me was to tell me an array class is similar but you need to modify it for string class and then pretty much told me to be on my way. So I figured maybe the power of OT could help. So I greatly appreciate it if someone could help me out. Here is what I have put together so far. and the stuff in bold is the assignment he gave us.

    Write a class to handle strings. As there is a C++ class called 'string', use another name such as String (uppercase). Your class should be based on a char * pointer and an array of char's will be allocated and its address placed in this pointer. As strings grow, de-allocate the old small string and allocate a new, bigger one. Maintain a private integer that tells you how long the string you allocated is. Overload the assignment, and comparison operators to permit String to String assignment and comparison and String to null terminated char array assignment and comparison (assign a char * string to a String only). Overload the + operator to concatenate two strings. Include a function that returns string length. You also need a destructor as well as a constructor.

    Code:
    #include <iostream>
    
    class String{
        
        Private:
        char*ptr;
        char getsize();
        }
    
    String::Array( int arraySize ){
        size = ( arraySize > 0 ? arraySize : 10 ); 
        ptr = new int[ size ]; // create space for array
        assert( ptr != 0 );    // terminate if memory not allocated
        ++arrayCount;          // count one more object
    
        for ( int i = 0; i < size; i++ )
            ptr[ i ] = 0;          // initialize array
        } // end Array constructor
    
    String::~Array(){
        delete [] ptr;            // reclaim space for array
        --arrayCount;             // one fewer objects
        } // end Array destructor
    
    int Array::getSize() const { return size; }
    
    const Array &Array::operator=( const Array &right ){
        if ( &right != this ) {  // check for self-assignment
          
            if ( size != right.size ) {
                delete [] ptr;         // reclaim space
                size = right.size;     // resize this object
                ptr = new int[ size ]; // create space for array copy
                assert( ptr != 0 );    // terminate if not allocated
                  } 
    
              for ( int i = 0; i < size; i++ )
            ptr[ i ] = right.ptr[ i ];  // copy array into object
        }
    
    return *this;
    }
    
     
  2. Why

    Why New Member

    Joined:
    Aug 1, 2006
    Messages:
    158
    Likes Received:
    0
    This crap will take forever, I'm going to do it in a bit...
     
  3. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    I greatly appreciate you helping out! I have been looking through 3 different books over the past few days and I must be stupid or somthing but I just can't figure it out. I think im going to go out and get a C++ for dummies book and just read that before we get another assignment, hoping it will help out.
     
  4. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    The size should be a private member, but the accessor method getSize() should be a public method. And it should return an int.

    class String {
    private:
    char *str;
    int size;

    public:
    String() : size(0) {} // default constructor
    int getSize() { return size; } // accessor method
    };
     
  5. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    you've got the right idea. don't know why youre allocating ptr with an int declaration. it should be with ptr = new char[size]; in additions to the corrections already listed. those should set you on your way. theres tons of examples of operator overloading on the web.
     
  6. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    Thanks guys I greatly appreciate it! I'll let you know if I run into any more problems!!
     

Share This Page