Tuesday, December 19, 2017

Programming Challenge 14.7 - Month Class

Example Files: MonthClass.7z


Month.h


#ifndef MONTH_H_
#define MONTH_H_

#include <iostream>
using std::istream;
using std::ostream;

#include <string>
using std::string;

const int NUM_MONTHS = 13;

class Month
{
    private:
        static const string monthNames[NUM_MONTHS];        // Array holding month names

        string name;                        // To hold a month name
        int    monthNumber;                // To hold a month number (1 through 12)

    public:
        Month();
        Month(string);
        Month(int);

        void setMonthNumber(const int &);
        void setMonthName(const string &);

        string getMonthName() const
        { return name; }

        int getMonthNumber() const
        { return monthNumber; }

        // Overloaded operator functions
        Month operator++();                    // Prefix ++
        Month operator++(int);                // Postfix ++
        Month operator--();                    // Prefix --
        Month operator--(int);                // Postfix --

        // Friends
        friend ostream &operator << (ostream &, const Month &);
        friend istream &operator >> (istream &, Month &);
};

#endif

Month.cpp


#include "Month.h"

#include <iostream>
using std::cin;
using std::cout;

// Definition of const static array member holding month names
const string Month::monthNames[] = { "", "January", "February", "March", "April",
                                                 "May", "June", "July", "August", "September",
                                                 "October", "November", "December" };

/* **********************************************************
            Month::Month() - Default Constructor
   ********************************************************** */

Month::Month()
{
    name = "January";
    monthNumber = 1;
}

/* **********************************************************
            Month::Month() : string - Constructor  
    The constructor accepts the name of a month as argument.
    It sets the month name and number via the setMonthName()
    function to the correct value.
   ********************************************************** */

Month::Month(string n)
{
    setMonthName(n);
}

/* **********************************************************
            Month::Month() : int - Constructor
    The constructor accepts the number of a month as argument.
    It sets the month number and name via the setMonthNumber()
    function to the correct value.
   ********************************************************** */

Month::Month(int mNum)
{
    setMonthNumber(mNum);
}

/* **********************************************************
            MonthName::setMonthNumber() : const int &
    This function accepts a const reference to an integer as
    argument. If the value passed to this function is within
    the accepted range, the monthNumber and name each receive
    the correct name and month number. Else, monthNumber and
    name are set to 1 and January.
   ********************************************************** */

void Month::setMonthNumber(const int &mNum)
{
    if (mNum >= 1 && mNum <= 12)
    {
        monthNumber = mNum;
        name = monthNames[mNum];
    }
    else
    {
        monthNumber = 1;
        name = monthNames[monthNumber];
    }
}

/* **********************************************************
            Month::setMonthName() : const string &
    This function accepts a const reference parameter to a
    string object as argument. If the name is valid, name is
    set to the value passed to it, and monthNumber is passed
    the array-index at which the name is stored at. Else, name
    is set to January and monthNumber is set to 1.
   ********************************************************** */

void Month::setMonthName(const string &n)
{
    bool isFound = false;

    for (size_t count = 1; count < NUM_MONTHS; count++)
    {
        if (n == monthNames[count])
        {
            name = n;
            monthNumber = count;
            isFound = true;
        }   
    }

    if (isFound == false)
    {
        cout << "\nInput Error: " << n << " does not exist.\n"
             << "Setting month name and number to default values:\n\n";
        monthNumber = 1;
        name = monthNames[monthNumber];
       
    }
}

/* **********************************************************
            Month Month::operator++() : Prefix ++
   ********************************************************** */

Month Month::operator++()
{
    ++monthNumber;

    getMonthNumber() <= 12 ? setMonthNumber(monthNumber) : setMonthNumber(1);

    return *this;
}

/* **********************************************************
            Month Month::operator++() : Postfix ++
   ********************************************************** */

Month Month::operator++(int)
{
    Month temp(monthNumber);

    monthNumber++;
    getMonthNumber() <= 12 ? setMonthNumber(monthNumber) : setMonthNumber(1);

    return temp;
}

/* **********************************************************
            Month Month::operator--() : Prefix --
   ********************************************************** */

Month Month::operator--()
{
    --monthNumber;

    getMonthNumber() >= 1 ? setMonthNumber(monthNumber) : setMonthNumber(12);

    return *this;
}

/* **********************************************************
            Month Month::operator--() : Postfix --
   ********************************************************** */

Month Month::operator--(int)
{
    Month temp(monthNumber);

    monthNumber--;
    getMonthNumber() >= 1 ? setMonthNumber(monthNumber) : setMonthNumber(12);

    return temp;
}

/* **********************************************************
            Month Month::operator >> () : Extraction Operator
   ********************************************************** */

istream &operator >> (istream &strm, Month &obj)
{
    strm >> obj.name;
    obj.setMonthName(obj.name);
   
    return strm;
}

/* **********************************************************
            Month Month::operator << () : Insertion Operator
   ********************************************************** */

ostream &operator << (ostream &strm, const Month &obj)
{
    return strm << obj.name << " is month number " << obj.monthNumber << ".";
}

MonthDm.cpp


#include "Month.h"

#include <iostream>
using std::cin;
using std::cout;

int main()
{
    cout << "Months Class - Demo\n\n"
          << "This program demonstrates various functions of the Month class.\n\n"
          << "\t1.) The three constructors\n"
          << "\t2.) The overloaded pre- and postfix ++ and -- operators\n"
          << "\t3.) The overloaded istream and ostream operators\n\n"
          << "If a name or month number is invalid, their values are set\n"
          << "to: January and 1.\n\n";

    cout << "\tConstructor Demonstration\n\n";

    cout << "Demonstrating the default constructor\n";
    Month one;
    cout << one.getMonthName() << " is month number " << one.getMonthNumber() << "\n\n";

    cout << "Demonstrating the Second Constructor (Accepting a Month Name)\n";
    Month two("February");
    cout << two.getMonthName() << " is month number " << two.getMonthNumber() << "\n\n";

    cout << "Demonstrating the Third Constructor (Accepting a Mont Number)\n";
    Month three(9);
    cout << "Month " << three.getMonthNumber() << " is " << three.getMonthName() << "\n\n";

    cout << "\tOverloaded Post- and Prefix ++ and -- Operator Demonstration\n\n";
    cout << "Prefix ++\n";
    ++one;
    cout << one.getMonthName() << " is month number " << one.getMonthNumber() << "\n";

    cout << "Postfix ++\n";
    one++;
    cout << one.getMonthName() << " is month number " << one.getMonthNumber() << "\n\n";

    cout << "Prefix --\n";
    --two;
    cout << two.getMonthName() << " is month number " << two.getMonthNumber() << "\n";

    cout << "Postfix --\n";
    two--;
    cout << two.getMonthName() << " is month number " << two.getMonthNumber() << "\n\n";

    cout << "Overloaded Istream and Ostream Operator Demonstration\n\n";
    cout << "Enter a month name (January through December): ";
    Month four;
    cin >> four;
    cout << four;

    cin.get();
    cin.ignore();
    return 0;
}

Example Output:





No comments:

Post a Comment