Quick Question about Classes

Pages: 12
I am learning classes this week and I have an assignment to use a class labeled date to have a user input a date, then read it back in 1/1/1111 form. My code stands as follows:

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

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

int main()
{
	cout <<"What day is it?\n";
	cin >>date.setDay;
	cout <<"What Month is it?\n";
	cin >>date.setMonth;
	cout <<"What Year is it?\n";
	cin >>date.setYear;
	cout <<"\n\n\t\t\t\tThat makes it "<<date.getMonth()<<"/"<<date.getDay()<<"/"<<date.getYear()<<"!";
	system("pause");
	return 0;
}


Now I know I'm missing a few things here. I believe the problem is that I'm not correctly storing the variables from main into the date class. Is this assumption right? How would I fix this?
Classes don't quite work like that.

When you declare a class, its the same as making your own variable type. You can think of it as creating a blueprint. Then from this blueprint(the class), you can create a instance of the class.

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

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

int main()
{
        date date;
	cout <<"What day is it?\n";
	cin >>date.setDay;
	cout <<"What Month is it?\n";
	cin >>date.setMonth;
	cout <<"What Year is it?\n";
	cin >>date.setYear;
	cout <<"\n\n\t\t\t\tThat makes it "<<date.getMonth()<<"/"<<date.getDay()<<"/"<<date.getYear()<<"!";
	system("pause");
	return 0;
}
So the line you added at 21 creates an instance of date from class date? Now what would the next step(s) be because it still gets an error when I compile it. Is it something along the lines of a "void date::setDay" argument?
You need to create an object of type date before you can call any of it's functions. Use cin to get a value first, then call the function with that value.

1
2
3
4
5
date MyDate;  //this creates a date object
int d;
cout <<"What day is it?\n";
cin >> d;
MyDate.setDay(d);


Where are the definitions of your functions?

Also, it is good practice to name the member variables with a leading m_ as in m_day. This is a reminder to you that it is a member variable, and makes naming function arguments easier.

And don't use using namespace std; it brings in heaps of stuff polluting the global namespace and causing naming conflicts for variables and function names.

Either put std:: before each std thing or put these after the include directives:

1
2
3
4
using std::cout;
using std::cin;
using std::endl;  //similar for other std things


I tend to do a mixture- do the using statements for frequently used things, and std:: for less frequent things like std::find or std::sort for example.

Instead of having a set / get function for each member variable, think about how it might happen in real life, and organise your functions that way. You could have a function setDate which asks for and sets all 3 variables. And a PrintDate function that prints out the date in the right format. This has the added advantage that the code is encapsulated in the class, leaving minimal code in main(). Remember that class functions have access to the classes' private & protected members. So main might look like this:

1
2
3
4
5
6
7
8
9
int main() {

date MyDate;

MyDate.setDate();
MyDate.PrintDate;

return 0;
}


Be aware hat you can build up cout statements over several lines - there is no need to have it all on one line - though line 27 is not too bad. I am just pointing out something for the future. It could be done equivalently:

1
2
cout <<"\n\n\t\t\t\tThat makes it "<<date.getMonth();
cout <<"/"<<date.getDay()<<"/"<<date.getYear()<<"!";


There is a bit of a rule that code should not exceed 80 chars per line for readability, and functions not longer than 80 lines.

Hope all this helps :)
This defiantly does help a lot. Unfortunately I have to use namespace std because of the class I'm in. I'll give it some work and post any results/questions again :)
Are you sure that you are forced to use namespace std? It's pretty pathetic for teachers to force their student to do something bad versus allowing acceptable standard practice. Are you going to loose marks for doing what I suggested?
I changed the code to
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
#include <iostream>
using namespace std;

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

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;
}


And now I get the error of
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
1>  13.5.cpp
1>13.5.obj : error LNK2028: unresolved token (0A00034D) "public: void __thiscall date::setDay(int)" (?setDay@date@@$$FQAEXH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>13.5.obj : error LNK2028: unresolved token (0A00034E) "public: void __thiscall date::setMonth(int)" (?setMonth@date@@$$FQAEXH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>13.5.obj : error LNK2028: unresolved token (0A00034F) "public: void __thiscall date::setYear(int)" (?setYear@date@@$$FQAEXH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>13.5.obj : error LNK2028: unresolved token (0A000350) "public: int __thiscall date::getDay(void)const " (?getDay@date@@$$FQBEHXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>13.5.obj : error LNK2028: unresolved token (0A000351) "public: int __thiscall date::getMonth(void)const " (?getMonth@date@@$$FQBEHXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>13.5.obj : error LNK2028: unresolved token (0A000352) "public: int __thiscall date::getYear(void)const " (?getYear@date@@$$FQBEHXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>13.5.obj : error LNK2019: unresolved external symbol "public: int __thiscall date::getYear(void)const " (?getYear@date@@$$FQBEHXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>13.5.obj : error LNK2019: unresolved external symbol "public: int __thiscall date::getDay(void)const " (?getDay@date@@$$FQBEHXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>13.5.obj : error LNK2019: unresolved external symbol "public: int __thiscall date::getMonth(void)const " (?getMonth@date@@$$FQBEHXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>13.5.obj : error LNK2019: unresolved external symbol "public: void __thiscall date::setYear(int)" (?setYear@date@@$$FQAEXH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>13.5.obj : error LNK2019: unresolved external symbol "public: void __thiscall date::setMonth(int)" (?setMonth@date@@$$FQAEXH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>13.5.obj : error LNK2019: unresolved external symbol "public: void __thiscall date::setDay(int)" (?setDay@date@@$$FQAEXH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>C:\Users\Al\Desktop\UAT\CSC 102\Week 5\13.5\Debug\13.5.exe : fatal error LNK1120: 12 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
To be honest I have no idea if I'd lose marks. I lost a point for spelling a file name wrong earlier in the year so she is pretty strict :P. I'm not even sure what namespace std is to be truthful because we never learned about it. I've just used it in everything I've made so far.
You don't have definitions for your functions. The compiler needs to know what code to execute when you call a function.

Did you see what I was saying about the setDate & PrintDate functions.

I should have also mentioned that the class declaration should go into a date.h header file. The function definitions go into a date.cpp file . This is where you write the code for the functions - something you haven't done yet. The header file is included by whichever .cpp file needs it - in this case main.cpp (or whatever you call the file that has main() in it)

If you are using an IDE you should be able to get it to do this for you. On my system I use the class wizard - it does all kinds of good things.
Last edited on
To be honest I have no idea if I'd lose marks. I lost a point for spelling a file name wrong earlier in the year so she is pretty strict :P. I'm not even sure what namespace std is to be truthful because we never learned about it. I've just used it in everything I've made so far.


It probably won't make any difference for your program - I just mentioned it because I think it is good to break bad habits early.

If you asked your teacher about it, I am sure she would say it is fine because that is what is done in industry. Just say you read somewhere that it was a good idea to do this.

Namespaces are used to help avoid clashes in names of variables and functions - especially for larger projects, the larger the project the higher the probability of a clash. But it can cause problems in really small programs as well, because the std namespace has hundreds of functions in it. Here is an example I saw recently:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

//function declaration
double distance(double x1, double y1, double x2, double y2);

int main() {

distance (1.0, 2.0, 6.0, 5.0);
return 0;
}

distance(double x1, double y1, double x2, double y2) {
//function definition

}


The problem is that the function call to distance, actually calls std::distance which does not have 4 double parameters, and causes lots of of errors.

If you remove the using namespace std the program works fine.

So it is best to refer to std things explicitly using the std:: scope resolution.
Last edited on
So how do I define it? Would I use a statement such as int date::getDay() const?
OK, back to basics, do you know what a function definition is?
I changed it to:
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
#include <iostream>

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

int main()
{
	date MyDate;
	int numb;

	std::cout <<"What day is it?\n";
	std::cin >>numb;
	MyDate.setDay(numb);
	std::cout <<"What Month is it?\n";
	std::cin >>numb;
	MyDate.setMonth(numb);
	std::cout <<"What Year is it?\n";
	std::cin >>numb;
	MyDate.setYear(numb);
	std::cout <<"\n\n\t\t\t\tThat makes it "<<MyDate.getMonth()<<"/"<<MyDate.getDay()<<"/"<<MyDate.getYear()<<"!";
	system("pause");
	return 0;
}


but still get the same error........
My last post - what is a function definition?

With my earlier post with the distance function, lines 14 - 17 are a function definition (although with no code inside) what is different about the definition as opposed to the declaration?
This IdeasMan is a boss... You'd have been an awesome teacher.

My professor actually is anal, however, she wouldn't have deducted points for it, as long as you can tell her why you used std:: vs using namespace std; ..

In your example of the doubles, I didn't see where you'd get errors. It all looks correct.
@alistjazz
In your example of the doubles, I didn't see where you'd get errors. It all looks correct.


Did you understand my explanation of why it wouldn't work?

@aidenkael

Your other thread had a function that was defined.
A definition only provides a blueprint for the function. A declaration is the function itself
In your case you sould have declarations for your functions.
1 model is this:
1
2
3
4
void date::SetDay(int d)
{
   day = d;
}

In esence you need to mace the functions themselvs.
nedo wrote:
A definition only provides a blueprint for the function. A declaration is the function itself

You got declaration and definition mixed up.
closed account (3qX21hU5)
Here is a quick example of what definitions and declarations look like outside of a class.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using std::cout;
using std::endl;

int addnumbers(int one, int two);   // This is the declaration

int main()
{
	int n1 = 5;
	int n2 = 5;
	int total = 0;

	total = addnumbers(n1, n2);
	cout << "Your number is: " << total << endl;
	
	return 0;
}

int addnumbers(int one, int two)	// This is the definition
{
	return one + two;
}


Now you can see that the declaration only gives the basics of a function, like what type of parameters it accepts, how many parameters, and what type it returns. It doesn't give any code that tells what the function is suppose to do, that is where the definition comes in.

The definition defines what the function should do. It is the exact same as a declaration EXCEPT it has extra code between the {} that tells what the function does. For example in my example my definition tells the computer that it wants to add two variables together and return the sum.

I hope this might help a little, I'm not the greatest at explaining things and think TheIdeasMan was doing a much better job ;p.
Corect, my bad... i often mix the words up.
Pages: 12