SlideShare a Scribd company logo
1 of 25
Download to read offline
Answer
****************************************main.cpp******************************
****************************
#include
#include
#include
#include "Month.h"
int main()
{
//testing the constructors here
Month myMonth1;
Month myMonth12(12);
Month myMonth3("March");
string newMonth = "May";
int newNum = 4;
//testing the get and set functions
cout << " ---------------------------------------------" << endl;
cout << "Testing Constructors, Set() and Get()" << endl;
cout << "---------------------------------------------" << endl;
cout << "myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl;
cout << "myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() << endl;
cout << "myMonth1.getMonthName() gives: " << myMonth12.getMonthName() << endl;
cout << "myMonth12.getMonthNumber() gives: " << myMonth12.getMonthNumber() <<
endl;
cout << "myMonth3.getMonthName() gives: " << myMonth3.getMonthName() << endl;
cout << "myMonth3.getMonthNumber() gives: " << myMonth3.getMonthNumber() << endl;
cout << endl << "Setting number to " << newNum << endl;
myMonth1.setMonthNumber(newNum);
cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl;
cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() <<
endl;
cout << endl << "Setting number to 13 (invalid) " << endl;
myMonth1.setMonthNumber(13);
cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl;
cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() <<
endl;
cout << endl << "Setting name to " << newMonth << endl;
myMonth1.setMonthName(newMonth);
cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl;
cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() <<
endl;
cout << endl << "Setting name to XYZ (invalid) "<< endl;
myMonth1.setMonthName("XYZ");
cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl;
cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() <<
endl;
//testing inputs, outputs, increments and decrements
cout << " ---------------------------------------------" << endl;
cout << "Testing cin, cout, increments and decrements" << endl;
cout << "---------------------------------------------" << endl;
cout << "myMonth12 is " << myMonth12<< endl;
cout << "myMonth12++ gives "<< myMonth12++ << endl;
cout << "But myMonth12 is now " << myMonth12 << endl;
cout << "++myMonth12 gives "<< ++myMonth12 << endl;
cout << "myMonth12-- gives "<< myMonth12-- << endl;
cout << "But myMonth2 is now " << myMonth12 << endl;
cout << "--myMonth12 gives "<< --myMonth12 << endl;
Month myMonth5;
cin >> myMonth5;
cout << "myMonth5 is set to "<< myMonth5 << endl;
cout << "Press Enter to quit." << endl;
cin.ignore();
cin.get();
return 0;
}
***********************************************month.h************************
****************************
#ifndef MONTH_H
#define MONTH_H
#include
#include
#include
#include "MyString.h"
class Month
{
private:
MyString monthName;
int monthNumber; // monthNumber will accept from 1 to 12
public:
Month() { monthName = "January"; monthNumber = 1; }
Month(const char *name) { setMonthName(name); }
Month(const int num) { setMonthNumber(num); }
MyString getMonthName() { return monthName; }
int getMonthNumber() const {return monthNumber; }
void setMonthName(const string name);
void setMonthNumber(const int num);
Month operator++();
Month operator++(int);
Month operator--();
Month operator--(int);
friend ostream &operator<<(ostream &strm, const Month &obj);
friend istream &operator >> (istream &strm, Month &monthObj);
};
#endif /* end of MONTH_H */
**************************************************month.cpp*******************
******************************
#include
#include
#include
#include "MyString.h"
#include "Month.h"
void Month::setMonthName(const string name)
{
if (name == "January" || name == "Jan") setMonthNumber(1);
else if (name == "February" || name == "Feb")
{
setMonthNumber(2);
}
else if (name == "March" || name == "Mar")
{
setMonthNumber(3);
}
else if (name == "April" || name == "Apr")
{
setMonthNumber(4);
}
else if (name == "May")
{
setMonthNumber(5);
}
else if (name == "June" || name == "Jun")
{
setMonthNumber(6);
}
else if (name == "July" || name == "Jul")
{
setMonthNumber(7);
}
else if (name == "August" || name == "Aug")
{
setMonthNumber(8);
}
else if (name == "September"|| name == "Sept")
{
setMonthNumber(9);
}
else if (name == "October" || name == "Oct")
{
setMonthNumber(10);
}
else if (name == "November" || name == "Nov")
{
setMonthNumber(11);
}
else if (name == "December" || name == "Dec")
{
setMonthNumber(12);
}
else
{
setMonthNumber(1);
}
}
void Month::setMonthNumber(const int num)
{
if (num < 1 || num > 12)
monthNumber = 1;
else
monthNumber = num;
if(monthNumber == 1)
monthName = "January";
else if(monthNumber == 2)
monthName = "February";
else if(monthNumber == 3)
monthName = "March";
else if(monthNumber == 4)
monthName = "April";
else if(monthNumber == 5)
monthName = "May";
else if(monthNumber == 6)
monthName = "June";
else if(monthNumber == 7)
monthName = "July";
else if(monthNumber == 8)
monthName = "August";
else if(monthNumber == 9)
monthName = "September";
else if(monthNumber == 10)
monthName = "October";
else if(monthNumber == 11)
monthName = "November";
else
monthName = "December";
}
Month Month::operator++()
{
if (monthNumber == 12)
monthNumber = 1;
else
++monthNumber;
setMonthNumber(monthNumber);
return *this;
}
Month Month::operator++(int)
{
Month temp(monthNumber);
setMonthNumber(++monthNumber);
return temp;
}
Month Month::operator--()
{
if (monthNumber == 1) monthNumber = 12;
else --monthNumber;
setMonthNumber(monthNumber);
return *this;
}
Month Month::operator--(int)
{
Month temp(monthNumber);
setMonthNumber(--monthNumber);
return temp;
}
ostream &operator << (ostream &strm, const Month &obj)
{
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)
{
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 *);
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];
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)
{
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)
{
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)
{
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;
}
Solution
Answer
****************************************main.cpp******************************
****************************
#include
#include
#include
#include "Month.h"
int main()
{
//testing the constructors here
Month myMonth1;
Month myMonth12(12);
Month myMonth3("March");
string newMonth = "May";
int newNum = 4;
//testing the get and set functions
cout << " ---------------------------------------------" << endl;
cout << "Testing Constructors, Set() and Get()" << endl;
cout << "---------------------------------------------" << endl;
cout << "myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl;
cout << "myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() << endl;
cout << "myMonth1.getMonthName() gives: " << myMonth12.getMonthName() << endl;
cout << "myMonth12.getMonthNumber() gives: " << myMonth12.getMonthNumber() <<
endl;
cout << "myMonth3.getMonthName() gives: " << myMonth3.getMonthName() << endl;
cout << "myMonth3.getMonthNumber() gives: " << myMonth3.getMonthNumber() << endl;
cout << endl << "Setting number to " << newNum << endl;
myMonth1.setMonthNumber(newNum);
cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl;
cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() <<
endl;
cout << endl << "Setting number to 13 (invalid) " << endl;
myMonth1.setMonthNumber(13);
cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl;
cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() <<
endl;
cout << endl << "Setting name to " << newMonth << endl;
myMonth1.setMonthName(newMonth);
cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl;
cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() <<
endl;
cout << endl << "Setting name to XYZ (invalid) "<< endl;
myMonth1.setMonthName("XYZ");
cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl;
cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() <<
endl;
//testing inputs, outputs, increments and decrements
cout << " ---------------------------------------------" << endl;
cout << "Testing cin, cout, increments and decrements" << endl;
cout << "---------------------------------------------" << endl;
cout << "myMonth12 is " << myMonth12<< endl;
cout << "myMonth12++ gives "<< myMonth12++ << endl;
cout << "But myMonth12 is now " << myMonth12 << endl;
cout << "++myMonth12 gives "<< ++myMonth12 << endl;
cout << "myMonth12-- gives "<< myMonth12-- << endl;
cout << "But myMonth2 is now " << myMonth12 << endl;
cout << "--myMonth12 gives "<< --myMonth12 << endl;
Month myMonth5;
cin >> myMonth5;
cout << "myMonth5 is set to "<< myMonth5 << endl;
cout << "Press Enter to quit." << endl;
cin.ignore();
cin.get();
return 0;
}
***********************************************month.h************************
****************************
#ifndef MONTH_H
#define MONTH_H
#include
#include
#include
#include "MyString.h"
class Month
{
private:
MyString monthName;
int monthNumber; // monthNumber will accept from 1 to 12
public:
Month() { monthName = "January"; monthNumber = 1; }
Month(const char *name) { setMonthName(name); }
Month(const int num) { setMonthNumber(num); }
MyString getMonthName() { return monthName; }
int getMonthNumber() const {return monthNumber; }
void setMonthName(const string name);
void setMonthNumber(const int num);
Month operator++();
Month operator++(int);
Month operator--();
Month operator--(int);
friend ostream &operator<<(ostream &strm, const Month &obj);
friend istream &operator >> (istream &strm, Month &monthObj);
};
#endif /* end of MONTH_H */
**************************************************month.cpp*******************
******************************
#include
#include
#include
#include "MyString.h"
#include "Month.h"
void Month::setMonthName(const string name)
{
if (name == "January" || name == "Jan") setMonthNumber(1);
else if (name == "February" || name == "Feb")
{
setMonthNumber(2);
}
else if (name == "March" || name == "Mar")
{
setMonthNumber(3);
}
else if (name == "April" || name == "Apr")
{
setMonthNumber(4);
}
else if (name == "May")
{
setMonthNumber(5);
}
else if (name == "June" || name == "Jun")
{
setMonthNumber(6);
}
else if (name == "July" || name == "Jul")
{
setMonthNumber(7);
}
else if (name == "August" || name == "Aug")
{
setMonthNumber(8);
}
else if (name == "September"|| name == "Sept")
{
setMonthNumber(9);
}
else if (name == "October" || name == "Oct")
{
setMonthNumber(10);
}
else if (name == "November" || name == "Nov")
{
setMonthNumber(11);
}
else if (name == "December" || name == "Dec")
{
setMonthNumber(12);
}
else
{
setMonthNumber(1);
}
}
void Month::setMonthNumber(const int num)
{
if (num < 1 || num > 12)
monthNumber = 1;
else
monthNumber = num;
if(monthNumber == 1)
monthName = "January";
else if(monthNumber == 2)
monthName = "February";
else if(monthNumber == 3)
monthName = "March";
else if(monthNumber == 4)
monthName = "April";
else if(monthNumber == 5)
monthName = "May";
else if(monthNumber == 6)
monthName = "June";
else if(monthNumber == 7)
monthName = "July";
else if(monthNumber == 8)
monthName = "August";
else if(monthNumber == 9)
monthName = "September";
else if(monthNumber == 10)
monthName = "October";
else if(monthNumber == 11)
monthName = "November";
else
monthName = "December";
}
Month Month::operator++()
{
if (monthNumber == 12)
monthNumber = 1;
else
++monthNumber;
setMonthNumber(monthNumber);
return *this;
}
Month Month::operator++(int)
{
Month temp(monthNumber);
setMonthNumber(++monthNumber);
return temp;
}
Month Month::operator--()
{
if (monthNumber == 1) monthNumber = 12;
else --monthNumber;
setMonthNumber(monthNumber);
return *this;
}
Month Month::operator--(int)
{
Month temp(monthNumber);
setMonthNumber(--monthNumber);
return temp;
}
ostream &operator << (ostream &strm, const Month &obj)
{
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)
{
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 *);
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];
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)
{
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)
{
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)
{
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;
}

More Related Content

Similar to Answer main.cpp.pdf

CounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
CounterTest.javaimport static org.junit.Assert.;import org.jun.pdfCounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
CounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
deepua8
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
shreeaadithyaacellso
 
@author Jane Programmer @cwid 123 45 678 @class.docx
@author Jane Programmer  @cwid   123 45 678  @class.docx@author Jane Programmer  @cwid   123 45 678  @class.docx
@author Jane Programmer @cwid 123 45 678 @class.docx
gertrudebellgrove
 
Lab Assignment #6, part 3 Time ConversionProgram Name lab.docx
Lab Assignment #6, part 3 Time ConversionProgram Name lab.docxLab Assignment #6, part 3 Time ConversionProgram Name lab.docx
Lab Assignment #6, part 3 Time ConversionProgram Name lab.docx
smile790243
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2
sajidpk92
 
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
BANSALANKIT1077
 
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfg++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
arakalamkah11
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
勇浩 赖
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
Dendi Riadi
 

Similar to Answer main.cpp.pdf (20)

CounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
CounterTest.javaimport static org.junit.Assert.;import org.jun.pdfCounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
CounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
 
@author Jane Programmer @cwid 123 45 678 @class.docx
@author Jane Programmer  @cwid   123 45 678  @class.docx@author Jane Programmer  @cwid   123 45 678  @class.docx
@author Jane Programmer @cwid 123 45 678 @class.docx
 
Lab Assignment #6, part 3 Time ConversionProgram Name lab.docx
Lab Assignment #6, part 3 Time ConversionProgram Name lab.docxLab Assignment #6, part 3 Time ConversionProgram Name lab.docx
Lab Assignment #6, part 3 Time ConversionProgram Name lab.docx
 
C# Assignmet Help
C# Assignmet HelpC# Assignmet Help
C# Assignmet Help
 
Bangun datar dan bangun ruang
Bangun datar dan bangun ruangBangun datar dan bangun ruang
Bangun datar dan bangun ruang
 
C++ project on police station software
C++ project on police station softwareC++ project on police station software
C++ project on police station software
 
Dynamically Evolving Systems: Cluster Analysis Using Time
Dynamically Evolving Systems: Cluster Analysis Using TimeDynamically Evolving Systems: Cluster Analysis Using Time
Dynamically Evolving Systems: Cluster Analysis Using Time
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2
 
ECMAScript 6 Review
ECMAScript 6 ReviewECMAScript 6 Review
ECMAScript 6 Review
 
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
 
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfg++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
 
Method::Signatures
Method::SignaturesMethod::Signatures
Method::Signatures
 
Include
IncludeInclude
Include
 
Mysql:Operators
Mysql:OperatorsMysql:Operators
Mysql:Operators
 
MySQL Operators
MySQL OperatorsMySQL Operators
MySQL Operators
 
Linear queue
Linear queueLinear queue
Linear queue
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
 
Circular queue
Circular queueCircular queue
Circular queue
 

More from vichu19891

using recursive method .pdf
  using recursive method .pdf  using recursive method .pdf
using recursive method .pdf
vichu19891
 
The thickness of non-saturated zone and physico-c.pdf
                     The thickness of non-saturated zone and physico-c.pdf                     The thickness of non-saturated zone and physico-c.pdf
The thickness of non-saturated zone and physico-c.pdf
vichu19891
 
This is actually pretty simple, so Ill help explain. Take a gi.pdf
This is actually pretty simple, so Ill help explain. Take a gi.pdfThis is actually pretty simple, so Ill help explain. Take a gi.pdf
This is actually pretty simple, so Ill help explain. Take a gi.pdf
vichu19891
 
There are many test IPv6 networks deployed across the world. For act.pdf
There are many test IPv6 networks deployed across the world. For act.pdfThere are many test IPv6 networks deployed across the world. For act.pdf
There are many test IPv6 networks deployed across the world. For act.pdf
vichu19891
 

More from vichu19891 (20)

1. Copper is above silver in the activity series. Thus Cu metal will.pdf
1. Copper is above silver in the activity series. Thus Cu metal will.pdf1. Copper is above silver in the activity series. Thus Cu metal will.pdf
1. Copper is above silver in the activity series. Thus Cu metal will.pdf
 
using recursive method .pdf
  using recursive method .pdf  using recursive method .pdf
using recursive method .pdf
 
a. Population - families in the state of Florida b. Variable .pdf
 a. Population - families in the state of Florida b. Variable .pdf a. Population - families in the state of Florida b. Variable .pdf
a. Population - families in the state of Florida b. Variable .pdf
 
there is no reaction between HNO3 and KCl. S.pdf
                     there is no reaction between HNO3 and KCl.  S.pdf                     there is no reaction between HNO3 and KCl.  S.pdf
there is no reaction between HNO3 and KCl. S.pdf
 
The thickness of non-saturated zone and physico-c.pdf
                     The thickness of non-saturated zone and physico-c.pdf                     The thickness of non-saturated zone and physico-c.pdf
The thickness of non-saturated zone and physico-c.pdf
 
The folding process of proteins is hierarchical, .pdf
                     The folding process of proteins is hierarchical, .pdf                     The folding process of proteins is hierarchical, .pdf
The folding process of proteins is hierarchical, .pdf
 
Step1 ppt of PbCl2 are soluble in hot water. Ste.pdf
                     Step1 ppt of PbCl2 are soluble in hot water.  Ste.pdf                     Step1 ppt of PbCl2 are soluble in hot water.  Ste.pdf
Step1 ppt of PbCl2 are soluble in hot water. Ste.pdf
 
Should take off H from SH group, forming RS-Na+ s.pdf
                     Should take off H from SH group, forming RS-Na+ s.pdf                     Should take off H from SH group, forming RS-Na+ s.pdf
Should take off H from SH group, forming RS-Na+ s.pdf
 
Rest are okay but B) I think should be vanderwall.pdf
                     Rest are okay but B) I think should be vanderwall.pdf                     Rest are okay but B) I think should be vanderwall.pdf
Rest are okay but B) I think should be vanderwall.pdf
 
pH =4.217 pH = - log(concentration of H+) = -log .pdf
                     pH =4.217 pH = - log(concentration of H+) = -log .pdf                     pH =4.217 pH = - log(concentration of H+) = -log .pdf
pH =4.217 pH = - log(concentration of H+) = -log .pdf
 
Liquids may change to a vapor at temperatures bel.pdf
                     Liquids may change to a vapor at temperatures bel.pdf                     Liquids may change to a vapor at temperatures bel.pdf
Liquids may change to a vapor at temperatures bel.pdf
 
intra extra equilibrium potential mEqL mEqL 1.pdf
                     intra extra equilibrium potential  mEqL mEqL  1.pdf                     intra extra equilibrium potential  mEqL mEqL  1.pdf
intra extra equilibrium potential mEqL mEqL 1.pdf
 
Which of the following forms of DES is considered the most vulnerabl.pdf
Which of the following forms of DES is considered the most vulnerabl.pdfWhich of the following forms of DES is considered the most vulnerabl.pdf
Which of the following forms of DES is considered the most vulnerabl.pdf
 
This is actually pretty simple, so Ill help explain. Take a gi.pdf
This is actually pretty simple, so Ill help explain. Take a gi.pdfThis is actually pretty simple, so Ill help explain. Take a gi.pdf
This is actually pretty simple, so Ill help explain. Take a gi.pdf
 
There are many test IPv6 networks deployed across the world. For act.pdf
There are many test IPv6 networks deployed across the world. For act.pdfThere are many test IPv6 networks deployed across the world. For act.pdf
There are many test IPv6 networks deployed across the world. For act.pdf
 
The three ways of presenting the changes in the balance of the Compr.pdf
The three ways of presenting the changes in the balance of the Compr.pdfThe three ways of presenting the changes in the balance of the Compr.pdf
The three ways of presenting the changes in the balance of the Compr.pdf
 
The RASopathies are a group of genetic syndromes caused by germline .pdf
The RASopathies are a group of genetic syndromes caused by germline .pdfThe RASopathies are a group of genetic syndromes caused by germline .pdf
The RASopathies are a group of genetic syndromes caused by germline .pdf
 
ThanksSolutionThanks.pdf
ThanksSolutionThanks.pdfThanksSolutionThanks.pdf
ThanksSolutionThanks.pdf
 
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p Anti.pdf
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p  Anti.pdfStep1 In O2 ; we have 2 unpaired electrons which occupy pi 2p  Anti.pdf
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p Anti.pdf
 
Quartzite is sandstone that has been converted to a solid quartz roc.pdf
Quartzite is sandstone that has been converted to a solid quartz roc.pdfQuartzite is sandstone that has been converted to a solid quartz roc.pdf
Quartzite is sandstone that has been converted to a solid quartz roc.pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 

Answer main.cpp.pdf

  • 1. Answer ****************************************main.cpp****************************** **************************** #include #include #include #include "Month.h" int main() { //testing the constructors here Month myMonth1; Month myMonth12(12); Month myMonth3("March"); string newMonth = "May"; int newNum = 4; //testing the get and set functions cout << " ---------------------------------------------" << endl; cout << "Testing Constructors, Set() and Get()" << endl; cout << "---------------------------------------------" << endl; cout << "myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl; cout << "myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() << endl; cout << "myMonth1.getMonthName() gives: " << myMonth12.getMonthName() << endl; cout << "myMonth12.getMonthNumber() gives: " << myMonth12.getMonthNumber() << endl; cout << "myMonth3.getMonthName() gives: " << myMonth3.getMonthName() << endl; cout << "myMonth3.getMonthNumber() gives: " << myMonth3.getMonthNumber() << endl; cout << endl << "Setting number to " << newNum << endl; myMonth1.setMonthNumber(newNum); cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl; cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() << endl; cout << endl << "Setting number to 13 (invalid) " << endl; myMonth1.setMonthNumber(13); cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl;
  • 2. cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() << endl; cout << endl << "Setting name to " << newMonth << endl; myMonth1.setMonthName(newMonth); cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl; cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() << endl; cout << endl << "Setting name to XYZ (invalid) "<< endl; myMonth1.setMonthName("XYZ"); cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl; cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() << endl; //testing inputs, outputs, increments and decrements cout << " ---------------------------------------------" << endl; cout << "Testing cin, cout, increments and decrements" << endl; cout << "---------------------------------------------" << endl; cout << "myMonth12 is " << myMonth12<< endl; cout << "myMonth12++ gives "<< myMonth12++ << endl; cout << "But myMonth12 is now " << myMonth12 << endl; cout << "++myMonth12 gives "<< ++myMonth12 << endl; cout << "myMonth12-- gives "<< myMonth12-- << endl; cout << "But myMonth2 is now " << myMonth12 << endl; cout << "--myMonth12 gives "<< --myMonth12 << endl; Month myMonth5; cin >> myMonth5; cout << "myMonth5 is set to "<< myMonth5 << endl; cout << "Press Enter to quit." << endl; cin.ignore(); cin.get(); return 0; } ***********************************************month.h************************ **************************** #ifndef MONTH_H #define MONTH_H #include
  • 3. #include #include #include "MyString.h" class Month { private: MyString monthName; int monthNumber; // monthNumber will accept from 1 to 12 public: Month() { monthName = "January"; monthNumber = 1; } Month(const char *name) { setMonthName(name); } Month(const int num) { setMonthNumber(num); } MyString getMonthName() { return monthName; } int getMonthNumber() const {return monthNumber; } void setMonthName(const string name); void setMonthNumber(const int num); Month operator++(); Month operator++(int); Month operator--(); Month operator--(int); friend ostream &operator<<(ostream &strm, const Month &obj); friend istream &operator >> (istream &strm, Month &monthObj); }; #endif /* end of MONTH_H */ **************************************************month.cpp******************* ****************************** #include #include #include #include "MyString.h" #include "Month.h" void Month::setMonthName(const string name) { if (name == "January" || name == "Jan") setMonthNumber(1); else if (name == "February" || name == "Feb")
  • 4. { setMonthNumber(2); } else if (name == "March" || name == "Mar") { setMonthNumber(3); } else if (name == "April" || name == "Apr") { setMonthNumber(4); } else if (name == "May") { setMonthNumber(5); } else if (name == "June" || name == "Jun") { setMonthNumber(6); } else if (name == "July" || name == "Jul") { setMonthNumber(7); } else if (name == "August" || name == "Aug") { setMonthNumber(8); } else if (name == "September"|| name == "Sept") { setMonthNumber(9); } else if (name == "October" || name == "Oct") { setMonthNumber(10); } else if (name == "November" || name == "Nov")
  • 5. { setMonthNumber(11); } else if (name == "December" || name == "Dec") { setMonthNumber(12); } else { setMonthNumber(1); } } void Month::setMonthNumber(const int num) { if (num < 1 || num > 12) monthNumber = 1; else monthNumber = num; if(monthNumber == 1) monthName = "January"; else if(monthNumber == 2) monthName = "February"; else if(monthNumber == 3) monthName = "March"; else if(monthNumber == 4) monthName = "April"; else if(monthNumber == 5) monthName = "May"; else if(monthNumber == 6) monthName = "June"; else if(monthNumber == 7) monthName = "July"; else if(monthNumber == 8) monthName = "August"; else if(monthNumber == 9) monthName = "September";
  • 6. else if(monthNumber == 10) monthName = "October"; else if(monthNumber == 11) monthName = "November"; else monthName = "December"; } Month Month::operator++() { if (monthNumber == 12) monthNumber = 1; else ++monthNumber; setMonthNumber(monthNumber); return *this; } Month Month::operator++(int) { Month temp(monthNumber); setMonthNumber(++monthNumber); return temp; } Month Month::operator--() { if (monthNumber == 1) monthNumber = 12; else --monthNumber; setMonthNumber(monthNumber); return *this; } Month Month::operator--(int) { Month temp(monthNumber); setMonthNumber(--monthNumber); return temp; } ostream &operator << (ostream &strm, const Month &obj)
  • 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];
  • 10. 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) { 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) {
  • 11. 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) { 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;
  • 12. 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) { bool status;
  • 13. 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; } Solution Answer ****************************************main.cpp****************************** **************************** #include #include #include #include "Month.h" int main() { //testing the constructors here Month myMonth1; Month myMonth12(12); Month myMonth3("March"); string newMonth = "May"; int newNum = 4;
  • 14. //testing the get and set functions cout << " ---------------------------------------------" << endl; cout << "Testing Constructors, Set() and Get()" << endl; cout << "---------------------------------------------" << endl; cout << "myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl; cout << "myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() << endl; cout << "myMonth1.getMonthName() gives: " << myMonth12.getMonthName() << endl; cout << "myMonth12.getMonthNumber() gives: " << myMonth12.getMonthNumber() << endl; cout << "myMonth3.getMonthName() gives: " << myMonth3.getMonthName() << endl; cout << "myMonth3.getMonthNumber() gives: " << myMonth3.getMonthNumber() << endl; cout << endl << "Setting number to " << newNum << endl; myMonth1.setMonthNumber(newNum); cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl; cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() << endl; cout << endl << "Setting number to 13 (invalid) " << endl; myMonth1.setMonthNumber(13); cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl; cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() << endl; cout << endl << "Setting name to " << newMonth << endl; myMonth1.setMonthName(newMonth); cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl; cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() << endl; cout << endl << "Setting name to XYZ (invalid) "<< endl; myMonth1.setMonthName("XYZ"); cout << "Now myMonth1.getMonthName() gives: " << myMonth1.getMonthName() << endl; cout << "And myMonth1.getMonthNumber() gives: " << myMonth1.getMonthNumber() << endl; //testing inputs, outputs, increments and decrements cout << " ---------------------------------------------" << endl; cout << "Testing cin, cout, increments and decrements" << endl; cout << "---------------------------------------------" << endl; cout << "myMonth12 is " << myMonth12<< endl;
  • 15. cout << "myMonth12++ gives "<< myMonth12++ << endl; cout << "But myMonth12 is now " << myMonth12 << endl; cout << "++myMonth12 gives "<< ++myMonth12 << endl; cout << "myMonth12-- gives "<< myMonth12-- << endl; cout << "But myMonth2 is now " << myMonth12 << endl; cout << "--myMonth12 gives "<< --myMonth12 << endl; Month myMonth5; cin >> myMonth5; cout << "myMonth5 is set to "<< myMonth5 << endl; cout << "Press Enter to quit." << endl; cin.ignore(); cin.get(); return 0; } ***********************************************month.h************************ **************************** #ifndef MONTH_H #define MONTH_H #include #include #include #include "MyString.h" class Month { private: MyString monthName; int monthNumber; // monthNumber will accept from 1 to 12 public: Month() { monthName = "January"; monthNumber = 1; } Month(const char *name) { setMonthName(name); } Month(const int num) { setMonthNumber(num); } MyString getMonthName() { return monthName; } int getMonthNumber() const {return monthNumber; } void setMonthName(const string name); void setMonthNumber(const int num);
  • 16. Month operator++(); Month operator++(int); Month operator--(); Month operator--(int); friend ostream &operator<<(ostream &strm, const Month &obj); friend istream &operator >> (istream &strm, Month &monthObj); }; #endif /* end of MONTH_H */ **************************************************month.cpp******************* ****************************** #include #include #include #include "MyString.h" #include "Month.h" void Month::setMonthName(const string name) { if (name == "January" || name == "Jan") setMonthNumber(1); else if (name == "February" || name == "Feb") { setMonthNumber(2); } else if (name == "March" || name == "Mar") { setMonthNumber(3); } else if (name == "April" || name == "Apr") { setMonthNumber(4); } else if (name == "May") { setMonthNumber(5); } else if (name == "June" || name == "Jun") {
  • 17. setMonthNumber(6); } else if (name == "July" || name == "Jul") { setMonthNumber(7); } else if (name == "August" || name == "Aug") { setMonthNumber(8); } else if (name == "September"|| name == "Sept") { setMonthNumber(9); } else if (name == "October" || name == "Oct") { setMonthNumber(10); } else if (name == "November" || name == "Nov") { setMonthNumber(11); } else if (name == "December" || name == "Dec") { setMonthNumber(12); } else { setMonthNumber(1); } } void Month::setMonthNumber(const int num) { if (num < 1 || num > 12) monthNumber = 1; else
  • 18. monthNumber = num; if(monthNumber == 1) monthName = "January"; else if(monthNumber == 2) monthName = "February"; else if(monthNumber == 3) monthName = "March"; else if(monthNumber == 4) monthName = "April"; else if(monthNumber == 5) monthName = "May"; else if(monthNumber == 6) monthName = "June"; else if(monthNumber == 7) monthName = "July"; else if(monthNumber == 8) monthName = "August"; else if(monthNumber == 9) monthName = "September"; else if(monthNumber == 10) monthName = "October"; else if(monthNumber == 11) monthName = "November"; else monthName = "December"; } Month Month::operator++() { if (monthNumber == 12) monthNumber = 1; else ++monthNumber; setMonthNumber(monthNumber); return *this; } Month Month::operator++(int)
  • 19. { Month temp(monthNumber); setMonthNumber(++monthNumber); return temp; } Month Month::operator--() { if (monthNumber == 1) monthNumber = 12; else --monthNumber; setMonthNumber(monthNumber); return *this; } Month Month::operator--(int) { Month temp(monthNumber); setMonthNumber(--monthNumber); return temp; } ostream &operator << (ostream &strm, const Month &obj) { 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
  • 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; }