![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2006
Posts: 25
Rep Power: 0
![]() |
Implementing a dynamic class
I'm working on designing and implementing a mystring class defined by a character pointer sequence that hold an address of the dynamic array of characters that make up the string, an integer 'curr_length' representing the number of characters in the string, and an integer 'allocated' that holds the current capacity of the string. My problem is I can't figure out the logic to overload the three different += operators.
here is the API for the += operators: mystring& operator+= (const mystring & addend); mystring& operator+= (char addend); mystring& operator+= (const char addend[]); //POST: addend is appended to the tail of string here is my code for the class so far(I've removed my attempt of coding += to save from embarrassment): [HTML]#ifndef MY_STR #define MY_STR #include <iostream> #include <vector> using namespace std; class mystring { istream& getline (istream& in, mystring& target); friend bool operator < (const mystring& lhs, const mystring& rhs); friend bool operator > (const mystring& lhs, const mystring& rhs); friend bool operator >= (const mystring& lhs, const mystring& rhs); friend bool operator <= (const mystring& lhs, const mystring& rhs); friend bool operator == (const mystring& lhs, const mystring& rhs); friend bool operator != (const mystring& lhs, const mystring& rhs); private: char * sequence; unsigned int allocated; unsigned int curr_length; void reserve(unsigned int n, bool copy); public: mystring(const char str []=""); mystring(const mystring& source); ~mystring(){ delete [] sequence; } int length() const; char operator[] (int pos) const; }; //constructor mystring::mystring(const char str[]){ int i; sequence = NULL; allocated = 0; curr_length = 0; if ( str==" ") return; //reserve space - count char is string for(i=0; str[i]!='\0'; i++) reserve(i,false); //copy str to this string for(i=0;str[i]!='\0';i++) sequence[i] = str[i]; //update size curr_length=i; } void mystring::reserve(unsigned int n, bool copy){ char* ptr=new char [n]; if(ptr==NULL){ cout<<"allocation failure"; exit(1); } if(copy){ for(unsigned int i = 0; i < curr_length; i++) ptr[i] = sequence[i]; } if(sequence!=NULL) delete [] sequence; sequence = ptr; allocated = n; } int mystring::length() const{ return curr_length; } istream& getline (istream& in, mystring& target){ //look for the new line char char ch; target=""; in.get(ch); while (ch!='/n'){ target+=ch; in.get(ch); } return in; } bool operator < (const mystring& lhs, const mystring& rhs){ unsigned int i=0; while(i<lhs.curr_length && i<rhs.curr_length){ if (lhs[i] > rhs[i]) return false; else if (lhs[i] < rhs[i]) return true; i++; } return (lhs.curr_length < rhs.curr_length); } bool operator > (const mystring& lhs, const mystring& rhs){ return rhs < lhs; } bool operator >= (const mystring& lhs, const mystring& rhs){ return (rhs > lhs || lhs == rhs); } bool operator <= (const mystring& lhs, const mystring& rhs){ return (rhs < lhs || lhs == rhs); } bool operator == (const mystring& lhs, const mystring& rhs){ int i=0; if (lhs.curr_length==rhs.curr_length){ if (lhs[i]==rhs[i]) return true; else return false; } else return false; } bool operator != (const mystring& lhs, const mystring& rhs){ return !(lhs==rhs); } #endif[/HTML] |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I would suggest that people would rather look at your (embarrassing) code in an attempt to pick up and point out the errors, than write the code and present it to you. Just a guess, you understand.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2006
Posts: 25
Rep Power: 0
![]() |
well I don't really have any code for it. I don't really know where to start with this one...I know that once i have the += operator that handles one char at a time I can then use that to code the other two but other than that I have nothing.
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2006
Posts: 25
Rep Power: 0
![]() |
so i got the code for the += that adds one char on, and i think i have the one for an entire mystring object.
mystring& operator+= (char addend); [HTML]mystring& mystring::operator +=(char addend){ if(allocated==curr_length) reserve(1+curr_length,true); sequence[curr_length]=addend; curr_length++; return *this; }[/HTML] I think i have miplaced the loop for this one mystring& operator+= (const mystring & addend); [HTML]mystring& mystring::operator +=(const mystring &addend){ if(allocated==curr_length) reserve(1+curr_length,true); sequence[curr_length]=addend; for (unsigned int i=0; i<=curr_length; i++){ curr_length++; return *this; } }[/HTML] only problem is that I don't know how to program mystring& operator+= (const char addend[]); |
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
That code will only add one character to the length, even if the addend string is more than that.
As you said originally, writing the += that takes a mystring is easy when you can use your += that takes a char mystring& mystring::operator +=(const mystring &addend){
for (unsigned int i=0; i<=addend.length(); i++)
*this += addend[i];
return *this;
}It may not be the most efficient way of doing it, but it should work. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Oct 2006
Posts: 25
Rep Power: 0
![]() |
so im almost finished with my code but i am getting some error from my overloading istream and overloading + operator.
here is what each should do: operator>> //input the mystring object, delimit with white space characters I know my code for >> is pretty much complete garbage. operator+ //take two mystring objects. The string returned is the concatenation of the two arguments [HTML]#ifndef MY_STR #define MY_STR #include <iostream> #include <vector> using namespace std; class mystring { istream& getline (istream& in, mystring& target); friend bool operator < (const mystring& lhs, const mystring& rhs); friend bool operator > (const mystring& lhs, const mystring& rhs); friend bool operator >= (const mystring& lhs, const mystring& rhs); friend bool operator <= (const mystring& lhs, const mystring& rhs); friend bool operator == (const mystring& lhs, const mystring& rhs); friend bool operator != (const mystring& lhs, const mystring& rhs); friend mystring& operator + (const mystring& lhs, const mystring& rhs); private: char * sequence; unsigned int allocated; unsigned int curr_length; void reserve(unsigned int n, bool copy); public: mystring(const char str []=""); mystring(const mystring& source); ~mystring(){ delete [] sequence; } int length() const; char operator[] (int pos) const; mystring& operator=(const mystring & source); mystring& operator+=(const mystring & addend); mystring& operator+=(char addend); mystring& operator+=(const char addend[]); friend ostream& operator << (ostream& out, const mystring& rhs); friend istream& operator >> (istream& in, const mystring& rhs); }; //constructor mystring::mystring(const char str[]){ int i; sequence = NULL; allocated = 0; curr_length = 0; if ( str==" ") return; //reserve space - count char is string for(i=0; str[i]!='\0'; i++) reserve(i,false); //copy str to this string for(i=0;str[i]!='\0';i++) sequence[i] = str[i]; //update size curr_length=i; } mystring::mystring(const mystring &source){ //create the vector and initialize vSize, vCapacity, and vArr sequence = NULL; allocated = 0; curr_length = 0; //if size is 0, we're done if(source.curr_length==0) return; //if size is not 0, then reserve the space reserve(source.curr_length, false); //update curr_length curr_length = source.curr_length; //initialize the elements with source for(unsigned int i=0; i<source.curr_length; i++) sequence[i]=source.sequence[i]; } void mystring::reserve(unsigned int n, bool copy){ char* ptr=new char [n]; if(ptr==NULL){ cout<<"allocation failure"; exit(1); } if(copy){ for(unsigned int i = 0; i < curr_length; i++) ptr[i] = sequence[i]; } if(sequence!=NULL) delete [] sequence; sequence = ptr; allocated = n; } int mystring::length() const{ return curr_length; } char mystring::operator [](int pos) const{ return sequence[pos]; } mystring& mystring::operator=(const mystring& source){ //do we need more space? if(source.curr_length>allocated) reserve(source.curr_length, false); //update the size of the miniVec curr_length = source.curr_length; //copy source elements for(unsigned int i=0; i<curr_length; i++) sequence[i]=source.sequence[i]; //return the updated object return *this; } mystring& mystring::operator +=(const mystring &addend){ if(allocated<(curr_length+addend.length())) reserve(addend.length()+curr_length,true); for (int i=0; i<=addend.length(); i++){ sequence[curr_length] = addend[i]; curr_length++; } return *this; } mystring& mystring::operator +=(char addend){ if(allocated==curr_length) reserve(1+curr_length,true); sequence[curr_length]=addend; curr_length++; return *this; } mystring& mystring::operator +=(const char addend[]){ int i; for(i=0;addend[i]!='\0';i++); if(allocated<curr_length+i) reserve(curr_length+i,true); for(int j=0; j<=i; j++){ sequence[curr_length] = addend[j]; curr_length++; } return *this; } ostream& operator << (ostream& out, const mystring& rhs){ unsigned int i=0; while (i<rhs.curr_length){ out<<rhs.sequence[i]; i++; } } istream& operator >> (istream& in, const mystring& rhs){ rhs.curr_length=0; char ch; rhs=""; in.get(ch); while(!isspace(ch)){ rhs+=ch; in.get(ch); } return in; } istream& getline (istream& in, mystring& target){ //look for the new line char char ch; target=""; in.get(ch); while (ch!='/n'){ target+=ch; in.get(ch); } return in; } bool operator < (const mystring& lhs, const mystring& rhs){ //is lhs less than rhs unsigned int i=0; while(i<lhs.curr_length && i<rhs.curr_length){ if (lhs[i] > rhs[i]) return false; else if (lhs[i] < rhs[i]) return true; i++; } return (lhs.curr_length < rhs.curr_length); } bool operator > (const mystring& lhs, const mystring& rhs){ //is rhs less than lhs return rhs < lhs; } bool operator >= (const mystring& lhs, const mystring& rhs){ //is lhs less than or equal to rhs return (rhs > lhs || lhs == rhs); } bool operator <= (const mystring& lhs, const mystring& rhs){ //is rhs less than or equal to lhs return (rhs < lhs || lhs == rhs); } bool operator == (const mystring& lhs, const mystring& rhs){ int i=0; //compare lhs and rhs to see if they equal each other if (lhs.curr_length==rhs.curr_length){ if (lhs[i]==rhs[i]) return true; else return false; } else return false; } bool operator != (const mystring& lhs, const mystring& rhs){ return !(lhs==rhs); } mystring& operator + (const mystring lhs, const mystring rhs){ mystring holder=lhs; lhs+=rhs; return holder; } #endif[/HTML] |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Wierd compile Error. Need help please. | Keiyentai | Java | 7 | Aug 19th, 2006 2:35 AM |
| URL class | Eric the Red | Java | 5 | Jun 24th, 2006 10:01 PM |
| What is: "Oriented programming (OO)?" | BrinyCode | C++ | 12 | Nov 22nd, 2005 8:40 AM |
| User Input for Number Format | ericelysia1 | Java | 0 | Jul 21st, 2005 4:41 PM |
| MFC/OpenGL: problem with 'Document' class | brenda | C++ | 11 | May 23rd, 2005 9:10 PM |