Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class Money{
- public:
- Money(); // default
- Money(unsigned, unsigned); //конструктор с параметри
- ~Money(); //деструктор
- Money(const Money&); // copy-конструктор
- Money& operator=(const Money&); //оператор за присвояване
- //КРАЙ НА ГОЛЯМАТА 5ОРКА
- void setLevas(unsigned);
- void setStots(unsigned);
- unsigned getLevas() const; //Туй са аксесори(не променям нищо с тях), затова има const след тях
- unsigned getStots() const;
- //Край на гетъри/сетъри
- std::ostream& ins(std::ostream&) const; // a tuk sa 2 parametyra - implicitniya i ostream
- //virtual std::ostream ins(std::ostream&) const;//tva e za po sledvashtite koga ima nasledyavane
- Money operator+(const Money&) const;
- Money operator-(const Money&) const;
- Money operator*(const Money&) const;
- Money operator/(const Money&) const;
- bool operator<(const Money&) const;
- bool operator>(const Money&) const;
- bool operator==(const Money&) const;
- private:
- unsigned levas;
- unsigned stotinkis;
- };
- Money::Money(){
- levas = 0;
- stotinkis = 0;
- }
- Money::Money(unsigned levas, unsigned stotinkis){ //Мога да пропусна this ako imenata тук и на долния ред са различни примерно uns leavass != levas
- this->levas = levas;
- this->stotinkis = stotinkis;
- }
- Money::~Money(){}
- Money::Money(const Money& obj){
- levas = obj.levas;
- stotinkis = obj.stotinkis;
- }
- Money& Money::operator=(const Money& rhs){
- if (this != &rhs){
- levas = rhs.levas;
- stotinkis = rhs.stotinkis;
- }
- return *this;
- }
- void Money::setLevas(unsigned l){
- levas = l;
- }
- void Money::setStots(unsigned s){
- stotinkis = s;
- }
- unsigned Money::getLevas() const{
- return levas;
- }
- unsigned Money::getStots() const{
- return stotinkis;
- }
- std::ostream& Money::ins(std::ostream& lhs) const{
- lhs << levas << "." << stotinkis;
- return lhs;
- }
- std::ostream& operator<<(std::ostream& lhs, const Money& rhs){
- return rhs.ins(lhs);
- }
- Money Money::operator+(const Money& rhs) const{
- return Money(levas + rhs.levas, stotinkis + rhs.stotinkis);
- }
- Money Money::operator-(const Money& rhs) const{
- return Money(levas - rhs.levas, stotinkis - rhs.stotinkis);
- }
- Money Money::operator*(const Money& rhs) const{
- return Money(levas * rhs.levas, stotinkis * rhs.stotinkis);
- }
- Money Money::operator/(const Money& rhs) const{
- return Money(levas / rhs.levas, stotinkis / rhs.stotinkis);
- }
- bool Money::operator>(const Money& rhs) const{
- unsigned temp1 = levas*100 * stotinkis;
- unsigned temp2 = rhs.levas*100 + rhs.stotinkis;
- return temp1 > temp2;
- }
- bool Money::operator<(const Money& rhs) const{
- unsigned temp1 = levas*100 * stotinkis;
- unsigned temp2 = rhs.levas*100 + rhs.stotinkis;
- return temp1 < temp2;
- }
- bool Money::operator==(const Money& rhs) const{
- return Money(levas, stotinkis) == Money(rhs.levas, rhs.stotinkis);
- }
- std::ostream& operator<<(std::ostream&, const Money&); //tova e s 3 parametyra imam 1 impliciten koito ne go vijdam
- int main(int argc, char **argv)
- {
- Money m(5, 40);
- Money n(3, 90);
- std::cout << m + n << std::endl;
- std::cout << n << std::endl;
- std::cout << m << std::endl;
- (m > n) ? std::cout << true : std::cout << false << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement