Thread: College Project Creating a class in C++: Help with the constructor and member functio

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    7

    College Project Creating a class in C++: Help with the constructor and member functio

    Hello, thanks for visiting my thread.

    The assignment requires that I create a class named Time. It has 3 data members, hour, minute, and second (all are ints). This class should have a constructor with 3 parameters to initialize the 3 data members. There are required bounds for each data member. The hour value should be in the range 0-23, if not set it to 12. The minute value should be in the range 0-59, if not set it to 0. The second value should be in the range 0-59, if not set it to 0. A set and get function is required for each data member. Lastly, a member function named "displayTime" that displays the hour, minute, and second separated by colons. Additionally, if any of the values in hour, minute, and second are between 0-9 then it needs to display as "00", "01", "02", etc.

    My current roadblock is that I don't know how to initialize a class with more than one parameter. There isn't an example in the text or online exercises that I can work from. I've tried modifying an example that initializes a single data member, but the compiler doesn't seem to like it.

    The second roadblock I foresee is the displyTime member function. I literally have no concept of what this member function is supposed to look like in terms of the syntax.

    My work-in-progess code is:

    Program code:
    Code:
    #include <iostream>#include <string>
    #include "Time.h"
    
    
    using namespace std;
    
    
    int main()
    {
        Time theTime(5,10,10);
        
        cout << "Current time is: " << theTime.displayTime();
    
    
        cout << "\n Please enter the Hour";
        int theHour;
        cin >> theHour;
        theTime.setHour(theHour);
        
        cout << "\n Please enter the Minute";
        int theSecond;
        cin >> theSecond;
        theTime.setSecond(theSecond);
        
        cout << "\n Please enter the Second";
        int theMinute;
        cin >> theMinute;
        theTime.setMinute(theMinute);    
        
        cout << "Current time is: " << theTime.displayTime();
    
    
    }
    Header code:
    Code:
    #include <string>
    
    class Time {
    
    
    public:
        Time () : hour{0} : minute{0}    : second{0} {
        }
        
    void setHour (int theHour) {
        if (theHour < 0 or theHour > 23)
        hour = 12;
        else 
        hour = theHour;
    }
    
    
    int getHour () {
        return hour;
    }
    
    
    void setMinute (int theMinute) {
        if (theMinute < 0 or theMinute > 59)
        minute = 0;
        else
        minute = theMinute;
    }
    
    
    int getMinute () {
        return minute;
    }
    
    
    void setSecond (int theSecond) {
        if (theSecond < 0 or theSecond > 59)
        second = 0;
        else
        second = theSecond;
    }
    
    
    int getSecond () {
        return second;
    }
    
    
    
    
    private:
        int hour;
        int minute;
        int second;
    };
    Last edited by poptonite; 02-03-2018 at 12:52 PM.

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    About passing parameters to a ctor: Constructor in C++ - Default Constructor - Parameterized Constructor - Overloaded Constructor
    Since you call displayTime without parameters and don't check one it should be void displayTime();

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    7
    PROGRESS HAS BEEN MADE!!!

    After several hours, I got the constructor to work. I also have the set and get functions working, along with the bound restrictions.

    Now I have to move onto the member function displayTime. I can write something that's functionally similar using getHour, getMinute, and getSecond such as

    Code:
    cout << "Now the time is: " << theTime.getHour();    
    cout << ":" << theTime.getMinute();
    cout << ":" << theTime.getSecond();
    I just got to wrap it into a single member function while accounting for the 01, 02, 03 values.

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    7
    I have the member function displayTime working, but without the requirement that it display each value for hour, minute, and second as 2 digits. ( 1 second should display as 01, 2 should be 02, etc.

    It looks like:
    Code:
    void displayTime () {    
        std::cout << "\n\nNow the time is: " << hour;
        std::cout << ":" << minute;
        std::cout << ":" << second;
    }
    If someone can point me in the right direction to code in the 2 digit display requirement I would appreciate it.
    Last edited by poptonite; 02-03-2018 at 05:12 PM.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Code:
    // either do it manually
        if (h < 10) cout << '0';
        cout << h << ':';
        if (m < 10) cout << '0';
        cout << m << ':';
        if (s < 10) cout << '0';
        cout << s << '\n';
    
    // or use the iostream formatting facilities
        cout << setfill('0');
        cout << setw(2) << h << ':';
        cout << setw(2) << m << ':';
        cout << setw(2) << s << '\n';
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Nov 2017
    Posts
    7
    The project is complete. Thank you to OldGuy2 and john.c for helping.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copy constructor for a class with an abstract member
    By hebrerillo in forum C++ Programming
    Replies: 1
    Last Post: 01-03-2018, 04:04 AM
  2. Replies: 3
    Last Post: 12-31-2011, 02:23 AM
  3. Replies: 51
    Last Post: 02-09-2009, 05:35 PM
  4. Replies: 6
    Last Post: 07-29-2008, 04:37 AM
  5. Replies: 5
    Last Post: 07-25-2008, 04:37 AM

Tags for this Thread