Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 31st, 2006, 12:34 PM   #1
fhslacrosse13
Newbie
 
Join Date: Oct 2006
Posts: 25
Rep Power: 0 fhslacrosse13 is on a distinguished road
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]
fhslacrosse13 is offline   Reply With Quote
Old Oct 31st, 2006, 1:30 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Oct 31st, 2006, 2:04 PM   #3
fhslacrosse13
Newbie
 
Join Date: Oct 2006
Posts: 25
Rep Power: 0 fhslacrosse13 is on a distinguished road
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.
fhslacrosse13 is offline   Reply With Quote
Old Oct 31st, 2006, 2:16 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Scorpions4Ever's tutorial on Operator-Assignment overloading
__________________
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
DaWei is offline   Reply With Quote
Old Oct 31st, 2006, 10:32 PM   #5
fhslacrosse13
Newbie
 
Join Date: Oct 2006
Posts: 25
Rep Power: 0 fhslacrosse13 is on a distinguished road
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[]);
fhslacrosse13 is offline   Reply With Quote
Old Nov 1st, 2006, 1:01 AM   #6
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
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;
}
similarly the one to go through const char addend[] can do the same loop, except you change the for loop to test for addend[i] == '\0', assuming you are expecting zero terminated strings.

It may not be the most efficient way of doing it, but it should work.
The Dark is offline   Reply With Quote
Old Nov 1st, 2006, 4:56 PM   #7
fhslacrosse13
Newbie
 
Join Date: Oct 2006
Posts: 25
Rep Power: 0 fhslacrosse13 is on a distinguished road
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]
fhslacrosse13 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:01 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC