MCC - Marquez - CIS162AB - C++ Level I
P11_ex Example Date Class Implementation
   cpp.gif

Description: The purpose of this C++ programming example is to familiarize students with defining classes and testing the class definitions with a driver program.

UML - Class Diagram:
Symbol definitions:
- private
+ public
# protected

DateMDY
- int month
- int day
- int year
+ DateMDY( ) //default constructor doesn't have parameters
+ DateMDY(int m, int d, int y) //overloaded constructor;
//parameter names in overloaded constructors should be different
//from the class variable names, because the parameter values
//are going to be assigned to the class variables.

+ ~DateMDY( ) //destructor

//Set accessors do NOT return a value (void) because they are used
//to assign a value to a private variable. The value to assign is
//passed through the parameter.
+ void setMonth(int m)
+ void setDay(int d)
+ void setYear(int y)

//Get accessors are used to return the value stored in a private
//variable, so a parameter is not passed.
+ int getMonth( )
+ int getDay( )
+ int getYear( )

//Input accessors prompt for, get, and store a value in a private
//variable. A value is not returned and a parameter is not passed.
+ void inputMonth( )
+ void inputDay( )
+ void inputYear( )

Sample Output: 

P11ex   Juan Marquez  

Enter Month: 9
Enter Day:   13
Enter Year:  1982

9/13/1982

10/31/1983

11/15/1984

Object going out of scope. month = 12
12/20/1985

Object going out of scope. month = 12

Object going out of scope. month = 10


Source Code:
//P11ex  DateMDY Class - Juan Marquez  
//
//This program includes the DateMDY class definition and 
//a small driver to test the DateMDY class.
//

#include <iostream>  // cin and cout
using namespace std;

//class interface

class DateMDY
{
private:
//member variables require accessors
    int month;
    int day;
    int year;

public: 
//default constructor - calls input functions
    DateMDY();

//overloaded constructor
    DateMDY(int m,  int d,  int y);

//destructor
    ~DateMDY();

//accessors
    void setMonth(int m);
    void setDay(int d);
    void setYear(int y);

    int  getMonth();
    int  getDay();
    int  getYear();

    void inputMonth();
    void inputDay();
    void inputYear();
};



//application - the driver to test class DateMDY

void displayDate(DateMDY& date);  //call-by-reference


void main()
{
    cout << "\nP11ex   Juan Marquez  \n\n";

                            //Input 09/13/1982 using
    DateMDY bday;            //default constructor
    displayDate(bday);

    bday.setMonth(10);        //use accessors
    bday.setDay(31);
    bday.setYear(1983);

    displayDate(bday);

    DateMDY payDay(11, 15, 1984);   //overloaded constructor
    displayDate(payDay);

    payDay = DateMDY(12, 20, 1985); //explicit call to overloaded

//calls destructor right after returning from constructor and before 
//assignment statement(=), that is why 12 is displayed on sample output

    displayDate(payDay);  
 
    return;
}

void displayDate(DateMDY& date)
{
//declare local variables
    int month;
    int day;
    int year;

//get values stored in private variables
    month = date.getMonth();
    day   = date.getDay();
    year  = date.getYear();

    cout << month << "/" << day << "/" << year << endl << endl;
} 
 

//class implementation
//Note that the ClassName:: goes before the function name

//default constructor
DateMDY::DateMDY()
{
    inputMonth();
    inputDay();
    inputYear();
}    

//overloaded constructor
DateMDY::DateMDY(int m,  int d,  int y)    
{
    month = m;
    day   = d;
    year  = y;
}    

//destructor
DateMDY::~DateMDY()
{
    cout << "\nObject going out of scope. month = " << month << endl;
}


//accessor definitions
void DateMDY::setMonth(int m)
{
    month = m;
} 

void DateMDY::setDay(int d)
{
    day = d;
} 
    
void DateMDY::setYear(int y)
{
    year= y;
} 

int DateMDY::getMonth()
{
    return month;
}

int DateMDY::getDay()
{
    return day;
} 

int DateMDY::getYear()
{
    return year;
}

void DateMDY::inputMonth()
{
    cout << "Enter Month: ";
    cin  >> month;
} 

void DateMDY::inputDay()
{
    cout << "Enter Day:   ";
    cin  >> day;
} 

void DateMDY::inputYear()
{
    cout << "Enter Year:  ";
    cin  >> year;
    cout << endl;
} 

//end of P11ex.cpp

Revised: 8/15/2009 - www.mc.maricopa.edu/~marquez/cis162ab/p11_ex_date_class.html