7. {
return strm << obj.monthName << "(" << obj.monthNumber << ")";
}
istream &operator >> (istream &strm, Month &monthObj)
{
string m_Name;
cout << endl << "Please enter the month name: " << endl;
strm >> m_Name;
monthObj.setMonthName(m_Name);
return strm;
}
*******************************************mystring.h**************************
***********
// header file for the MyString class
#ifndef MYSTRING_H
#define MYSTRING_H
#include
#include
class MyString;
// declaration of forward operators.
ostream &operator<<(ostream &, const MyString &);
istream &operator>>(istream &, MyString &);
// MyString is a class. This is an abstract data type for handling strings.
class MyString
{
private:
char *str;
int len;
public:
// this is a default constructor
MyString()
{
str = NULL; len = 0;
}
// this is a copy constructor
MyString(MyString &right)
8. {
str = new char[right.length() + 1];
strcpy(str, right.getValue());
len = right.length();
}
// The following constructor initializes the
// MyString object with a C-string
MyString(char *sptr)
{
len = strlen(sptr);
str = new char[len + 1];
strcpy(str, sptr);
}
// this is a destructor
~MyString()
{
if (len != 0)
delete [] str;
}
// Here is the length function which returns the string length.
int length() const
{
return len;
}
// The getValue function which returns the string.
const char *getValue() const
{
return str;
};
// here are overloaded operators
const MyString operator+=(MyString &);
const char *operator+=(const char *);
const MyString operator=(MyString &);
const char *operator=(const char *);
int operator==(MyString &);
int operator==(const char *);
9. int operator!=(MyString &);
int operator!=(const char *);
bool operator>(MyString &);
bool operator>(const char *);
bool operator<(MyString &);
bool operator<(const char *);
bool operator>=(MyString &);
bool operator>=(const char*);
bool operator<=(MyString &);
bool operator<=(const char *);
// Friends functions
friend ostream &operator<<(ostream &, const MyString &);
friend istream &operator>>(istream &, MyString &);
};
#endif /* end of MYSTRING_H */
**************************************************mystring.cpp*****************
***********************
// This is an implementation file for the MyString class
#include // include this for string library functions
#include "MyString.h"
const MyString MyString::operator=(MyString &right)
{
if (len != 0)
delete [] str;
str = new char[right.length() + 1];
strcpy(str, right.getValue());
len = right.length();
return *this;
}
const char *MyString::operator=(const char *right)
{
if (len != 0)
delete [] str;
len = strlen(right);
str = new char[len + 1];
20. #include
class MyString;
// declaration of forward operators.
ostream &operator<<(ostream &, const MyString &);
istream &operator>>(istream &, MyString &);
// MyString is a class. This is an abstract data type for handling strings.
class MyString
{
private:
char *str;
int len;
public:
// this is a default constructor
MyString()
{
str = NULL; len = 0;
}
// this is a copy constructor
MyString(MyString &right)
{
str = new char[right.length() + 1];
strcpy(str, right.getValue());
len = right.length();
}
// The following constructor initializes the
// MyString object with a C-string
MyString(char *sptr)
{
len = strlen(sptr);
str = new char[len + 1];
strcpy(str, sptr);
}
// this is a destructor
~MyString()
{
if (len != 0)
21. delete [] str;
}
// Here is the length function which returns the string length.
int length() const
{
return len;
}
// The getValue function which returns the string.
const char *getValue() const
{
return str;
};
// here are overloaded operators
const MyString operator+=(MyString &);
const char *operator+=(const char *);
const MyString operator=(MyString &);
const char *operator=(const char *);
int operator==(MyString &);
int operator==(const char *);
int operator!=(MyString &);
int operator!=(const char *);
bool operator>(MyString &);
bool operator>(const char *);
bool operator<(MyString &);
bool operator<(const char *);
bool operator>=(MyString &);
bool operator>=(const char*);
bool operator<=(MyString &);
bool operator<=(const char *);
// Friends functions
friend ostream &operator<<(ostream &, const MyString &);
friend istream &operator>>(istream &, MyString &);
};
#endif /* end of MYSTRING_H */
**************************************************mystring.cpp*****************
***********************
22. // This is an implementation file for the MyString class
#include // include this for string library functions
#include "MyString.h"
const MyString MyString::operator=(MyString &right)
{
if (len != 0)
delete [] str;
str = new char[right.length() + 1];
strcpy(str, right.getValue());
len = right.length();
return *this;
}
const char *MyString::operator=(const char *right)
{
if (len != 0)
delete [] str;
len = strlen(right);
str = new char[len + 1];
strcpy(str, right);
return str;
}
const MyString MyString::operator+=(MyString &right)
{
char *temp = str;
str = new char[strlen(str) + right.length() + 1];
strcpy(str, temp);
strcat(str, right.getValue());
if (len != 0)
delete [] temp;
len = strlen(str);
return *this;
}
const char *MyString::operator+=(const char *right)
{
23. char *temp = str;
str = new char[strlen(str) + strlen(right) + 1];
strcpy(str, temp);
strcat(str, right);
if (len != 0)
delete [] temp;
return str;
}
int MyString::operator==(MyString &right)
{
return !strcmp(str, right.getValue());
}
int MyString::operator==(const char *right)
{
return !strcmp(str, right);
}
int MyString::operator!=(MyString &right)
{
return strcmp(str, right.getValue());
}
int MyString::operator!=(const char *right)
{
return strcmp(str, right);
}
bool MyString::operator>(MyString &right)
{
bool status;
if (strcmp(str, right.getValue()) > 0)
status = true;
else status = false;
return status;
}
bool MyString::operator>(const char *right)
{
24. bool status;
if (strcmp(str, right) > 0)
status = true;
else
status = false;
return status;
}
bool MyString::operator<(MyString &right)
{
bool status;
if (strcmp(str, right.getValue()) < 0)
status = true;
else
status = false;
return status;
}
bool MyString::operator<(const char *right)
{
bool status;
if (strcmp(str, right) < 0)
status = true;
else
status = false;
return status;
}
bool MyString::operator>=(MyString &right)
{
bool status;
if (strcmp(str, right.getValue()) >= 0)
status = true;
else
status = false;
return status;
}
bool MyString::operator>=(const char *right)
{
25. bool status;
if (strcmp(str, right) >= 0)
status = true;
else
status = false;
return status;
}
bool MyString::operator<=(MyString &right)
{
bool status;
if (strcmp(str, right.getValue()) <= 0)
status = true;
else
status = false;
return status;
}
bool MyString::operator<=(const char *right)
{
bool status;
if (strcmp(str, right) <= 0)
status = true;
else
status = false;
return status;
}
ostream &operator<<(ostream &strm, const MyString &obj)
{
strm << obj.str;
return strm;
}
istream &operator>>(istream &strm, MyString &obj)
{
strm.getline(obj.str, obj.len);
strm.ignore();
return strm;
}