How do I turn this program from a structure to a class?

main.cpp
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
#include <iostream>
#include "datestruct.h"

using namespace 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);
            return false;
        }
    }
}


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 


datestruct.cpp
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
#include <iostream>
#include "datestruct.h"

using namespace 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.

Thank you!
Okay, so this compiles and as such, I think I got it. Let me know if you guys have any comments or anything to make my code 'better' or 'neater'.

main.cpp
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
#include <iostream>
#include "dateclass.h"

using namespace 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(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 != input.getYear(input))
        {
            cout << "Wrong. The correct year is " << input.getYear(input) <<endl;
        }

        if (month != input.getMonth(input))
        {
            cout << "Wrong. The correct month is " << input.getMonth(input) << endl;
        }

        if (day != input.getDay(input))
        {
            cout << "Wrong. The correct day is " << input.getDay(input) << endl;
        }

        if ((year == input.getYear(input)) &&
            (month == input.getMonth(input))
            && (day == input.getDay(input)))
        {
            input.DatePrint(input);
            return false;
        }
    }
}


dateclass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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 


dateclass.cpp
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
#include <iostream>
#include "dateclass.h"

using namespace std;

Date::Date(int year, int month, int day)
{
    m_nMonth = month;
    m_nDay = day;
    m_nYear = year;
}

int Date::getYear(Date input)
{
    return m_nYear;
}

int Date::getMonth(Date input)
{
    return m_nMonth;
}

int Date::getDay(Date input)
{
    return m_nDay;
}

/*Date DateSet(int year, int month, int day)
{
    Date input;

    m_nYear = year;
    m_nMonth = month;
    m_nDay = day;

    return input;
}*/


void Date::DatePrint(Date input)
{
    cout << "Good job. The date you entered was: " << endl;
    cout << input.getYear(input) << " " << input.getDay(input) << " " << input.getMonth(input) << endl;
}
Someone, please?

I also had another question: is there any way to not have to use the dot operator in main?
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.
Okay, thank you!

Was my conversion from a structure to class correct, then?
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
1
2
3
4
5
void Date::DatePrint(Date input)
{
    cout << "Good job. The date you entered was: " << endl;
    cout << input.getYear(input) << " " << input.getDay(input) << " " << input.getMonth(input) << endl;
}

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.
In this case, I only have access to them because of the constructor, correct?

And had I left them like they were, would that have been bad programming practice or..? I'm wondering because it still compiled.

Also, just to get this right - after I make the constructor, every public function within the class does not need to have arguments, correct?
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).
Topic archived. No new replies allowed.