No, you have to set the return type before the name on all class functions (just like regular functions) unless it's a constructor or destructor.
1 2 3 4 5 6 7 8 9 10 11 12 13
class DigitalTime
{
public:
DigitalTime(int theHour, int theMinute); //Constructor
~DigitalTime( ) { } //Destructor
int getHour( ) const { return (hour); }
int getMinute( ) const { return (minute); }
void advance(int minutesAdded);
private:
int hour;
int minute;
};
The destructor, getHour and getMinute functions I declared INSIDE the class, so you don't need to set them up later. The constructor and advance functions are only prototypes though, so you would have to define them later like this..
#include<iostream>
usingnamespace std;
void doStuff(); //This is a function declaration.
int main()
{
doStuff(); //This is a function call.
return 0;
}
void doStuff() //This is the function defenition or impelemtation.
{
cout << "Hello world!\n";
}
In order to call a function in C++ implemented or at least declared before the it's call.
This is also legal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<iostream>
usingnamespace std;
void doStuff() //This is the function defenition or impelemtation.
{
cout << "Hello world!\n";
}
int main()
{
doStuff(); //This is a function call.
return 0;
}
void doStuff() //This is the function defenition or impelemtation.
{
cout << "Hello world!\n";
}
This is not legal since the compiler does not see the function declaration or implementation before the function call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream>
usingnamespace std;
//Function declaration missing here.
int main()
{
//The function call happens here first which does not make the compiler happy.
doStuff(); //This is a function call.
return 0;
}
void doStuff() //This is the function defenition or impelemtation.
{
cout << "Hello world!\n";
}
In general if you find this general form: return_type function_name(parameter list); //notice the semi colon
This is a function deceleration
but, if you find this:
1 2 3 4
return_type function_name(parameter list)
{
//body here
}
No it is not a mistake in the book the functions in the class are all function declarations because they have no implementations in them there is no body for the functions!