Question

In: Computer Science

Using C++ programming. Thank you. Money class has the following member variables and functions. -member variable...

Using C++ programming. Thank you.

Money class has the following member variables and functions.
-member variable : dollar, cent
-member function : setMoney(int dollar, int cent), printMoney(), operator overloading(+)

Program the Money class so that the following function can be performed.

int main(){
Money a, b, c;
A.setMoney(10, 60);
B.setMoney(20, 70;
(a+b).printMoney();
c = a + b;
c.printMoney();
}

Solutions

Expert Solution

code in c++

(code to copy)

#include <bits/stdc++.h>
using namespace std;

class Money
{
public:
        int doller;
        int cent;
        //member function to set money
        void setMoney(int doller, int cent)
        {
                // initialize member variables
                this->doller = doller;
                this->cent = cent;
        }
        //member function to print money
        void printMoney()
        {
                cout << doller << " doller(s) " << cent << " cent(s)" << endl;
        }
        //operator overloading
        Money operator+(Money const &obj)
        {
                //create result object
                Money result;
                int total_dollers = doller + obj.doller;
                int total_cents = cent + obj.cent;
                //adjust overflow in cents
                total_dollers += total_cents / 100;
                total_cents %= 100;
                //set values in result
                result.setMoney(total_dollers, total_cents);
                return result;
        }
};

int main()
{
        Money a, b, c;
        a.setMoney(10, 60);
        b.setMoney(20, 70);
        (a + b).printMoney();
        c = a + b;
        c.printMoney();
}

code screenshot

code output screenshot


Related Solutions

java programming Create a class named Money. It should have member variables for Member Variables Store...
java programming Create a class named Money. It should have member variables for Member Variables Store dollars and cents as members (both should be int). They should be accessible from only inside the class. Methods  Write a default constructor that sets members to 0.  Write a two-parameter constructor that sets members to the parameter values.  Write get/set methods for the member variables.  Write an override method for toString. The returned string should be formatted as a...
In C++ please Your class could have the following member functions and member variables. However, it's...
In C++ please Your class could have the following member functions and member variables. However, it's up to you to design the class however you like. The following is a suggestion, you can add more member variables and functions or remove any as you like, except shuffle() and printDeck(): (Note: operators must be implemented) bool empty(); //returns true if deck has no cards int cardIndex; //marks the index of the next card in the deck Card deck[52];// this is your...
//JAVA //basic please for learning programming 2. Thank you. Write the Class Variables Class Box is...
//JAVA //basic please for learning programming 2. Thank you. Write the Class Variables Class Box is to have the following private data members: height of type double width of type double length of type double Write the following Overridden Method Class Box is to have an overridden equals() method that does the following:​​​​ tests to see if the parameter represents an object (null test) tests to see if the parameter object is of the same class type as the calling...
In C++ Programming Language: 1a. Declare a class, namely ReverseUniverse, that contains three public member functions....
In C++ Programming Language: 1a. Declare a class, namely ReverseUniverse, that contains three public member functions. Please see Q2 and Q3 for the name of the second and third function. (5 points) Write a function string reverseString that reverses a string (first member function). It takes a string parameter and returns its reversed version. (10 points) 1b. In the class ReverseUniverse in Q1. Write a function vector reverseVector that reverses a vector (second member function). It takes a double vector...
Using C++ Design a class named PersonData with the following member variables: lastName firstName address city...
Using C++ Design a class named PersonData with the following member variables: lastName firstName address city state zip phone and at a minimum the following member functions: A class constructor - which initializes all the member variables above a display() function - which ONLY displays all the person's data to the console Also write the appropriate accessor/mutator functions for the member variables Write a NewPersonData function. This is a stand-alone function that you can use in your main to generates...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Car Class Write a class named Car that has the following member variables: • year. An...
Car Class Write a class named Car that has the following member variables: • year. An int that holds the car’s model year. • make. A string object that holds the make of the car. • speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. • Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables....
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function that returns r value of property variable -Put(int d): A function that stores d in attribute variable r *Declare a one-dimensional array of type Circle and in each array element Read and store integers from standard input device *Declare the swap() function to swap two elements with each other *Write a program that sorts the elements of a one-dimensional array of circle type in...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and complexNum of type double. Your class shall have following member functions Complex(); Complex(double, double); Complex add(Complex num1, Complex num2); Complex subtract(Complex num1, Complex num2); string printComplexNumber(); Write a driver program to test your code. Please make sure your code is separated into Complex.h Containing definition of the class Complex.cpp Containing the implementation ComplexTestProgram.cpp Containing main program Use the following directive in class definition to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT