lol ok
HEADER FILE:
Code:
#ifndef __cDATE_H
#define __cDATE_H
#include <iostream.h>
#include <string.h>
class cSTRING
{
protected:
int _nCmpValue, _num1, _num2;
char *pszName1, *pszName2;
public:
cSTRING();
cSTRING(char *pname, int returnVal);
cSTRING(char *pszStr1);
cSTRING(cSTRING &rhs);
~cSTRING();
char *getName1()
{
return this->pszName1;
}
char *getName2()
{
return this->pszName2;
}
void print()
{
cout << pszName1 << endl;
}
cSTRING& operator=(const cSTRING& cStr);
cSTRING operator+(const cSTRING& cStr);
cSTRING& operator+=(const cSTRING& cStr);
int operator==(const cSTRING& cStr);
int operator!=(const cSTRING& cStr);
cSTRING& Reverse();
friend ostream &operator<<(ostream &o, cSTRING& cStr);
friend ostream &operator>>(ostream &o, cSTRING& cStr);
//cSTRING& strrev(cSTRING& cStr);
//int palindrome(const cSTRING& cStr);
};
#endif
CPP FILE:
Code:
#include "cDATE.h"
#include <iostream.h>
#include <string.h>
cSTRING::cSTRING(char *name, int returnVal)
{
_nCmpValue = returnVal;
pszName1=0;
pszName2=0;
pszName1 = new char[strlen(name) + 1];
pszName2 = new char[strlen(name) + 1];
//Allocates an character array object
strcpy(pszName1, name);
strcpy(pszName2, name);
cout << "cSTRING Default Constructor Called" << endl;
}
cSTRING::cSTRING(char *name1)
{
this->pszName1 = 0;
this->pszName2 = 0;
pszName1 = new char[strlen(name1) + 1];
pszName2 = new char[strlen(name1) + 1];
pszName1 = strcpy(pszName1, name1);
for (int x=0; x<6; x++)
cout << name1[x];
cout << "Names copied" << endl;
}
cSTRING& cSTRING::operator=(const cSTRING& other)
{
cout << "inside =" << endl;
if (this!=&other) // check for self-assignment
{
// deallocate old memory
delete [] pszName1;
delete [] pszName2;
// allocate new memory
pszName1 = new char[strlen(other.pszName1) + 1];
// copy
strcpy(pszName1, other.pszName1);
}
return *this;
}
cSTRING cSTRING::operator+(const cSTRING& rhs)
{
cout << "inside +" << endl;
char tmp[50]; //temporary variable
char *pszTemp=tmp;
//pszTemp = strcpy(pszTemp, temp);
//getting size for new string
int nAlloc = (strlen(pszName1) + strlen(rhs.pszName1) +1 );
cout << nAlloc << endl;
//backing-up
strcpy(pszTemp, pszName1);
//deallocating old memory
delete [] pszName1;
delete [] pszName2;
//allocating new memory
pszName1 = new char [nAlloc];
pszName2 = new char [nAlloc];
strcat(pszTemp, rhs.pszName1);
strcpy(pszName1,pszTemp);
return *this;
}
cSTRING& cSTRING::operator+=(const cSTRING& rhs)
{
char tmp[50]; //temporary variable
char *pszTemp=tmp;
cout << "inside +=" << endl;
//getting size for new string
int nAlloc = (strlen(pszName1) + strlen(rhs.pszName1) +1 );
cout << nAlloc << endl;
//backing-up
strcpy(pszTemp, pszName1);
//deallocating old memory
delete [] pszName1;
delete [] pszName2;
//allocating new memory
pszName1 = new char[nAlloc];
pszName2 = new char[nAlloc];
pszName1 = strcpy(pszName1,pszTemp);
pszName1 = strcat(pszName1, rhs.pszName1);
return *this;
}
int cSTRING::operator==(const cSTRING& rhs)
{
//comparing strings
if (strcmp(pszName1, rhs.pszName1))
return 1; //returns 1 when not equal
else
return 0; //returns 0 when equal
}
int cSTRING::operator!=(const cSTRING& rhs)
{
//comparing strings
if (strcmp(pszName1, rhs.pszName1))
return 0; //returns 1 when equal
else
return 1; //returns 0 when not equal
}
cSTRING& cSTRING::Reverse ()
{
cout << "INSIDE REVERSE" << endl;
int nCnt = strlen(pszName1)-1;
for (int x=0; x<strlen(pszName1); x++)
{
pszName1[x]=pszName1[nCnt];
nCnt+=-1;
}
for (int x=0; x<strlen(pszName1); x++)
cout << pszName1[x];
return *this;
}
ostream &operator<<(ostream &o, cSTRING& rhs)
{
o << rhs.pszName1 << " (" <<strlen(rhs.pszName1)<<") ";
return o;
}
ostream &operator>>(ostream &o, cSTRING& rhs)
{
cout << "INSIDE cin" << endl;
//o >> rhs.pszName1;
return o;
}
/*int palindrome(const cSTRING pszName1)
{
int result;
cout << "Inside PALINDROME: "<< endl;
/* Reverse string and compare (ignore case): */
/* result = _stricmp( pszName1, _strrev( _strdup( pszName1 ) ) );
if( result == 0 )
cout << "Is a palindrome:: " << endl;
else
cout << "is not a palindrome " << endl;
return result;
}*/
cSTRING::cSTRING(cSTRING &rhs)
{
pszName1 = 0;
pszName2 = 0;
pszName1 = new char[strlen(rhs.getName1()) + 1];
strcpy(pszName1,rhs.pszName1);
pszName2 = new char[strlen(rhs.getName2()) + 1];
strcpy(pszName2,rhs.pszName2);
}
cSTRING::~cSTRING()
{
if (pszName1)
delete[] pszName1;
if (pszName2)
delete[] pszName2;
cout << "Destructor called" << endl;
};
cSTRING::cSTRING()
{
pszName1=0;
pszName2=0;
}
DRIVER FILE:
Code:
#include "cDATE.h"
#include <iostream.h>
#include <string.h>
int main(void)
{
char szStr1[] = "Hello";
char szStr2[] = "Hell";
int returnVal;
cSTRING setName1(szStr1);
cSTRING setName2(szStr2);
cout << "\n\nNow Equalling...." << endl;
cSTRING s2 = setName2;
cout << s2;
cout << "\n\nNow Adding...." << endl;
cSTRING s3 = setName1;
s3 = (s3 + setName2);
cout << s3;
cout << "\n\nNow += " << endl;
cSTRING s4 = setName1;
s4 += setName2;
cout << s4;
cout << "\n\nNow Comparing == ...." << endl;
returnVal = (setName1 == setName2);
cout << endl;
cout << returnVal << endl;
cout << "\n\nNow Comparing != ...." << endl;
returnVal = (setName1 != setName2);
cout << endl;
cout << returnVal << endl;
return (0);
cout << "\n\nNow reversing:::::" << endl;
cSTRING s5;
s2.Reverse();
/*
cout << "\n\nNow palindrome : " << endl;
returnVal = palindrome(pszName1);
*/
}
while you are at it, i would also like to know how would i overload operator>> i got the operator<< to work but this one is tricky