Question

this is a c++ program Define a class Money with two integer data members, dollars and...

this is a c++ program

Define a class Money with two integer data members, dollars and cents.

  1. Member functions: Overload arithmetic operators (+ and -), and return new Money objects;
  2. Non-member functions:
  • Overload comparison operators (<, >, !=, and ==), and return boolean values;

Write a main function to test your code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Program:

#include<iostream>
using namespace std;

class Money {
public:
   int dollars, cents;
public:
   Money(int d, int c) {
   dollars = d;
   cents = c;
   }
  
   Money operator + (Money const &obj) {
       Money res(0,0);
       res.dollars = dollars + obj.dollars;
       res.cents = cents + obj.cents;
       if(res.cents>=100){
       res.dollars++;
       res.cents-=100;
       }
       return res;
   }
   Money operator - (Money const &obj) {
       Money res(0,0);
       if(cents<obj.cents){
       res.dollars = dollars - obj.dollars - 1;
       res.cents = cents - obj.cents + 100;
       }
       else{
       res.dollars = dollars - obj.dollars;
       res.cents = cents - obj.cents;
       }
       return res;
   }
   void print() {
   cout << "$"<<dollars << "." << cents << endl;
   }
};

bool operator <(Money const &obj1,Money const &obj2){
   if(obj1.dollars<obj2.dollars || (obj1.dollars==obj2.dollars && obj1.cents<obj2.cents))
   return true;
   else
   return false;
}

bool operator >(Money const &obj1,Money const &obj2){
   if(obj1.dollars>obj2.dollars || (obj1.dollars==obj2.dollars && obj1.cents>obj2.cents))
   return true;
   else
   return false;
}

bool operator !=(Money const &obj1,Money const &obj2){
if(obj1.dollars==obj2.dollars && obj1.cents==obj2.cents)
return false;
else
return true;
}

bool operator ==(Money const &obj1,Money const &obj2){
if(obj1.dollars==obj2.dollars && obj1.cents==obj2.cents)
return true;
else
return false;
}

int main()
{
Money m1(34,50),m2(10,50);
cout<<"Money first object(m1): ";
m1.print();
cout<<"Money second object(m2): ";
m2.print();
Money m3 = m1 + m2;
cout<<"Addition money object(m1+m2)(m3): ";
m3.print();
Money m4 = m3 - m2;
cout<<"Subtraction money object(m3-m2)(m4): ";
m4.print();
cout<<"Is m4<m2?: ";
if(m4<m2)
cout<<"True\n";
else
cout<<"False\n";
cout<<"Is m1>m2?: ";
if(m1>m2)
cout<<"True\n";
else
cout<<"False\n";
cout<<"Is m1!=m2?: ";
if(m1!=m2)
cout<<"True\n";
else
cout<<"False\n";
cout<<"Is m1==m1?: ";
if(m1==m1)
cout<<"True\n";
else
cout<<"False\n";
return 0;
}

Output:

Add a comment
Know the answer?
Add Answer to:
this is a c++ program Define a class Money with two integer data members, dollars and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

  • Overview This checkpoint is intended to help you practice the syntax of operator overloading with member...

    Overview This checkpoint is intended to help you practice the syntax of operator overloading with member functions in C++. You will work with the same money class that you did in Checkpoint A, implementing a few more operators. Instructions Begin with a working (but simplistic) implementation of a Money class that contains integer values for dollars and cents. Here is my code: /********************* * File: check12b.cpp *********************/ #include using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test...

  • Programming in C# Create a Money class that has as data members dollars and cents. Include...

    Programming in C# Create a Money class that has as data members dollars and cents. Include IncrementMoney and DecrementMoney instance methods. Include constructors that enable the Money class to be instantiated with a single value representing the full dollar/cent amount as well as a constructor that enables you to create an instance of the class by sending two separate integer values representing the dollar and cent amounts. Include an instance method that returns as a string the number of dollars,...

  • C++ Program 2 Consider a class Employee with data members: age (an integer), id (an integer)...

    C++ Program 2 Consider a class Employee with data members: age (an integer), id (an integer) and salary (a float), and their corresponding member functions as follows: class Employee { private: int age; int id; float salary: public: Employee; // default constructor: age=0, id=0, and salary=0 void setAge(int x); // let age = x void setId(int x); // let id = x void setSalary(float x); // salary = x int getAge(); // return age int getId; // return id float...

  • C++ program Create a class Time having data members HH, MM, SS of type integer. Write...

    C++ program Create a class Time having data members HH, MM, SS of type integer. Write a C++ program to do the following: 1. Use a Constructor to initialize HH, MM, SS to 0. 2. Create two Objects of this class and read the values of HH, MM, SS from the user for both objects (Using proper member functions). 3. Use a binary + operator to add these two objects created (Store & display result using separate object). 4. Use...

  • Write the declaration for class Building. The class’s members should be: doors, an integer. This variable...

    Write the declaration for class Building. The class’s members should be: doors, an integer. This variable should not be accessible to code outside the class or to member functions in any class derived from class Building. steps, an integer. This variable should not be accessible to code outside the class, but should be accessible to member functions in any class derived from class Building. setDoors, getDoors, setSteps, and getSteps. These are the set and get functions for the member variables...

  • Create a class named Cash. Include one private integer variable to hold the number of dollars...

    Create a class named Cash. Include one private integer variable to hold the number of dollars and one to hold the number of cents. Create your own accessor and mutator functions to read and set both member variables. Create a function that returns the amount of money as a double. Test all of your functions. Use at least 4 Cash objects. Must be done in c++

  • C++ Define the class HotelRoom. The class has the following private data members: the room number...

    C++ Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [3pts]. The constructors and the set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception...

  • In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...

    In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class should contain the following functions: •...

  • 1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

    1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default value 1) -width, an integer (default value 1) Provide the following member functions: -default constructor -constructor that takes parameters used to initialize the data -get/set function for each attribute -a function perimeter that returns the perimeter of the rectangle -a function area that returns the area of the rectangle b. Write a program that uses the class Rectangle to create two rectangle objects with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT