Understanding C++ Constructors | Homework Question

  • Comp Sci
  • Thread starter AKJ1
  • Start date
  • Tags
    C++
In summary: The [b] are probably some copied syntaxhighlighting, but those codes are not supported in code tags here.In summary, the programmer is getting compiler warnings that hours, milli, seconds, and hours is not initialized in the constructor. The programmer should set them to zero within the constructor.
  • #1
AKJ1
43
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
  • #2
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.
 
  • #3
mfb said:
The [b] are probably some copied syntaxhighlighting, but those codes are not supported in code tags here.
I removed them...
 

1. What is a constructor in C++?

A constructor in C++ is a special member function that is used to initialize objects of a class. It is called automatically when an object of the class is created and is responsible for setting initial values to the data members of the object.

2. How is a constructor different from a regular function?

A constructor is different from a regular function in several ways. Firstly, it has the same name as the class and does not have a return type, not even void. Secondly, it is automatically called when an object is created, whereas a regular function needs to be called explicitly. Lastly, it is used for initialization purposes, while a regular function can perform any desired task.

3. Can a class have multiple constructors?

Yes, a class can have multiple constructors, also known as constructor overloading. This means that a class can have more than one constructor with different parameters. The appropriate constructor is called based on the arguments passed during object creation.

4. What is the default constructor in C++?

The default constructor in C++ is a constructor that takes no arguments. It is automatically created by the compiler if no constructor is explicitly defined in the class. It initializes the data members of the class with their default values.

5. Can a constructor be declared as private?

Yes, a constructor can be declared as private. This is known as a private constructor and it is used to prevent the creation of objects of the class. It is often used in singleton design pattern where only one instance of the class is allowed.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
844
  • Engineering and Comp Sci Homework Help
Replies
2
Views
949
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
Back
Top