Is this a function declaration?

Hi,

I am not so new to c++, but I still don't recognise the following code. Maybe I have forgotten something, or maybe you guys can explain:

class DigitalTime
{
public:
DigitalTime(int theHour, int theMinute);
DigitalTime( );

getHour( ) const;
getMinute( ) const;

void advance(int minutesAdded);
...

I read this code in a book, and I couldn't figure it out. I am asking:getHour and getMinutes are functions,right? So where is the return type?

Also, if this does not describes the problem good enough, please ask.
Thanks in advance.
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..
1
2
3
4
5
6
7
8
9
10
DigitalTime::DigitalTime(int theHour, int theMinute)
{
     hour = theHour;
     minute = theMinute;
}

void DigitalTime::advance(int minutesAdded)
{
     minute += minutesAdded;
}
Last edited on
Look at this example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>

using namespace 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>

using namespace 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>

using namespace 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
} 

This is a function definition.
OK thanks guys. So basically, its a mistake in the book, right? Because I copied it the way it was written.
Last edited on
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!
Last edited on
@OP: yes, it's a mistake. They need return types.

@Mohamed Fouad: you missed the point completely.
Sorry my bad.
thanks everyone. I can move on now, and let this rest in peace :)

no, seriously, thank you TheNoobie,Mohamed Fouad and filipe x3
Topic archived. No new replies allowed.