Concept explainers
Notice the member function output in the class definition of Money given in Display 11.3. In order to write a value of type Money to the screen, you call output with cout as an argument. For example, if purse is an object of type Money, then to output the amount of money in purse to the screen, you write the following in your program:
purse.output(cout);
It might be nicer not to have to list the stream cout when you send output to the screen.
Rewrite the class definition for the type Money given in Display 11.3. The only change is that this rewritten version overloads the function name output so that there are two versions of output. One version is just like the one shown in Display 11.3; the other version of output takes no arguments and sends its output to the screen. With this rewritten version of the type Money, the following two calls are equivalent:
purse.output(cout);
and
purse.output();
but the second is simpler. Note that since there will be two versions of the function output, you can still send output to a file. If outs is an output file stream that is connected to a file, then the following will output the money in the object purse to the file connected to outs:
purse.output(outs);
Want to see the full answer?
Check out a sample textbook solutionChapter 11 Solutions
Problem Solving with C++ plus MyProgrammingLab with Pearson eText-- Access Card Package (9th Edition)
Additional Engineering Textbook Solutions
Database Concepts (8th Edition)
C Programming Language
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Absolute Java (6th Edition)
Starting Out with Python (3rd Edition)
- Put the client program in a separate file from the class, and divide the class into specification file (fraction.h) and implementation file (fraction.cpp), so your code will be in 3 separate files. Fraction.h #include <iostream>using namespace std; int gcd(int a, int b) { if (a == 0) return b; if (b == 0) return a; if (a == b) return a; if (a > b) return gcd(a - b, b); return gcd(a, b - a);} class Fraction {private: // numerator and denominator int n, d; public: // simplifies the fraction void simplify() { int g = gcd(n, d); n /= g; d /= g; } // default constructor Fraction() { n = 0, d = 1; } // parameterised constructor Fraction(int a, int b) { n = a, d = b; simplify(); } // prints the fration as n/d void print() const { cout << n << "/" << d; } // multiplies 2 fractions and returns it Fraction multipliedBy(Fraction f) const { f.simplify(); Fraction r; r.n = n * f.n; r.d = d * f.d; r.simplify(); return r; } // divides two fraction and…arrow_forwardWrite a code in c++ to calculate area of triangle (A=1/2 ???ℎ? ∗ ????ℎ). Take objects of class and pass static value to set parameter. By overloading binary operator “_”to subtract the values of two objects. At the end calculate area of triangle by using these value and display itarrow_forwardWrite a class called Dog in C++. Dogs have attributes (such as number of legs and eye color), and they can run and bark and wag tails. Dogs can also optionally have a bone, and you'll have to create a Bone class for this. At a minimum, Dogs should have the following attributes: numberOfLegs (integer, must be between 0 and 4). Default to 4 (can be changed with a setter as shown below) eyeColor (a string that could be of any value) furColor (a string that could be of any value) weight (a float, must be between 1 and 200) isHappy (a boolean, defaulting to true) name (a string that could be of any value) Dogs should have a constructor and a destructor: The constructor should take one argument: a string representing the name. It should print a message noting that the dog as been created (e.g "_name_ is alive!") The destructor should print the dog's name and a message (e.g. "_name_ is no longer with us")arrow_forward
- The class Real is a number type with a whole and a fraction part; both are ints. Write a conversion constructor that creates a Real number from an integer. Given: (from .h file) class Real { public: Real(); // 0.0 Real(int whole); Real(int whole, int fract); private: int m_whole; int m_fraction; }; *****Please help implement the three member functions; I think I did the first one correct but please correct me if I'm wrong, and please write comments to explain. I appreciate it, thanks!arrow_forwardPrevious Exercise to help with answering the question which is below | Write the header file (.h file) of a class Counter containing: A data member counter of type int. A data member named counterID of type int. A static int data member named nCounters. A constructor that takes an int argument. A function called increment that accepts no parameters and returns no value. A function called decrement that accepts no parameters and returns no value. A function called getValue that accepts no parameters and returns an int. A function named getCounterID that accepts no parameters and returns an int. #include<iostream>using namespace std; class Counter{private:int counter;int counterID;static int nCounters;public:Counter(int c);void increment();void decrement();int getValue();int getCounterID();}; ----------------------------------------------------------------------- ANSWER THIS QUESTION!In C++ Write the implementation (.cpp file) of the Counter class of the previous exercise.The…arrow_forwardWrite a program to swap the values of private data members of a class named as class_1 and class_2 by using the concept of friend classes.arrow_forward
- Write a java program that contains variables to hold employee data like; employeeCode, employeeName and date Of Joining. Write a function that assigns the user defined values to these variables. Write another function that asks the user to enter current date and then checks if the employee tenure is more than three years or not. Call the functions in main. Now write a runner class that declares two employee objects and check their tenure periods.arrow_forwardConsider the code of both classes A.cpp and B.cpp listed below. Read carefully the code of class A and Class B. 1- Replace ? with public. Would the code of class B compile? Explain why or why not the code of B will or will not compile (10 points). Provide me with the output as a comment at the end of the code. Submit both codes as LastName_FirstName_A.cpp and LastNameFirstName_B.cpp. 2- If we replace ? in class A with protected, would the code of class B compile? Explain why or why not the code of B will or will not compile (10 points). Comment out the line in code B that is causing the error. Compile and verify that the code works. Provide me with the output of B.Submit both codes as:LastName_FirstName_A_Proctected.cpp LastNameFirstName_B_Proctected.cpp. 3- If we replace ? in class A with private, would the code of class B compile? Explain why or why not the code of B will or will not compile (10 points). Submit both codes as: LastName_FirstName_A_Private.cpp…arrow_forwardSuppose you have a Person class with the following attributes: Member Variables: int NID, double income, int age, String name Methods: public Person (int NID, double income, int age, String name) { Assign the variables ;} You will read the information about the persons from a text file named "input.txt". In the first line of the file, there will be a number n which will denote the total number of people. Then, you will read a total of n lines from the file, each line containing a person’s information (NID, income, age, name), create a Person object. Then you will store the object in an ArrayList. Print each person's information sorted by the descending order of income in an “output.txt” file. If you find an equal salary then place the person with a smaller name length first. (See the Output below) You have to sort the ArrayList using the Comparator class. Sample input.txt file: 3 1011 10000 34 Rahim 7234 30000 33 Karim 922 10000 30 Ron Sample output.txt file: 7234 30000 33 Karim…arrow_forward
- Write a program to make a class of English Distance with data members feet and inches, and member function getdata(), showdata(), adddata() and operator+() to get data for two distances, to display data for two distances, to add the data of two distances and to operator overload function to overload the data of class’ object.arrow_forwardSuppose that you have the Object class Employee which is an object that contains Name, Surname and Payment with the respective set and get functions. You will create following functions on 'EmpRW' class, and create a main java class to call them. a. Write a java function that writes on a file named 'emp.txt' by using Formatter Class, this Employee Object. b. Write another java function that reads from the file 'emp.txt' and stores the data as an array of Instances of Employee object. C. Write a function that prints out the name, surname and maximum salary from the Employee list taken from the file. Now write the main java class that takes data by using Input Dialogs, and that uses all of above functions of 'EmpR W' class. e. Write the same functions in a and b by assuming the Employee Object class is serializable, on 'EmpSerR W' class. The name of the file to read and write should be 'emp.binarrow_forwardGiven the class Pet below, write a client program to read data for five pets and display the following data: name of smallest pet, name of largest pet, and average weight of the five pets. Make sure you explain and justify every line of your code using internal documentation to get full mark. /**Class for basic pet data: name, age, and weight. */ public class Pet {private String name;private int age; //in years private double weight;//in kg public Pet(String initialName, int initialAge, double initialWeight) {name = initialName;if ((initialAge < 0) || (initialWeight < 0)) { System.out.println("Error: Negative age or weight."); System.exit(0); } else {age = initialAge; weight = initialWeight; } } public void setPet(String newName, int newAge, double newWeight) { name = newName;if ((newAge < 0) || (newWeight < 0)) {System.out.println("Error: Negative age or weight."); System.exit(0); } else {age = newAge; weight = newWeight; } } public Pet(String initialName) {name =…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning