CIS-255 Home Page: http://www.c-jump.com/bcc/c255c/c255syllabus.htm

Project 2/Part 2: Overloaded substring operators


Description

The purpose of this assignment is to add a set of overloaded operators to the substring class that you implemented earlier.

Instructions

  1. Load substring project into your development environment.

  2. Add the following declarations to the substring class header file, substring.h:

    
    // CIS-255 substring class
    
    #ifndef _CIS62_SUBSTRING_H_INCLUDED_
    #define _CIS62_SUBSTRING_H_INCLUDED_
    
    #include <iostream>
    #include <string>
    
    class substring {
        //...
    public:
        friend std::ostream& operator<<( std::ostream& os, substring const& sub );
    
        // Operators overloaded as members functions:
    
        // Assignments:
        substring& operator=( substring const& other );
        substring& operator=( std::string const& str );
    
        // Merge with another substring:
        substring& operator+=( substring const& other );
    
        // Compare with another substring:
        bool operator>( substring const& other ) const;
        bool operator<( substring const& other ) const;
    
        // Subscript access to individual characters within substring:
        char& operator[]( size_t idx );
        char operator[]( size_t idx ) const;
    
    };//class substring
    
    // Operators overloaded as non-member functions:
    std::ostream& operator<<( std::ostream& os, substring const& sub );
    
    // Equality/Inequality tests between two substrings:
    bool operator==( substring const& one, substring const& another );
    bool operator!=( substring const& one, substring const& another );
    
    // Equality/Inequality tests between substring and std::string:
    bool operator==( std::string const& str, substring const& sub );
    bool operator==( substring const& sub, std::string const& str );
    bool operator!=( std::string const& str, substring const& sub );
    bool operator!=( substring const& sub, std::string const& str );
    
    // Equality/Inequality tests between substring and zero-terminated string:
    bool operator==( substring const& sub, char const* cstr );
    bool operator!=( substring const& sub, char const* cstr );
    bool operator==( char const* cstr, substring const& sub );
    bool operator!=( char const* cstr, substring const& sub );
    
    #endif // _CIS62_SUBSTRING_H_INCLUDED_
    
    
    
  3. Open substring.cpp file and add code for each operator function. For example,

    
    // substring.cpp
    substring& substring::operator=( substring const& other )
    {
        if ( this == &other ) // beware of self assignment!
            return *this;
    
        m_begin = other.m_begin;
        m_end = other.m_end;
        m_parse_begin = other.m_parse_begin;
        m_parse_success = other.m_parse_success;
    
        return *this;
    }
    
    
  4. Test your code against this new version of the main driver: substr2_main.cpp.