Reputation:
What does the code:
class Time {
private:
int hours;
int minutes;
int seconds;
public:
Time() { hours = 0; minutes = 0; seconds = 0; }
};
differ from:
class Time {
public:
Time();
private:
int hours;
int minutes;
int seconds;
};
Time::Time() {
hours = 0;
minutes = 0;
seconds = 0;
}
Sorry if the question is oversimple but I am very new to C++. I run both codes and I see no difference...
Upvotes: 0
Views: 65
Reputation: 8587
Maybe offtopic, but I believe it is a good habit to do it in the initialization list. For primitive types this wouldn't make a difference but it will if you have object members.
Time()
: hours(0)
, minutes(0)
, seconds(0) {}
Upvotes: 1
Reputation: 172894
If you run the code, they do the same thing.
But in the 1st version, the default ctor will become an inline function, because it's defined inside the class.
According to the working draft standard, [class.mfct] (9.3/2 in N3337)
A member function may be defined (8.4) in its class definition, in which case it is an inline member function (7.1.2), or it may be defined outside of its class definition if it has already been declared but not defined in its class definition.
Upvotes: 0
Reputation: 4873
Functionally in regards to what you see, there is no difference. Both will do the same thing.
Now, not sure if you'll understand the actual difference if you're new to programming, but nonetheless, there is a difference in the assembly code, which doesn't have higher level methods, or non-global variables.
The first one is inline, which means anywhere you do Time timeObj
in the assembly code, you will actually get the inline code placed there. Which is something like the below happening. Note that in your code, it will not compile.
Time timeObj;
timeObj.hours = 0;
timeObj.minutes = 0;
timeObj.seconds = 0;
In your example, that is fine, as it's simple. However, in more complicated classes, you may add a few hundred lines to every place you make a new version of that object, which makes the code much longer and more inefficient.
The second one is defined as non-inline, which means that every time you make a Time object, the assembly code will jump to some other point in the code, execute the code, and then jump back. Again, you can't actually do the following, but it is similar to what happens.
Time timeObj;
timeObj.Time();
That's a very basic way to explain it, and if it doesn't make sense, don't concern yourself with it too much yet. In general, inline definitions are fine if they're a few lines of simple code, like yours is. But any longer and it should be not be implemented as an inline function.
BTW, you can make your inline version even better by doing the following:
Time() { hours = minutes = seconds = 0; }
Upvotes: 1
Reputation: 4213
class Time {
private:
int hours;
int minutes;
int seconds;
public:
Time() { hours = 0; minutes = 0; seconds = 0; }
};
This code declares and defines the Time
class' default constructor.
class Time {
public:
Time();
private:
int hours;
int minutes;
int seconds;
};
Time::Time() {
hours = 0;
minutes = 0;
seconds = 0;
}
This code declares the Time
class' default constructor in the class definition and then defines it outside of the class definition. Usually the two would be separated into Time.h
and Time.cpp
like so:
Time.h
class Time {
public:
Time();
private:
int hours;
int minutes;
int seconds;
};
Time.cpp
#include "Time.h";
Time::Time() {
hours = 0;
minutes = 0;
seconds = 0;
}
Upvotes: 1