Timer OOP Method (I/O)

This is a discussion on Timer OOP Method (I/O) within the C++ Programming forums, part of the General Programming Boards category; Hi, here is an OOP based time program which is although static and works on input or initializing variables here ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    54

    Timer OOP Method (I/O)

    Hi, here is an OOP based time program which is although static and works on input or initializing variables here for u:


    Code:
    time1.h
    
    #pragma once
    class time1
    {
    public:
    time1(int=0, int=0, int=0);
    void setTime(int, int, int);
    void printMilitary();
    void printStandard();
    ~time1(void);
    private:
        int hour, minute, second;
        
    
        
    };
    Code:
    time1.cpp
    
    #include "time1.h"
    #include <iostream>
    using namespace std;
    
    time1::time1(int hr, int min, int sec)
    {
        setTime(hr, min, sec);
    }
    
    void time1::setTime(int h, int m, int s)
    {
        hour=  (h>=0 && h<24) ? h:0;
        minute=(m>=0 && m<60) ? m:0;
        second=(s>=0 && s<60) ? s:0;
    }
    
    void time1::printMilitary()
    {
        cout<< (hour<10 ? "0" : "") << hour << ":"
            << (minute <10 ? "0" : "")<<minute;
            
    }
    
    void time1::printStandard()
    {
        cout<< ((hour==0 || hour==12) ? 12 : hour % 12)
            << ":" << (minute < 10 ? "0" : "") << minute
            << ":" << (second < 10 ? "0" : "") <<second
            <<(hour < 12 ? " AM" : " PM");
    }
    time1::~time1(void)
    {
    }
    Code:
    source file source.cpp
    
    #include<iostream>
    #include"time1.h"
    using namespace std;
    void main()
    {
        time1 t1, t2(2), t3(21, 34), t4(12,25, 42), t5(27,74,99);
    
        cout<< "Default Time: \n";
        t1.printMilitary();
        cout<<endl;
        t1.printStandard();
    
        cout<< "\n\nHour input, minutes and seconds are default: \n";
        t2.printMilitary();
        cout<<endl;
        t2.printStandard();
    
        cout<<"\n\nhours and minutes specified, seconds default: \n";
        t3.printMilitary();
        cout<<"\n";
        t3.printStandard();
    
        cout<<"\n\nEverything is specified: \n";
        t4.printMilitary();
        cout<<endl;
        t4.printStandard();
    
        cout<<"\n\nall invalid values given: \n";
        t5.printMilitary();
        cout<<endl;
        t5.printStandard();
        cout<<endl;
        system("pause");
    
        
    
    }

    Any Question, feel free to ask!

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,262
    O_o

    Well, there is no timer in that code so the label is misleading.

    The design is also not "OOP".

    You have certainly hit "Oh! My! God! OOPS!" for critical damage: you just taken two exceedingly simple functions, put them in a class, and moved the parameters to the constructor for absolutely no reason.

    I don't really have a question. ;_;

    Soma

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,629
    I have three questions for you:
    - Why are you using void main?
    - Why are you not following the recommended advice in this article?
    - Why are you not rejecting incorrect input (eg inputting hours > 24 sets it to 0)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timer
    By GhH in forum C Programming
    Replies: 5
    Last Post: 09-19-2011, 04:50 AM
  2. difference between this->method() and method()
    By nacho4d in forum C++ Programming
    Replies: 7
    Last Post: 11-21-2009, 03:11 PM
  3. Encrypt method (from decrypt method)
    By mmmmmm in forum C# Programming
    Replies: 3
    Last Post: 09-19-2009, 10:35 AM
  4. calling a class method within different class method
    By alyeska in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2009, 10:56 AM
  5. timer in C
    By nightking in forum C Programming
    Replies: 3
    Last Post: 01-31-2009, 04:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21