I am write a program for class and I am not sure if I am headed in the correct direction. I will first put what my instructions say then I will post my code for my function.
Design a class called Date that has integer data members to store month, day, year. The class should have three-parameter default constructor that allows the date to be set at the time a new Date object is created. If the user creates a Date object without passing any arguments, or if any of the values passed are invalid , the default values of 1, 1, 2001 (i.e January 1, 2001)should be used. The class should have member functions to print the date in the following format:
3/15/10
March 15, 2010
15 March 2010
Demonstrate the class by writing a program that uses it.
Input Validation: Only accept values between 1 and 12 for month, between 1 and 31 for days, and between 1950 and 2020 for the year.
This is what I have for my function
1 2 3 4 5 6 7 8 9 10 11 12
class Date
{
public:
void input();
void output();
void set (int month, int day , int year);
int get_month;
int get_day;
int get_year;
}
Please let me know if there is something wrong and why its wrong so I can correct it. Thank you in advance.
//ClassDate.cpp
//##
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
//class definition
class Date{
private:
int month;
int day;
int year;
public:
Date();
Date(int month,int day,int year);
void print_f1(); //format 1
void print_f2(); //format 2
void print_f3(); //format 3
};//end class Date
int main(){
//testing class Date
Date myDate; //no arguments
//if you want pass arguments
//you should do something
//like this Date myDate(3,1,2014);
myDate.print_f1(); //print date
cout<<endl; //new line
return 0; //indicates success
}//end of main
//class implementation
Date::Date(){
month=1;
day=1;
year=2001;
}//end Date constructor -no arguments passed-
Date::Date(int month,int day,int year){
//code validation here
}//end Date constructor
void Date::print_f1(){
cout<<month<<'/'<<day<<'/'<<year;
}//end method print_f1
You have been a big help. Two more things.
1. In void Date::print_f2() & void Date::print_f3() i need to change the digital numbers of the month to actual word for the month like 1= January and so on. can you get me started. I was thinking of an if statement and I'm not sure if it will work.
2. i tried to use iomanip to use setw() on one of my dates and I could not get it to work correctly. I kept getting an error saying setw() not defined.
If you would like to see what I have so far and any changes I have made let me know and I post the code.
//ClassDate.cpp
//##
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
//class definition
class Date{
private:
int month;
int day;
int year;
public:
Date();
Date(int month,int day,int year);
void print_f1(); //format 1
void print_f2(); //format 2
void print_f3(); //format 3
};//end class Date
int main(){
//testing class Date
Date myDate; //no arguments
//if you want pass arguments
//you should do something
//like this Date myDate(3,1,2014);
myDate.print_f1(); //print date (format 1)
cout<<endl; //new line
myDate.print_f2(); //print date in format 2
cout<<endl; //new line
return 0; //indicates success
}//end of main
//class implementation
Date::Date(){
month=1;
day=1;
year=2001;
}//end Date constructor -no arguments passed-
Date::Date(int month,int day,int year){
//code validation here
}//end Date constructor
void Date::print_f1(){
cout<<month<<'/'<<day<<'/'<<year;
}//end method print_f1
void Date::print_f2(){
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;
}//end switch
cout<<Month<<'/'<<day<<'/'<<year; //you can use , / - ...etc.
}//end method print_f2
You need to #include <string> to use strings properly. The reason that this error is being recieved is probably that the definition for those operators are in the string header.
@kmartar you can
implement your own
if-else statements instead
of the switch example or
use @Chervil's implementation
(if you are allowed to use arrays)
Error compilation
Option 1
1 2 3 4 5 6 7
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
If you write usingnamespace std;
statement after the headers:
1 2
#include <iostream>
#include <string>
you do not need
to specify that you are
using objects/functions
from a header i.e.
1 2 3 4 5 6 7
#include <iostream>
using std::cout; //using standard C++ object console output
using std::cin; //using standard C++ object console input
using std::endl;
#include <string>
using std::string;
I am a little busy
right now but in a
few minutes i can help
to you further
//ClassDate.cpp
//##
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
//class definition
class Date{
private:
int month;
int day;
int year;
public:
Date();
Date(int Month,int Day,int Year);
void print_f1(); //format 1
void print_f2(); //format 2
void print_f3(); //format 3
};//end class Date
int main(){
//testing class Date
Date myDate; //no arguments
//if you want pass arguments
//you should do something
//like this Date myDate(3,1,2014);
myDate.print_f1(); //print date (format 1)
cout<<endl; //new line
myDate.print_f2(); //print date in format 2
cout<<'\n'<<endl; //new line
Date D_Day(6,6,1944);
D_Day.print_f1();
cout<<endl;
D_Day.print_f2();
cout<<"\n\n";
Date Fall_Berlin_Wall(11,9,1989);
Fall_Berlin_Wall.print_f1();
cout<<endl;
Fall_Berlin_Wall.print_f2();
cout<<endl;
return 0; //indicates success
}//end of main
//class implementation
Date::Date(){
month=1;
day=1;
year=2001;
}//end Date constructor -no arguments passed-
Date::Date(int Month,int Day,int Year){
if((Month<1||Month>12)||(Day<1||Day>31)||(Year<1950||Year>2020)){
month=1;
day=1;
year=2001;
}else{
month=Month;
day=Day;
year=Year;
}//end if-else
}//end Date constructor
void Date::print_f1(){
cout<<month<<'/'<<day<<'/'<<year;
}//end method print_f1
void Date::print_f2(){
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;
}//end switch
cout<<Month<<'/'<<day<<'/'<<year; //you can use , / - ...etc.
}//end method print_f2