• Forum
  • Lounge
  • MyString Project... This is for a school

 
MyString Project... This is for a school project.

Here are my guidelines:
You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows.

Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (keep in mind that if you were actually writing a prgram that needs a string, you would use the c++ standard string class, so this is mostly a learning experience to teach yourself the kind of things that go in the implementation of the C++ standard string class.
I'm given a header file to work with and most write the cpp file. I'll post that up here... any help?
Post up the header file so we can see what kind of operations you're professor is wanting you to implement.
Help? Okay. Follow the instructions.
What things are you concerned about not being able to do by yourself?
Sorry been in class, here is the begining part and second to come. It's kinda long:
// Provided by: BRADFORD GERL
// Email Address: bradforddaniel.gerl@calbaptist.edu
// FILE: MyString.h
//
// DETAILED SPECIFICATION OF THE MYSTRING ADT
//
// Typename
// MyString (a class to keep track of a sequence of characters, similar to
// the C++ standard string class but with fewer operations)
//
// Domain
// Each MyString value includes:
// sequence --- the string is stored as a null-terminated dynamic character
// array that sequence points to
// allocated --- the total length of the dynamic array; Note: the total
// number of characters prior to the null character is always
// less than allocated
//
// Operations
// Constructor
// Function: Constructor.
// Input: a C-string (i.e., a null-terminated char array). Output: none.
// Precondition: The input char array is null-terminated (you do not need to
// check this precondition, it is assumed to be satisfied when the user
// uses the constructor).
// Postcondition: sequence now holds the content of the input C-string.
// allocated is one more than the length of the input C-string (so as to
// hold the null character).
//
// Copy Constructor
// Function: Copy constructor.
// Input: a MyString instance. Output: none.
// Precondition: None.
// Postcondition: The input MyString instance has been copied to the current
// instance. Note: the string contained in sequence is copied, but
// not the address of sequence (this is why we write this copy constructor).
//
// Destructor
// Function: Destructor.
// Input: none. Output: none.
// Precondition: None.
// Postcondition: The memory locations for sequence have been deallocated.
//
// getLength
// Function: Function to return the length of the sequence string.
// Input: none. Output: a nonnegative integer number.
// Precondition: None.
// Postcondition: The return value is the number of characters in the
// sequence string (excluding the null character and any character after
// it).
//
// operator []
// Function: Overloaded operator [] for the MyString class.
// Input: a nonnegative integer number position. Output: a character.
// Precondition: position < length of the current MyString instance.
// Postcondition: The value returned is the character at the specified
// position of the sequence string. A string's positions start from 0 at
// the start of the sequence and go up to length-1 at the right end.
//
// reserve
// Function: Function for reserving memory locations.
// Input: a nonnegative integer number n. Output: none.
// Precondition: None.
// Postcondition: Allocate new memory locations consisting of n bytes only if
// n is larger than current allocated. If new memory locations are allocated,
// copy the string to the new memory locations and point sequence to them.
// Also, the old memory locations for the string are deallocated.
//
// operator =
// Function: Overloaded operator = for the MyString class.
// Input: a MyString instance. Output: the current MyString instance.
// Precondition: none.
// Postcondition: The input MyString instance has been assigned to the current
// instance, and the current MyString instance is returned (so we may use
// multiple assignment such as s1 = s2 = s3). Note: the string contained
// in sequence is copied, but not the address of sequence. (Note: Be sure to
// develop your code so that self-assignment can be taken care of.)
//
// operator +=
// Function: Overloaded operator += for the MyString class.
// Input: a MyString instance. Output: none.
// Precondition: none.
// Postcondition: The input MyString instance has been concatenated to the

// end of the current MyString instance. Note: If allocated of the current
// instance is to small to hold the result, update allocated to be one
// more than the sum of the lengths of the input instance and the current
// instance and use Reserve function to allocate new meory locations.
//
// operator +=
// Function: Overloaded operator += for the MyString class.
// Input: a C-string. Output: none.
// Precondition: none.
// Postcondition: The input C-string has been concatenated to the
// end of the current MyString instance. Note: If allocated of the current
// instance is to small to hold the result, update allocated to be one
// more than the sum of the lengths of the input C-string and the current
// instance and use reserve function to allocate new meory locations.
//
// operator +=
// Function: Overloaded operator += for the MyString class.
// Input: a character. Output: none.
// Precondition: none.
// Postcondition: The input character has been catenated to the
// end of the current MyString instance. Note: If allocated of the current
// instance is to small to hold the result, increment allocated by one and
// use Reserve function to allocate new meory locations.
//
// NON-MEMBER Friend Functions for the MyString Class:
//
// operator +
// Function: Overloaded binary operator + for the MyString class.
// Input: two MyString instances. Output: a MyString instance.
// Precondition: none.
// Postcondition: The returned result is a new MyString instance that looks
// as if the second input MyString instance has been concatenated to the
// end of the first input MyString instance. The new MyString instance's
// allocated is one more than the sum of the lengths of the two input strings.
//
// operator ==
// Function: Overloaded operator == for the MyString class.
// Input: two MyString instances. Output: true or false.
// Precondition: none.
// Postcondition: true is returned if the C-strings of the two MyString
// instances are the same (allocated does not need to be the same); otherwise,
// false is returned.
//
// operator !=
// Function: Overloaded operator != for the MyString class.
// Input: two MyString instances. Output: true or false.
// Precondition: none.
// Postcondition: true is returned if the C-strings of the two MyString
// instances are not the same; otherwise, false is returned.
//
// operator >=
// Function: Overloaded operator >= for the MyString class.
// Input: two MyString instances. Output: true or false.
// Precondition: none.
// Postcondition: true is returned if the C-string of the first instance
// is lexicographically equal to or after the C-string of the second instance;
// otherwise, false is returned (e.g., "Mary" >= "Alice").
//
// operator <=
// Function: Overloaded operator >= for the MyString class.
// Input: two MyString instances. Output: true or false.
// Precondition: none.
// Postcondition: true is returned if the C-string of the first instance
// is lexicographically equal to or before the C-string of the second instance;
// otherwise, false is returned (e.g., "John" <= "Walter").
//
// operator >
// Function: Overloaded operator > for the MyString class.
// Input: two MyString instances. Output: true or false.
// Precondition: none.
// Postcondition: true is returned if the C-string of the first instance
// is lexicographically after the C-string of the second instance;
// otherwise, false is returned.
//
And for this header below, I want to break it down and work on each piece individually, and right now I am tring to write the string for the
size_t getlength() function.

#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
#include <stdlib.h> // Provides size_t
using namespace std;

// Some compilers require that we prototype the << and >> operator functions
// outside the class. For this reason, we have added the following 3 lines to the
// MyString.h class specificaiton file.
class MyString; // Forward Declaration
ostream& operator <<(ostream&, const MyString&);
istream& operator >>(istream&, MyString&);

class MyString
{
public:
// CONSTRUCTORS and DESTRUCTOR
MyString(const char str[ ] = "");
MyString(const MyString& source); // This is the copy constructor
~MyString( );

// CONSTANT MEMBER FUNCTIONS
size_t getLength( ) const;
char& operator[](size_t position) const;*/

// MODIFICATION MEMBER FUNCTIONS
void reserve(size_t n);
const MyString& operator =(const MyString& source);
void operator +=(const MyString& addend);
void operator +=(const char addend[]);
void operator +=(char addend);

// FRIEND FUNCTIONS
friend MyString operator +(const MyString& addend1, const MyString& addend2);
friend bool operator ==(const MyString& s1, const MyString& s2);
friend bool operator !=(const MyString& s1, const MyString& s2);
friend bool operator >=(const MyString& s1, const MyString& s2);
friend bool operator <=(const MyString& s1, const MyString& s2);
friend bool operator > (const MyString& s1, const MyString& s2);
friend bool operator < (const MyString& s1, const MyString& s2);
friend ostream& operator <<(ostream& outs, const MyString& source);
friend istream& operator >>(istream& ins, MyString& target);
friend istream& getline(istream& ins, MyString& target);

private:
char *sequence;
size_t allocated; // size_t is defined as an unsigned int type. Using size_t
// rather than unsigned int for string length leads to
// better portability of the code.
};

#endif
Why are loads of the operators friend functions?
Why no code tags?
My professor said that some ways could be quicker but that this is a learning experience and that's why we have to work with what he gave us. I'm assuming that's why the operators are friend functions.
Sorry when I copied it code tags didn't make it for some reason. Probably makes it harder to read haha.
Oh and any help on writting the size_t getlength() function?
closed account (3qX21hU5)
What do you have so far? And you might want to edit your code so you have code tags (highlight all your code then press the <> off to the right )
According to the instructions an instance may have more elements than are used. Since allocated = size of allocated array and there is no size_t length member in which to cache the number of chars preceding the '\0' I think you will have to count the elements in the getlength() function.
Something like:
1
2
3
4
5
6
7
size_t getlength() const
{
    size_t length = 0;
    while( sequence[length] != '\0' && length < allocated )
        assign a new value to length  <- substitute code here (one line)
    return length;
}

Veltas wrote:
Why are loads of the operators friend functions?

This constructor MyString(const char str[ ] = ""); allows the compiler to "cast" a character array to a MyString object.
When the operators are normal member functions the left operand must be a MyString object (it calls the function), but the right operand can be a character array because of the above constructor.
Since the operators are friend functions taking 2 arguments either operand (or even both operands) can be a character array. The friend functions increase the types of expressions that can be handled.
Eg.
MyString = char[] + MyString couldn't be done with a regular member function.

EDIT: I added the 2nd condition on line 4 && length < allocated just to be safe, but it shouldn't be necessary. By making sequence private you can write the member functions so as to ensure that there is always a '\0' in the array (a benefit of encapsulation).
Last edited on
I just joined this site, and am looking to better myself in writting code. I am new and didn't know where to post for help?
Also would this kinda follow what I need:
nlength = 1;
nstring = new char[nlength];
nstring[nlength-1] = '\0';
Topic archived. No new replies allowed.