Question

Write a C++ class named CashRegister. The class should have private data members named Hundreds, Tens,...

Write a C++ class named CashRegister. The class should have private data members named
Hundreds, Tens, Fives, and Singles all of type integer. The class should have the following:
1. A member function named GetTotalCash to retrieve the total cash inside the cash
register.
2. Overload the + operator to allow adding two objects of type CashRegister (note, add the
hundreds to the hundreds, tens to tens and so on).
3. Overload the > operator to compare between two CashRegisters objects based on their
totalcash.
4. Overload the += operator.

Thank you

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

Please find the code below:

#include <iostream>
using namespace std;

class CashRegister{
private:
   int hundreds;
   int tens;
   int fives;
   int singles;
public:
   CashRegister(){

   }
   CashRegister(int h,int t,int f,int s){
       hundreds = h;
       tens = t;
       fives =f;
       singles = s;
   }

   int GetTotalCash (){
       return hundreds * 100+tens*10+fives*5+singles;
   }

   CashRegister operator + (CashRegister const &obj) {
       CashRegister res;
       res.hundreds = hundreds + obj.hundreds;
       res.tens = tens + obj.tens;
       res.fives = fives + obj.fives;
       res.singles = singles + obj.singles;
       return res;
   }


   bool operator > (CashRegister &obj) {
       return GetTotalCash()>obj.GetTotalCash();
   }


   void operator += (CashRegister const &obj) {
       hundreds = hundreds + obj.hundreds;
       tens = tens + obj.tens;
       fives = fives + obj.fives;
       singles = singles + obj.singles;
   }


   int getFives() const
   {
       return fives;
   }

   int getHundreds() const
   {
       return hundreds;
   }

   int getSingles() const
   {
       return singles;
   }

   int getTens() const
   {
       return tens;
   }

   void setFives(int fives)
   {
       this->fives = fives;
   }

   void setHundreds(int hundreds)
   {
       this->hundreds = hundreds;
   }

   void setSingles(int singles)
   {
       this->singles = singles;
   }

   void setTens(int tens)
   {
       this->tens = tens;
   }

};

int main(){

   CashRegister r(1,2,3,4);
   cout<<"First object cash value is : "<<r.GetTotalCash()<<endl;
   CashRegister r2(4,3,2,1);
   cout<<"Second object cash value is : "<<r2.GetTotalCash()<<endl;

   CashRegister r3 = r+r2;
   cout<<"Third object cash value is : "<<r3.GetTotalCash()<<endl;

   bool res = r>r2;
   cout<<"First > Second ? "<<res<<endl;

   r +=r2;
   cout<<"First object cash value is : "<<r.GetTotalCash()<<endl;

   return 0;
}

output:

Add a comment
Know the answer?
Add Answer to:
Write a C++ class named CashRegister. The class should have private data members named Hundreds, Tens,...
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
  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • In C++ Write a header cashRegister.h and source cashRegister.cpp files for the a CashRegister class. The...

    In C++ Write a header cashRegister.h and source cashRegister.cpp files for the a CashRegister class. The class a CashRegister class has the following data members: 1) an array of 100 Item objects. 2) Cash Register Name and 3) Count of Item objects purchased. 4) Item Cash Total 5) State Tax Rate. The Item is represented as a class in an item.h file. The Item class has the following data members: 1) Name of the item 2) Cost of the item....

  • (Classes and Objects) Design a class named Box. The class should have the following private members:...

    (Classes and Objects) Design a class named Box. The class should have the following private members: width - A double member variablethat holds the width of the box. length – A double member variable for holding the length of the box. height – A double member variable of type double that holds the height of the box. In addition, provide the following member functions with full (inline) implementation. a) A default constructor that sets all member variables to 0. b)...

  • Construct a class named Fractions containing two integer data members named num and denom, used to...

    Construct a class named Fractions containing two integer data members named num and denom, used to store the numerator and denominator of a fraction having the form num/denom. Your class should include a default constructor that initializes num and denom to 1 if there's no user initialization, and it must prohibit a 0 denominator value. In addition, create member functions for displaying an object's data values and overloaded operator functions for adding, subtracting, multiplying, and dividing two Fraction objects, as...

  • Design a class named NumDays, to store a value that represents a number of hours and...

    Design a class named NumDays, to store a value that represents a number of hours and convert it to a number of days. For example, 24 hours would be converted to 1 day, 36 hours would be converted to 1.5 days, and 54 hours would be converted to 2.25 days. This class has two private member variables with data type double: one is named as hours; another is named as days. This class has a constructor that accepts a value...

  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

  • 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...

  • Write a class called Book that has two private member variables called page (integer) and topic...

    Write a class called Book that has two private member variables called page (integer) and topic (string). It also has a static private member variable called count (integer). This class has only one constructor with default argument for page and topic. Write this constructor. Also overload the addition operator for this class such that it will add an integer value to the page variable of the book. For example when in main we say: book2 = book1 + 4; then...

  • 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. Member functions: Overload arithmetic operators (+ and -), and return new Money objects; Non-member functions: Overload comparison operators (<, >, !=, and ==), and return boolean values; Write a main function to test your code.

  • Overloading Design a UML diagram for a TimeClock class with the following private data members: •...

    Overloading Design a UML diagram for a TimeClock class with the following private data members: • float days • float hours The class should also have the following public member methods: • Constructor with a default that sets the data values to 0 • getDays to return the private data member days • getHours to return the private data member hours • setDays to set the private data member days • setHours to set the private data member hours •...

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