2

This class, specifically the lines in bold are confusing me. Why do we have to mention Time twice? What would happen if I deleted the first bold line?

Time.h

class Time {
public:
    Time(); // this is "the first bold line"
    Time(int h, int m, int s); // this is another "lines in bold"
    void set(int h, int m, int s);
    void print();
    int allSeconds();
    void difference(Time t);
    int getHour();
    int getMinute();
    int getSecond();
    void setHour(int h);
    void setMinute(int m);
    void setSecond(int s);

private:
    int hour, minute, second;
};
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
rddead
  • 103
  • 10
  • 4
    [Find a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), read it, especially the parts about *function overloading*. – Some programmer dude Feb 29 '16 at 09:36
  • 1
    Those are two different constructors... – Bob__ Feb 29 '16 at 09:37
  • I knew this was a pretty dumb question, but textbooks haven't arrived in the bookstore yet + the slides don't really explain this. Thanks for answering though. – rddead Feb 29 '16 at 11:29
  • 1
    Re: "Thinking in Java" -- if you're learning C++ this is a horrible idea. Java programmers have to unlearn many wrong things when they move to C++. – Pete Becker Feb 29 '16 at 15:48
  • I'm continuing my C++ and starting Java (horrible idea? that's university for you) atm but since this is a C++ question, the above was meant to say "Thinking in C++". Both seem like pretty good books though. – rddead Feb 29 '16 at 17:11

3 Answers3

1

Time(); is the declaration of the default constructor. It's called when something like Time t; is encountered.

Because the additional constructor Time(int h, int m, int s); has been supplied, the compiler will not generate a default constructor automatically.

You can tell the compiler to adopt the compiler-generated default constructor by writing Time() = default;. Alternatively, supply default arguments for your 3 argument constructor: it can then stand in for the default one.

Some parts of the C++ standard library (especially containers) require that an object is default-constructable. So if you miss it out then you might get some compile errors depending on how your class is used.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

If the 'bold line' would be deleted, the class Time would have no parameterless constructor. The compiler would not generate one, as a non-parameterless constructor is defined. The actual effect would depend on whether any part of the overall implementation uses the parameterless constructor or not.

The 'bold line' is not a repetition; in fact, two different constructors with different signatues are declared.

Codor
  • 17,447
  • 9
  • 29
  • 56
0

They're not class definition, they're constructors. You'll see what would happen if delete the first one (i.e. the default constructor) with the following code.

int main() {
    Time time1;           // 1st (default) constructor called
    Time time2(12, 0, 0); // 2nd constructor called
    return 0;
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405