Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
[email protected] web/email now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!

Paste

Pasted as C++ by registered user raphael ( 1 year ago )
#include <iostream>
#include "heure.h"
using namespace std;

int main()
{
    Heure * h1 = new Heure(2, 30, 30);
    Heure * h2 = new Heure(1, 12, 58);

    Heure *h3 = new Heure(0,0,0);
    *h3 = *h1 + *h2;
    cout<<"\nObjet h3 :"<<endl;
    h3->print();

    *h3= *h3 + *h1;     //Methode membre operator+
    cout<<"\nObjet h3 apres addition :"<<endl;
    h3->print();

    Heure h4 = operator+(*h1, *h2); //Methode friend operator+
    cout<<"\nObjet h4 :"<<endl;
    h4.print();
    return 0;
}

//-------------------------------------------------------------------------------------------------------
#ifndef HEURE_H
#define HEURE_H

#include <iostream>
using namespace std;

class Heure
{
public:
    Heure(int h,int m,int s);
    int heure;
    int minute;
    int seconde;
    Heure operator+(const Heure& source);
    friend Heure operator+(const Heure& a, const Heure& b);
    void print();
};

#endif // HEURE_H
//-------------------------------------------------------------------------------------------------------
#include "heure.h"
#include <iostream>
using namespace std;
Heure::Heure(int h, int m, int s)
{
    this->heure=h;
    this->minute=m;
    this->seconde=s;
}

Heure Heure::operator +(const Heure& source){
    int h = this->heure + source.heure;
    int m = this->minute + source.minute;
    int s = this->seconde + source.seconde;

    if (s >= 60) {
        s -= 60;
        m++;
    }
    if (m >= 60) {
        m -= 60;
        h++;
    }

    return Heure(h, m, s);
}

void Heure::print(){
    cout<<"heure : "<<this->heure<<"\nminute : "<<this->minute<<"\nseconde : "<<this->seconde<<endl;
}

Heure operator+(const Heure& a, const Heure& b){
    int h = b.heure + a.heure;
    int m = b.minute + a.minute;
    int s = b.seconde + a.seconde;

    if (s >= 60) {
        s -= 60;
        m++;
    }
    if (m >= 60) {
        m -= 60;
        h++;
    }

    return Heure(h, m, s);
}

 

Revise this Paste

Parent: 126185
Your Name: Code Language: