1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ program! classes

Discussion in 'C++' started by andreacmt, Oct 24, 2010.

  1. andreacmt

    andreacmt New Member

    Joined:
    Oct 24, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi!! Please I need on a program in C++. Well I have to complete the following program. The objective is that the program asks the user a number and the user writes 10 for example but the user must say if he wants to add this number to the hours, minutes or seconds. Another trouble is that if the user writes -10 the program must not add but subtract. Finally if we have set 9:30:56 and the user enters 10 the program must add 4 seconds to the seconds and the rest to the minutes, just as real life. Please help me!!!!

    The program is right here!!
    Code:
    #include <iomanip>
    #include <iostream>
    using namespace std;
    class Hora
    {
    private:
         int horas; // 0 - 23 (formato 24-horas )
         int minutos; // 0 - 59
      int segundos; // 0 -59
    public:
     Hora(); // Constructor
     void setHoras( int h); //Fija Horas
     void setMinutos( int m); //Fija Minutos
     void setSegundos( int s); //Fija Segundos
     int  getHoras(); // Retorna las horas
     int  getMinutos(); // Retorna los minutos
     int  getSegundos(); // Retorna los segundos
     int  compara(Hora h); //Se compara con h
     void mostrar(); // Imprime la hora.
    };
    // Implementacion de la clase hora
    Hora::Hora()
    { setHoras(0); setMinutos(0); setSegundos(0);
    }
    void Hora::mostrar() // Imprime la hora.
    {    cout << setfill( '0' ) << setw( 2 ) << horas << ":";
      cout << setfill( '0' ) << setw( 2 ) << minutos << ":";
      cout << setfill( '0' ) << setw( 2 ) << segundos;
    }
    void Hora::setHoras(int h) // Fija Hora 
    { horas = ( h >= 0 && h < 24 ) ? h : 0; // valida la hora
    }
    void Hora::setMinutos(int m) // Fija  Minuto
    { minutos = ( m >= 0 && m < 60 ) ? m : 0; // valida minutos
    }
    void Hora::setSegundos( int s) // Fija Segundos
    { segundos = ( s >= 0 && s < 60 ) ? s : 0; // valida segundos
    }
    int  Hora::getHoras() // Retorna las horas
    { return horas;
    }
    int  Hora::getMinutos() // Retorna los minutos
    { return minutos;
    }
    int  Hora::getSegundos() // Retorna los segundos
    { return segundos;
    }
    int  Hora::compara(Hora h) //Se compara con h
    { if (horas > h.getHoras()) return 1;
     if (horas < h.getHoras()) return -1;
     if (minutos > h.getMinutos()) return 1;
     if (minutos < h.getMinutos()) return -1;
     if (segundos > h.getSegundos()) return 1;
     if (segundos < h.getSegundos()) return -1;
     return 0;
    }
    int main()
    {
     Hora x, y, a[100];
     
     x.setHoras(12); x.setMinutos(35); x.setSegundos(40);
     x.mostrar();
     cout << endl;
     y.setHoras(12); y.setMinutos(35); y.setSegundos(42);
     y.mostrar(); 
     cout << endl;
     if (x.compara(y)>0)
      cout << "mayor" << endl;
     if (x.compara(y)==0)
      cout << "igual" << endl;
     if (x.compara(y)<0)
      cout << "menor" << endl;
     cout << endl;
     system ("pause");
     return 0;
    }
     
    Last edited by a moderator: Oct 24, 2010
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    What frequently happens when writing programs to handle dates and times is that sooner or later you need a conversion to a serial number that uniquely represents a date, and that assumes zero=some specific date, e.g. 1st Jan 1970. And of course a conversion back again. This makes your program very easy: convert the start date/time to a serial number, add the number entered by the user, then convert it back again. It also makes the next project very easy, which will probably involve comparing dates, you just convert the dates to be compared to serial numbers, then compare those integers.
     

Share This Page