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:
#include <iostream>
usingnamespace 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?
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.
#include <iostream>
usingnamespace 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 usingnamespace 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.
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?
#include <iostream>
usingnamespace 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.