SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Friends Overloaded Operators And Arrays In Classes
Exercise:
Self-test Exercises
Question:18 | ISBN:9780321531346 | Edition: 7

Question

Following is the definition for a class called Percent. Objects of type

Percent represent percentages such as 10% or 99%. Give the definitions

of the overloaded operators >> and << so that they can be used for input and output with objects of the class Percent. Assume that input always consists of an integer followed by the character ‘%’, such as 25%. All percentages are whole numbers and are stored in the int member variable named value. You do not need to define the other overloaded operators and do not need to define the constructor. You only have to define the overloaded operators >> and <<.

#include <iostream>

using namespace std;

class Percent

{

public:

friend bool operator ==(const Percent& first,

const Percent& second);

friend bool operator <(const Percent& first,

const Percent& second);

Percent( );

Percent(int percent_value );

friend istream& operator >>(istream& ins,

Percent& the_object);

//Percent.

//Precondition: If ins is a file input stream, then ins

//has already been connected to a file.

friend ostream& operator <<(ostream& outs,

const Percent& a_percent);

//Overloads the << operator for output values of type

//Percent.

//Precondition: If outs is a file output stream, then

//outs has already been connected to a file.

private:

int value;

};

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.