-
C++ (
http://www.daniweb.com/forums/forum8.html)
| neologic | Aug 23rd, 2005 9:55 am | |
| Whats wrong with this class??? class String
{
char* pData;
public:
String(); //default constructor
~String(); //default destructor
String( String&); //copy constructor
const String& operator=( String&); //overloaded assignment operator
void operator==( const String&); //overloaded compare operator
String operator+( String); //overloaded concat operator
void operator[]( int); //index operator
String( char*); //one argument operator
char* GetBuffer() { return pData;} //to get pData
inline int Size(); //to find the length of pData;
void ToUpper(); //convert to uppercase
void ToLower(); //convert to lowercase
void SubString( int, int); //to get a sub string
void LeftTrim(); //to trim spaces on left
//void RightTrim(); //to trim spaces on right
};
String::String()
{
//cout << "Constructor";
pData = NULL;
}
String::~String()
{
//cout << "Destructor";
delete pData;
}
String::String( String& strn1)
{
//cout << "Copy Constructor" << endl;
int len1;
len1 = strn1.Size();
if ( pData)
delete pData;
pData = new char[len1];
strcpy( pData, strn1.pData);
}
const String& String::operator=( String& strn2)
{
//cout << "Overloaded = Operator" << endl;
int len2;
if( this == &strn2)
return *this;
len2 = strn2.Size();
if( pData)
delete pData;
pData = new char[len2];
strcpy( pData, strn2.pData);
return *this;
}
void String::operator==( const String& strn3)
{
//cout << "Inside ==";
if( *strn3.pData == *pData)
cout << "The strings match" << endl;
else
cout << "The strings dont match" << endl;
}
String String::operator+( String strn4)
{
int len3, plen, totlen;
len3 = strn4.Size();
plen = Size();
totlen = len3 + plen;
char* temp = new char[totlen];
for ( int i = 0; i < plen; i++)
{
temp[i] = pData[i];
}
if( pData)
delete pData;
pData = new char[totlen];
for ( int i = 0; i < len3; i++)
{
temp[plen + i] = strn4.pData[i];
}
String newtemp( temp);
return newtemp;
/*
for ( int i = 0; i < totlen; i++)
{
cout << temp[i];
} */
cout << endl;
}
void String::operator[]( int posi)
{
char ch;
ch = pData[posi - 1];
cout << ch << endl;
}
String::String( char* strn5)
{
//cout << "One arg Constructor" << endl;
pData = strn5;
}
inline int String::Size()
{
return strlen(pData);
}
void String::ToUpper()
{
int len4;
len4 = Size();
char* temp = new char[len4];
temp = pData;
char* ch = new char[len4];
for( int i = 0; i < len4; i++)
{
ch[i] = toupper(*temp);
temp++;
}
for( int i = 0; i < len4; i++)
{
cout << ch[i];
}
}
void String::ToLower()
{
int len5;
len5 = Size();
char* ch = new char[len5];
for ( int i = 0; i < len5; i++)
{
ch[i] = tolower(*pData);
pData++;
}
for( int i = 0; i < len5; i++)
{
cout << ch[i];
}
}
void String::SubString( int pos, int range)
{
int len6;
len6 = Size();
char* str = new char[len6];
str = pData;
for( int i = pos - 1; i < range + pos - 1; i++)
{
cout << str[i];
}
cout << endl;
}
void String::LeftTrim()
{
int count = 0, len7;
len7 = Size();
char *pTmp = new char[Size()];
pTmp = pData;
if( pData)
delete pData;
for( int i = 0; i < len7; i++)
{
if( *pTmp == ' ')
{
*pTmp++;
count++;
}
}
for( int i = 0 ; i < len7 - count; i++)
{
cout << pTmp[i];
}
cout << endl;
}
void String::RightTrim()
{
int count = 0, len8;
len8 = Size();
char *pTmp = new char[Size()];
pTmp = (pData + len8);
if( pData)
delete pData;
for( int i = len8; i > 0; i--)
{
if( *(pTmp - 1) == ' ')
{
*pTmp--;
count++;
}
}
char *pnewTmp = new char[Size()];
pnewTmp = pTmp;
if( pTmp)
delete pTmp;
for( int i = 0; i < len8; i++)
{
cout << pnewTmp[i];
}
cout << endl;
} |
| Narue | Aug 23rd, 2005 10:06 am | |
| Re: Whats wrong with this class??? >Whats wrong with this class???
It has no point because the standard library already provides an excellent string class. That said, what is it not doing that you want it to? Don't expect us to test and debug your program just because you didn't give us enough information about your problem.
Though at a glance, your code is not exception safe because it leaves the object in an unpredictable state if any of your allocations fail, and you have a bunch of unnecessary member functions that could easily be non-members. And your index operator is...funky. |
| neologic | Aug 23rd, 2005 10:30 am | |
| Hey Sorry Sorry guys did not post the questions....
I was trying to implement a String class similar to the one in Java in C++...Also i have used the bloodshe dev c++ compiler 4.0
I have a few questions regarding this:
1. I am passing a constant String reference to one of the methods and finding its size using Size()....but still its giving an error there saying constant functions cannot be modified.
2. There seems to be a problem in the overloaded operators....
cud some1 help me?? |
| Ancient Dragon | Aug 23rd, 2005 11:33 am | |
| Re: Hey Sorry you need to make Size() method const
inline int Size() const; //to find the length of pData; |
| Stoned_coder | Aug 23rd, 2005 6:27 pm | |
| Re: Whats wrong with this class??? If you are doing this as a learning exercise then I agree with narue. You need to think a little more about what would happen in the face of an exception when you design a class. Heres some thoughts from Herb Sutter . You would do well to heed most advice he gives. Read through the guru of the week exercises especially the ones about class design, exception safety and pimpl idiom. Then fix the problems with your code. Funky isnt really the word for your subscript operator. You should have two not one and neither returns a void. one returns const char the other returns char&. |
| All times are GMT -4. The time now is 11:10 am. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC