MS Rumi. Powered by Blogger.
Showing posts with label Class and Objects. Show all posts
Showing posts with label Class and Objects. Show all posts

Saturday, 18 October 2014

Object Oriented Programming Task For Time Clock In C++

OOP Time Clock Parametrized and Default Constructor

Task To Convert Twenty Four Hour Format To Twelve Hour Format

Complete the function definitions of the following ADT
class Time
{
public:
void setTime( int, int, int ); // set hour, minute, second
void setHour( int ); // set hour
void setMinute( int ); // set minute
void setSecond( int ); // set second
int getHour(); // return hour
int getMinute(); // return minute
int getSecond(); // return second
void printTwentyFourHourFormat(); // print universal time

void printTwelveHourFormat(); // print standard time 

private:
int hour; // 0 - 23 (24-hour clock format)
int minute; // 0 - 59
int second; // 0 - 59
};


Task Solution

#include<iostream.h>
#include<iomanip.h>
class Time
    {
        public:
            void setTime( int hour, int minute, int second ) // set hour, minute, second
            {
                setHour(hour);
                setMinute(minute);
                setSecond(second);
            }
            void setHour( int h ) // set hour
            {
                hour=(h>=0 && h<24)? h:0;
            }
            void setMinute( int m ) // set minute
            {
                minute=(m>=0 && m<60)? m:0;
            }
            void setSecond( int s ) // set second
            {
                second=(s>=0 && s<60)? s:0;
            }
            int getHour() // return hour
            {
                return hour;
            }
            int getMinute() // return minute
            {
                return minute;
            }
            int getSecond() // return second
            {
                return second;
            }
            void printTwentyFourHourFormat() // print universal time
            {
                cout << setfill( '0' ) << setw( 2 ) << hour << ":" << setw( 2 ) << minute << ":" << setw( 2 ) << second;
            }
            void printTwelveHourFormat() // print standard time
            {
                cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
                << ":" << setfill( '0' ) << setw( 2 ) << minute
                << ":" << setw( 2 ) << second << ( hour < 12 ? " AM" : " PM" );
            }
            //Void incSec(int=1); // increment in the second (01)
            //Void incMin(int=1); // increment in the minute (01)
            //Void incHour(int=1); // increment in the hour (01)
        private:
            int hour; // 0 - 23 (24-hour clock format)
            int minute; // 0 - 59
            int second; // 0 - 59
    };

void main ()
{
    Time t;
    t.setTime(21,4,55);
    t.printTwelveHourFormat();
    cout<<endl;
    t.printTwentyFourHourFormat();
}

E-mail Newsletter

Sign up now to receive breaking news and to hear what's new with us.

Recent Articles

TOP