-
(Date dt)??? I'm lost...
Hi all!
OK, so here is my question (code is below): class name for the below project is Date. In the below functions that return a boolean and a string, what is (Date dt)? I'm used to seeing parameter syntax as (datatype dataname). What the heck is Date doing where the data type normally goes? Where did this "dt" come from? It kind of fell out of the sky, there is no earlier reference to it in the data declaration, and it is in the method declaration section as :
bool Date::isWeekday(Date dt);
string Date::NameOfDay(Date dt);
Would someone explain to me what this is and what is does in the program? I'm so lost and frustrated, and my professor thinks I'm a dummy, but the text does not explain at all!
-Patrick
__________________________________________________
Code:
bool Date::isWeekday(Date dt)
{
int day;
day = dt.dayOfWeek();
if(day >= 2 && day <= 6)
return true;
else
return false;
}
string Date::nameOfDay(Date dt)
{
int day;
day = dt.dayOfWeek();
switch(day)
{
case 0:
return "Satruday";
break;
case 1:
return "Sunday";
break;
case 2:
return "Monday";
break;
case 3:
return "Tuesday";
break;
case 4:
return "Wednesday";
break;
case 5:
return "Thursday";
break;
case 6:
return "Friday";
break;
}
}
-
One more thing....
Is this creating an object "dt" of the type "Date"? Can objects be created w/in parameter/argument in this manner? If it is an object of type "Date", I don't see any initial values...
Thanks again,
-Patrick
-
A class is a data type, only it is defined by a programmer,and
consists of data (ints, chars,floats,other class types etc), and
methods to manipulate that data. You declare an instance of
a class just as you would declare an int, and you can pass that
instance to a function just as you would as normal. The
isWeekday and nameOfDay functions are part of the Date class
(member functions). Notice that dayOfWeek is as well: this line
demonstrates how member functions are called:
day = dt.dayOfWeek();
Notice the '.' - that means that the dayOfWeek function is being
called against the class instance dt. General form:
class_instance.member_function (args);
The strange thing about this code is that because isWeekday and
nameOfDay are member functions, they are called against
instances of a class, but they are taking a class of the same type
and returning information about that instance, as opposed to the
instance it's being called against.
That's poor design, judging by what the functions do.
-
dt is an object of type date.
Now,...
The first line reads bool Date::isWeekday(Date dt)
This defines a function named isWeekday belonging to the Date class. This function returns a bool and accepts a Date object as a parameter. The parameter has been named dt. When this function is called, a Date object must be passed. That object will be recognized inside the function as having the name dt.
This is procedural programming. You may want to take a look at the functions tutorial on this website.