2018年8月5日 星期日

Operator Overloading as Member Functions

Money.cpp

#include "Money.h"

Money::Money(int dollars, int cents) : dollars{dollars}, cents{cents} {}

Money::Money(int total) : dollars {total/100}, cents{total%100}  {}
 

//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
//----WRITE YOUR METHOD DEFINITIONS BELOW THIS LINE----

bool Money::operator==(const Money &p)const{
    if((this->dollars == p.dollars) && (this->cents == p.cents))return true;
 
    else return false;
}
 
bool Money::operator!=(const Money &p)const{
   if((this->dollars != p.dollars) && (this->cents != p.cents))return true;
 
    else return false;
}

//----WRITE YOUR METHOD DEFINITIONS ABOVE THIS LINE----



==============================================
Money.h

#ifndef _MONEY_MONEY_H
#define _MONEY_MONEY_H


#include <iostream>

class Money {
    int dollars;
    int cents;
public:
    Money(int dollars, int cents);
    Money(int total);

    //----DO NOT MODIFY THE CODE ABOVE THIS LINE----
    //----WRITE YOUR METHOD DECLARATIONS BELOW THIS LINE----
    bool operator==(const Money &a)const;
 
    bool operator!=(const Money &a)const;
 
    //----WRITE YOUR METHOD DECLARATIONS ABOVE THIS LINE----
    //----DO NOT MODIFY THE CODE BELOW THIS LINE----
};


#endif //_MONEY_MONEY_H

沒有留言:

張貼留言