Quick Question about Classes

Pages: 12
Sorry for the delay in my postings, the last few days have been hell on earth for me (buddy of mine was jumped and I've spent most of the last two days in the hospital). Anyway I figured it out and my completed code is below. As you can see it wasn't the namespace std (although I do understand what you are saying in why it isn't always smart to use it), it was that I missed the definitions all together somehow (just like nedo explained).

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
#include <iostream>
using namespace std;

class date
{
private:
	int day;
	int month;
	int year;
public:
	void setDay(int);
	void setMonth(int);
	void setYear(int);
	int getDay() const;
	int getMonth() const;
	int getYear() const;
};

void date::setDay(int d)
{
	day=d;
}
void date::setMonth(int m)
{
	month=m;
}

void date::setYear(int y)
{
	year=y;
}

int date::getDay() const
{
	return day;
}

int date::getMonth() const
{
	return month;
}

int date::getYear() const
{
	return year;
}

int main()
{
	date MyDate;
	int numb;

	cout <<"What day is it?\n";
	cin >>numb;
	MyDate.setDay(numb);
	cout <<"What Month is it?\n";
	cin >>numb;
	MyDate.setMonth(numb);
	cout <<"What Year is it?\n";
	cin >>numb;
	MyDate.setYear(numb);
	cout <<"\n\n\t\t\t\tThat makes it "<<MyDate.getMonth()<<"/"<<MyDate.getDay()<<"/"<<MyDate.getYear()<<"!";
	system("pause");
	return 0;
}
OK, I am pleased to see that you have figured out the functions so that your program works now, but could you try to have the 2 functions I mentioned earlier, instead of all the get / set functions? Maybe a constructor that takes 3 arguments and sets all 3 member variables, in the one function. That way you can initialise all the member variables at once when you create the object - which is handy sometimes.

The other advice was splitting the code into a header file, date.cpp, and main.cpp. And renaming the member variables with a leading m_ as in m_Year.

I would like to see you get your first class program more correct from the start, and the stuff about namespace std was aimed at getting into good habits early. Remember that just because a program works, doesn't mean that it is as good as it could be, and now is the time to experiment with different ways of doing things.

Hope all goes well :)
Topic archived. No new replies allowed.
Pages: 12