Hello kibasu,
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
You have a good start. With code tags and proper indenting your code is much easier to read.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#include <iostream>
#include <string>
#include <limits>
using namespace std;
/*Objective: Create class called date that has integer data members to store month, day, and year.
Class should have three parameter default that allows date to be set at he time a new date object is created.
without passing any arguments, dafault values of 1, 1, 2001 (January 1, 2001) should be used. Month should be between 1 and 12.
Day should be between 1 and number of days of each month */
class Date
{
private:
int month, day, year; //User input
public:
Date() // <--- default ctor.
{
month = 1;
day = 1;
year = 2001;
}
Date(int m, int d, int y) // <--- Overloaded ctor.
{
month = m; // <--- Changed.
day = d; // <--- Changed.
year = y; // <--- Changed.
}
~Date() // <--- Need to define a default dtor.
{
}
void setDay(int d)
{
if (d < 1 && d > 31)
{
day = 1; // <--- Changed.
}
else
{
day = d;
}
}
void setMonth(int m)
{
if (m < 1 && m > 12)
{
month = 1; // <--- Changed.
}
else
{
month = m;
}
}
void setYear(int y)
{
if (y < 1900 && y > 2020)
{
year = 2001; // <--- Changed.
}
else
{
y = year;
}
}
void showDate()
{
std::cout << "\n "
<< (month < 10 ? "0" : "") << month << '/'
<< (day < 10 ? "0" : "") << day << '/'
<< year << std::endl;
}
};
int main()
{
Date date;
//Date date(5, 5, 2010);
date.showDate();
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0;
}
|
This should help you get started. In the class I have put comments to show you what I have changed.
As an example when you write:
1 2 3 4 5 6 7 8 9 10 11 12
|
void setMonth(int m);
{
if (m < 1 && m > 12)
{
month = 1; // <--- Changed.
}
else
{
month = m;
}
}
|
Ending line 1 with a semi-colon makes a good prototype or for the class a forward declaration. But this means the block that follows is not part of a function as you want;
Also when you wrote the function "Date" the closing } brace needs to come after the "year" not at the ens of all the other functions.
For your set functions you wrote:
void setDay(int);
First you need to remove the semi-colon to make it a function and then you need to add "d" to the parameter to define it for use in the function.
In your overloaded ctor you are doing things backwards.
m = month;
"m" is the variable you are sending to the function. "month" is the class member variable and at this point has no value, so you are setting "m" to a garbage value. It needs to be reversed to be right.
Here is a tip for you:
When you store "01" into an "int" variable the leading zero is dropped. Later as in the "showDate" function you will have to add this zero back if you want it. What I did in the "showDate" function is an easy way of doing this. It can be replaced with if statements, but makes the code longer.
Looking back at your instructions the overloaded ctor needs to check the information sent to it to make sure they are valid numbers and if not set a default date to use otherwise set the class member variables to the information sent.
You will also need something to deal with the other date formats. My first thought is to do this in the "showDate" function. Also thinking about using a switch to choose the format. I will have to work on that a little.
The above code should give you a better idea of how the class is written and what you need to do.
Any questions let me know. As it is it works. Changing the comments on lines 91 and 92 will allow you to test the default and overloaded ctors. This will help you in writing the rest of the program.
Hope that helps,
Andy