Why are these functions called before they are defined?

These are cpp functions of time. The code defines the functions of the time.h file at this time .cpp. My question is: why is this function definition possible if the functions inside this fct are subsequently defined? Thanks you

void Time::setTime(int hour, int minute, int second)
{
    sethour(hour);
    setminute(minute);
    setseconds(seconds);
}

void Time::sethour( int h)
{ ....
+5
source share
3 answers

You do not need a definition to call a function, you only need an declaration. The compiler is only satisfied with the declaration. The compiler requires code generation and requires definition, but it does not matter when you define them, if you do.

In your case, the declaration of each member function is visible to all other member functions, even if it appears after the class definition after:

class Time
{
   void setTime();  //setTime knows about sethour even if it before
   void sethour();
};

, , . - :

void foo();
void goo()
{
    foo(); //can call foo here even if it just declared and not defined
}
+7

, - (, ) .

, ; . , , ( , ) , , - . (.. ) .

+2

You can choose when to define them.

0
source

All Articles