2

I've just started to learn C++.

I have this struct:

struct dateTime
{
    int sec;
    int min;
    int hour;
    int day;
    int mon;
    int year;
    bool daylightSaving;
    char timeZone;
};

And I need to set daylightSaving to false by default.

How can I do it? Maybe I have to use a class instead of a struct.

2
  • 2
    Add constructor dateTime() : daylightSaving ( false ) {} Commented Dec 15, 2019 at 18:55
  • 3
    struct and class are the same thing in c++, just different defaults. Commented Dec 15, 2019 at 18:55

2 Answers 2

3

You can write for example

struct dateTime
{
    int sec;
    int min;
    int hour;
    int day;
    int mon;
    int year;
    bool daylightSaving = false;
    char timeZone;
};
Sign up to request clarification or add additional context in comments.

Comments

1

So you say in C++, what about having a default constructor initializing all values?

struct dateTime
{
 dateTime()
 : sec(0)
 , min(0)
 , hour(0)
 , day(0)
 , mon(0)
 , year(0)
 , daylightSaving(false)
 , timeZone('a') //Are you sure you just want one character? time zones have multiple... UTC GMT ...
 {}

...
}

You can use a class instead, but the difference is only that all values are private by default. So you need

class ...
{
   public:
   ...
}

to have the same behavior as with the struct.

1 Comment

I’m using a char to store a value between-127 and 127 (forget the unsigned, is a mistake).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.