#include <iostream>
#include "datestruct.h"
usingnamespace std;
int main()
{
int year = 0;
int month = 0;
int day = 0;
cout << "Enter a year: " << endl;
cin >> year;
cout << "Enter a month: " << endl;
cin >> month;
cout << "Enter a day: " << endl;
cin >> day;
Date input = DateSet(year, month, day);
while (true)
{
cout << endl << "Okay, now tell me that date again: " << endl;
cout << "Enter a year: " << endl;
cin >> year;
cout << "Enter a month: " << endl;
cin >> month;
cout << "Enter a day: " << endl;
cin >> day;
if (year != getYear(input))
{
cout << "Wrong. The correct year is " << getYear(input) <<endl;
}
if (month != getMonth(input))
{
cout << "Wrong. The correct month is " << getMonth(input) << endl;
}
if (day != getDay(input))
{
cout << "Wrong. The correct day is " << getDay(input) << endl;
}
if ((year == getYear(input)) &&
(month == getMonth(input))
&& (day == getDay(input)))
{
DatePrint(input);
returnfalse;
}
}
}
datestruct.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ifndef DATESTRUCT_H_INCLUDED
#define DATESTRUCT_H_INCLUDED
struct Date
{
int nMonth;
int nDay;
int nYear;
};
void DatePrint(Date input);
int getYear(Date input);
int getMonth(Date input);
int getDay(Date input);
Date DateSet(int year, int month, int day);
#endif // DATESTRUCT_H_INCLUDED
#include <iostream>
#include "datestruct.h"
usingnamespace std;
int getYear(Date input)
{
return input.nYear;
}
int getMonth(Date input)
{
return input.nMonth;
}
int getDay(Date input)
{
return input.nDay;
}
Date DateSet(int year, int month, int day)
{
Date input;
input.nYear = year;
input.nMonth = month;
input.nDay = day;
return input;
}
void DatePrint(Date input)
{
cout << "Good job. The date you entered was: " << endl;
cout << getYear(input) << " " << getDay(input) << " " << getMonth(input) << endl;
}
The program asks the user for a year, month, and day, then makes the user repeat them. If the user gets any of the numbers wrong, it loops until all inputted numbers are correct.
However, seeing as how we're about to start seeing classes in my current C++ class, I'd like to know how to change it to a class instead of a structure.
I've read up on classes, and I know how to write a class-based program but on a much simple scale and simply would like help on this.
#ifndef DATECLASS_H_INCLUDED
#define DATECLASS_H_INCLUDED
class Date
{
private:
int m_nMonth;
int m_nDay;
int m_nYear;
public:
void DatePrint(Date input);
int getYear(Date input);
int getMonth(Date input);
int getDay(Date input);
Date(int year, int month, int day);
//Date DateSet(int year, int month, int day);
};
#endif // DATECLASS_H_INCLUDED
Well, classes require either the "."(dot) or the "->"(for pointers to objects).
You can't call a class member function or variable withouth using one of this 2 operators.
For the get functions remove the argument. For those functions you needed arguments only because you need to acces the members of the struct. In this case you already have acces to them.
As for DatePrint remove the argument and use the variables
instead of
it should be
[code]
void Date::DatePrint()
{
cout << "Good job. The date you entered was: " << endl;
cout << m_nYear << " " << m_nDay << " " << m_nMonth << endl;
}
Remember, when you use the class your member variables, even if they are public, private or protected are visible in the scope of the member functions.
Partly correct.
A constructor is needed when you need to initialize a object(instance of a class) with certin values.Usually if a constructor, destructor and copy constructor are not defined by the user, the compiler generates them automaticly. In most non trivial cases you will need to make a cunstructor and a destructor atleast. You can have more constructors with diferent signatures(diferent argument number, types).
Also about the variables, no it's not because of your constructor.
Any variable that is a member of a class is visible within the member functions of that class. The constructor only has the role of initialising those variables. But the initialisation can be done in other functions as well.
For exemple you could have a function called Initialize that would do that and in the constructor you only call the apropriate initialize based on the arguments you give to the constructor.
Hope i was clear enough(i sometimes tend to be rather unclear in my explaining).