Write in c++ another overloaded operator

Assignment Help C/C++ Programming
Reference no: EM13165833

Write in C++ another overloaded operator to go in the program that has Treasury. Overload the forward slash /  so that in the main program, you can declare sale to be of type Treasury, and commission to be of type Treasury, and commispctage to be of type double, writing

Treasury sale, commission;

                  double commisspctage;

                  commisspctage = 6.25;

cin>> sale;

                  cout<<sale<<" is value of sale.\n";

                  commission = sale/commisspctage;

                  cout<<"The commission is "<<commission<<endl;

 

 

Write in C++ another overloaded operator to go in the program that has Treasury. Overload the forward slash /   so that in the main program, you can declare sale to be of type Treasury, and commission to be of type Treasury, and commispctage to be of type double, writing

Treasury sale, commission;

                  double commisspctage;

                  commisspctage = 6.25;

cin>> sale;

                  cout<<sale<<" is value of sale.\n";

                  commission = sale/commisspctage;

                  cout<<"The commission is "<<commission<<endl;

 

to have it calculate the commission at 6.25 percent of the sale. (Notice that 6.25 is the percent as given in the main program. Within your function code of the overloaded operator you will need to convert that to a decimal fraction. Also in the function code, use static_cast to avoid mode-mixing between int and double

variables. Note that you will be returning something of type Treasury.

And while you're working on the program, please improve the << operator function code so it will handle outputting single-digit cents correctly.

NOTE: Test your forward slash calculation using at least these sets of data for the sale

100 dollars 0 cents

987 dollars 89 cents

23 dollars 99 cents

1359 dollars 99 cents

 

And CHECK the output by hand to make sure it gives you the correct results.



//
#include <iostream>
using namespace std;
class Treasury
{
public:
    Treasury(); // default constructor to initialize to 10 dollars and 0 cents
    Treasury(int wholedollars);    // constructor toinitialize the dollar part only
    Treasury(int dollaramt, int centsamt);   // costructor to initialize both parts
    friend Treasury operator + (const Treasury& amt1, const Treasury& amt2);
    friend ostream& operator <<(ostream& outs, const Treasury& thevalue);
    friend istream& operator >>(istream& ins, Treasury& avalue);
    friend bool operator == (const Treasury& oneside, const Treasury& otherside);
private:
    int dollars, cents;
};
int main()
{
    Treasury billfold(25,50), pocket(3,48), total, check(28,98);
    total = billfold + pocket;
    if(total == check)
        cout<<"The total amount is "<<total<<endl;    // where second << is overloaded
    else
        cout<<"Something went wrong.\n";
    // add an overload of / so that sale/commisspctage calculates the commission where commisspctage of the sale is, say, 6.25 percent   
return 0;
}
Treasury::Treasury() // default constructor to initialize to 10 dollars and 0 cents
{
    dollars = 10;
    cents = 0;
}
Treasury::Treasury(int wholedollars)    // constructor toinitialize the dollar part only
{
    dollars = wholedollars;
}
Treasury::Treasury(int dollaramt, int centsamt)   // costructor to initialize both parts
{
    dollars = dollaramt;
    cents = centsamt;
}
Treasury operator + (const Treasury& amt1, const Treasury& amt2)
{
    int totdollars, totcents;
    Treasury result;
    totdollars = amt1.dollars + amt2.dollars;
    totcents = amt1.cents + amt2.cents;
    if(totcents >= 100)
    {
        totdollars++;
        totcents = totcents % 100;
    }
    result.dollars = totdollars;
    result.cents = totcents;
    return result;
}
ostream& operator <<(ostream& outs, const Treasury& thevalue)
{
    outs<<"$"<<thevalue.dollars<<"."<<thevalue.cents;
    return outs;
}
istream& operator >>(istream& ins, Treasury& avalue)   // note: don't use const here
{
    ins>>avalue.dollars>>avalue.cents;
    return ins;
}
bool operator == (const Treasury& oneside, const Treasury& otherside)
{
    if((oneside.dollars == otherside.dollars) && (oneside.cents == otherside.cents))
        return true;
    else
        return false;
}

 

 

Reference no: EM13165833

Questions Cloud

A fuel economy study was carried out : A Fuel economy study was carried out for five models of cars. each car was driven 100 miles, and then the model of the car and the number of gallons used were placed in a line of the file Mileage.txt. Table 7.22 shows the data for the entries of t..
Make the circles have a thickness of 5 pixels, : Make the circles have a thickness of 5 pixels, except for the circle that didn't open at Sochi; make that one a solid disk.
Script that creates and calls a function : Write a script that creates and calls a function named fnItemTotal that calculates the total amount of an item in the OrderItems table (discount price multiplied by quantity)
Accepts filename to sort and run : Accepts filename to sort and run size as command line arguments. Calls the sort routine. Writes the sorted result to a file named sorted.txt.
Write in c++ another overloaded operator : Write in C++ another overloaded operator to go in the program that has Treasury. Overload the forward slash /  so that in the main program, you can declare sale to be of type Treasury, and commission to be of type Treasury, and commispctage to be of ..
Consider a version of the bounded buffer problem : Consider a version of the bounded buffer problem in which there is two producer processes (P1and P2) and one consumer processes (P3) all sharing the same buffer. Assume that the size of the buffer is n=4, and that we start with a completely empty buf..
What is the infinite union of all context-sensitive language : What is the infinite union of all context-sensitive languages? Decidable languages? What is the infinite intersection of all context-sensitive languages? Decidable languages?
Match the os mechanisms on the left to the hardware support : Match the OS mechanisms on the left to the hardware support specific for each mechanism on the right.
Decision tables and dependency diagrams : Decision Tables and Dependency Diagrams

Reviews

Write a Review

C/C++ Programming Questions & Answers

  To reverse the order of elements on a stack

Write a program in C++ to  reverse the order of elements on a stack S using two additional stacks using one additional stack

  Struct definition to represent the data of a person''s bank

Define a struct definition to represent the data of a person's bank account. There will be one string for the name, and two doubles for balance and interest rate. Declare two variables of this new type in the main function. Modify the values of each ..

  Searching f and removing certain states from the hash table

use the class hashT, ''Hashing: Implementation Using Quadratic Probing,'' which uses quadratic probing to resolve collision, to create a hash table to keep track of each state's information. Use the state's name as the key to determine the hash addre..

  We wish to process survey results

Suppose we wish to process survey results that are stored in a file. This exercise requires twoseparate programs. First, create a program that prompts the user for survey responses and outputseach response to a file

  Write a driver program called testrationalnumber.cpp

Write a driver program called testRationalNumber.cpp to test each of the above functions and overloaded operators in the RationalNumber class.

  Takes a string containing a full name

Write a program that takes a string containing a full name and outputs each part of the name separately with its length. The name should be in the form of first, middle, and last name, separated from each other by a single space. For example, if the ..

  Write a c function to convert gallons-quarts-pints and cups

Write a C function named liquid() that is to accept an integer number and the addresses of the variables gallons, quarts, pints, and cups.

  Write function to accept character array

Write down the C++ function which will accept the character array of at most 30 cells. Your function must return true if string and its reverse are identical;

  Create a text file named grades.txt

Write a program to calculate students' average test scores and their grades. Creat a text file named  grades.txt

  Create if-then statement with single alternative decision

Create the If-Then statement (or a flowchart with a single alternative decision structure) which assigns 20 to  variable y and allots 40 to variable z if variable x is greater that 100.

  Design a nested program

How many levels of nesting are there in this design?

  Find the pairs in any given matrix

write a c program which will find the pairs in any given matrix, whose sum of pairs are 10.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd