Understanding C++ Constructors | Homework Question

  • Context: Comp Sci 
  • Thread starter Thread starter AKJ1
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
SUMMARY

The discussion centers on the implementation of constructors in a C++ class named Time. A compiler warning arises due to uninitialized member variables hours, minutes, seconds, and milli in one of the constructors. The consensus is to initialize these variables to zero within the constructor for clarity and to avoid warnings. The Time class includes multiple constructors, a destructor, and methods for time representation and formatting.

PREREQUISITES
  • Understanding of C++ class structure and syntax
  • Familiarity with constructors and destructors in C++
  • Knowledge of member variable initialization
  • Basic understanding of time representation in programming
NEXT STEPS
  • Research C++ constructor initialization lists
  • Learn about C++ compiler warnings and how to resolve them
  • Explore best practices for class design in C++
  • Study the use of std::string for formatting output in C++
USEFUL FOR

C++ students, software developers working with object-oriented programming, and anyone looking to improve their understanding of class constructors and initialization in C++.

AKJ1
Messages
43
Reaction score
0
1. Homework Statement
In the third constructor in the second snippet of code, I keep getting a compiler warning that hours, milli, seconds, and hours is not initialized in the constructor. Should I just set them equal to zero within that constructor? I have never worked with multiple constructors before, so if something looks really off, please let me know. Thanks!

edit - I don't know why some things are showing up with a and in the code

2. Homework Equations

Code:
//.h file

#ifndef TIME_H_
#define TIME_H_
#include <iostream>
#include <string>
usingnamespace std;

/**

* Time class

*

* The Time class contains time as hours:minutes:seconds:milliseconds (AM/PM).

*/

class Time

{

public:

/**

* Constructor with zero values

*/

    Time();
    /**

    * Constructors with arguments

    */

    Time(long time);

    Time(int hours, int minutes, int seconds, int milli);
    /**

    * Deconstructor

    */

    virtual~Time();
    /**

    * Return time as a long value representing time in milliseconds

    */

   long asLong() const;
    /**

    * Display as a string in the format hours:minutes:seconds:milliseconds.

    * For example 1:45:30:56 PM

    *

    * The time is displayed as 24 hours if the 24 hour flag is set true.

    */

    std::string toString() const;
    /**

    * Enable/disable 24 hour time display

    */

    void set24Hour(bool value);
    /**

    * Return true if 24 hour time display is enabled

    */

    bool is24Hour() const;
private:
    int hours;

    int minutes;

    int seconds;

    int milli;

    long time;
    /**

    * Private members go here

    */

};
#endif/* TIME_H_ */
Code:
* Time.cpp
*
*
*/

#include "Time.h"

Time::Time()
{
    hours=0;
    minutes=0;
    seconds=0;
    milli=0;
    time=0;
}

Time::Time(int hr, int min, int sec, int mil)
{
    hours = hr;
    minutes = min;
    seconds = sec;
    milli = mil;
    time = 0;
}

Time::Time(long tm)
{
    time = tm;
}

Time::~Time()
{

}
 
Last edited by a moderator:
Physics news on Phys.org
AKJ1 said:
Should I just set them equal to zero within that constructor?
Set it to whatever is reasonable for your class. If zero is a good choice, set them to zero.

The [b] are probably some copied syntaxhighlighting, but those codes are not supported in code tags here.
 
mfb said:
The are probably some copied syntaxhighlighting, but those codes are not supported in code tags here.
I removed them...
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K