Day_23
Comment: With this homework I had a lot of trouble understanding classes. I watched a lot of you tube videos on how classes work, but I feel like everyone had a different way of writing classes. I got David to help me finish the code. He emphasize how important structures are so I better understand them before the final.
//ClassDate.cpp
#include <iostream>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
//class definition
class Date{
private: // User manipuation.
int month;
int day;
int year;
public: // Only the program can change this.
Date();
Date(int month,int day,int year);
void print_f1(); //format 1. 01/01/2014
void print_f2(); //format 2. January 1, 2014
void print_f3(); //format 3 1 January 2014
};//end class Date
int main()
{
//testing class Date
Date myDate;
myDate.print_f1(); //print date #1
cout<<endl;
myDate.print_f2(); //print date #2
cout<<endl;
//myDate.print_f3(); //print date #3
cout<<endl;
system("pause");
return 0;
}
//class implementation
Date::Date()
{
cout << "Enter the number for the month.";
cin >> month;
cout << "Enter the day.";
cin >> day;
cout << "Enter a four digit year.";
cin >> year;
} //end Date constructor -no arguments passed-
Date::Date(int month,int day,int year) // Validation.
{
if((month > 1) || (month > 12) || (day <1) || (day > 31) || (year < 1950) || (year > 2020))
cout << "Illegal data." << endl;
} //end Date constructor
void Date::print_f1() //Print menthod #1.
{
string Month;
switch(month){//class data member: month
case 1:
Month="January";
break;
case 2:
Month="February";
break;
case 3:
Month="March";
break;
case 4:
Month="April";
break;
case 5:
Month="May";
break;
case 6:
Month="June";
break;
case 7:
Month="July";
break;
case 8:
Month="August";
break;
case 9:
Month="September";
break;
case 10:
Month="October";
break;
case 11:
Month="November";
break;
case 12:
Month="December";
break;
}
cout << Month << '/'<< day << '/' <<year;
}
void Date::print_f2() //Print menthod #2.
{
cout << month << '/' << day << '/' << year;
}
//ClassDate.cpp
#include <iostream>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
//class definition
class Date{
private: // User manipuation.
int month;
int day;
int year;
public: // Only the program can change this.
Date();
Date(int month,int day,int year);
void print_f1(); //format 1. 01/01/2014
void print_f2(); //format 2. January 1, 2014
void print_f3(); //format 3 1 January 2014
};//end class Date
int main()
{
//testing class Date
Date myDate;
myDate.print_f1(); //print date #1
cout<<endl;
myDate.print_f2(); //print date #2
cout<<endl;
//myDate.print_f3(); //print date #3
cout<<endl;
system("pause");
return 0;
}
//class implementation
Date::Date()
{
cout << "Enter the number for the month.";
cin >> month;
cout << "Enter the day.";
cin >> day;
cout << "Enter a four digit year.";
cin >> year;
} //end Date constructor -no arguments passed-
Date::Date(int month,int day,int year) // Validation.
{
if((month > 1) || (month > 12) || (day <1) || (day > 31) || (year < 1950) || (year > 2020))
cout << "Illegal data." << endl;
} //end Date constructor
void Date::print_f1() //Print menthod #1.
{
string Month;
switch(month){//class data member: month
case 1:
Month="January";
break;
case 2:
Month="February";
break;
case 3:
Month="March";
break;
case 4:
Month="April";
break;
case 5:
Month="May";
break;
case 6:
Month="June";
break;
case 7:
Month="July";
break;
case 8:
Month="August";
break;
case 9:
Month="September";
break;
case 10:
Month="October";
break;
case 11:
Month="November";
break;
case 12:
Month="December";
break;
}
cout << Month << '/'<< day << '/' <<year;
}
void Date::print_f2() //Print menthod #2.
{
cout << month << '/' << day << '/' << year;
}
Comments
Post a Comment