Hilfe bei einer Funktion



  • Also ich hab hier mein erstes Programm mit Klassen geschrieben jetzt hab ich nur noch das Problem , dass ich einen Uhrenvergleich mittels Clock::operator== integrieren muss das war ja bei < und > noch leicht aber bei == weiß ich nit weiter kann mir da bitte wer helfen???

    Mein Programm:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    class Clock {
    
    public:
      // Konstruktoren
      Clock() : h(0), m(0), s(0) {}
      Clock(const Clock& c) 
        : h(c.h), m(c.m), s(c.s) {}
      Clock(int hour, int min, int sec)
        : h(hour), m(min), s(sec) {}
      // Destruktor ( nichts zu tun ... )
      ~Clock() {}
    
      //  Zeitvergleich
      bool operator< (const Clock& c) const;
      bool operator> (const Clock& c) const;
      bool operator== (const Clock& c) const;
    
      //  stelle Zeit ein
      void set(int hour, int min, int sec);
      //  gib Zeit aus
      void print() const
        { cout << h << ":" << m << ":" << s << endl; }
    
      friend ostream& operator<< (ostream&, const Clock&);
    
    private:  
    
      int h,m,s;
    
    };
    
    //  Initialisierung der Uhr
    void Clock::set(int hour, int min, int sec)
    {
       h=hour; m=min; s=sec;    
    }
    
    //  Uhrenvergleich <
    bool Clock::operator< (const Clock& c) const
    {
       if (h!=c.h)
         return h<c.h;
       else if (m!=c.m)
         return m<c.m;
       else 
         return s<c.s;
    }
    
    //  Uhrenvergleich >
    bool Clock::operator> (const Clock& c) const
    {
       if (h!=c.h)
         return h>c.h;
       else if (m!=c.m)
         return m>c.m;
       else 
         return s>c.s;
    }
    
    //  Ausgabe
    ostream& operator<< (ostream& os, const Clock& c)
    {
      return os << c.h << ":" << c.m << ":" << c.s;
    }
    
    int main()
    {
      //  Uhrenbeispiele
      Clock c1,c2,c3(12,36,0),c4(c3);
    
      c1.set(12,3,0);
      c2.set(15,32,0);
    
      //  Zeitausgabe
      c1.print();
      c2.print();
      c3.print();
      c4.print();
    
      cout << "c1 ... " << c1 << endl;
      cout << "c2 ... " << c2 << endl;
      cout << "c3 ... " << c3 << endl;
      cout << "c4 ... " << c4 << endl;
    
      cout << endl << "Press ENTER to continue..." << endl;   //  **
      cin.get();                                              //  **    
    }
    


  • bool Clock::operator==(const Clock& c) const
    {
        return h==c.h && m==c.m && s==c.s;
    }
    


  • Wobei ich hier eher zu globalen Operatoren (ausserhalb der Klasse) greifen würde:

    bool operator==(const Clock& Left, const Clock& Right);
    

Anmelden zum Antworten