Concept explainers
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 percentValue); friend istream& operator >>(istream& ins, Percent& theObject); //Overloads the >> operator to input values of type //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& aPercent); //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; }; |
Want to see the full answer?
Check out a sample textbook solutionChapter 11 Solutions
Problem Solving with C++ (10th Edition)
Additional Engineering Textbook Solutions
Starting Out with Python (4th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Degarmo's Materials And Processes In Manufacturing
Concepts Of Programming Languages
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Assume there is a class named Animal, which overloads the = and + operators. In the following statement, assume cat, tiger, and wildcat are all instances of the Animal class: wildcat = cat + tiger;arrow_forwardb) Write code fragments in the application / driver class that can perform the following tasks: (Assume all relevant methods have been defined in all classes involved) i) Declare an array of object named tp to store various types of theme park where the size of array is entered by the user.arrow_forwardMark the following statements as true or false. The member variables of a class must be of the same type. (1) The member functions of a class must be public. (2) A class can have more than one constructor. (5) A class can have more than one destructor. (5) Both constructors and destructors can have parameters. (5)arrow_forward
- Write a class ‘Box’ which has three members height, width and depth; To set values and use these members, write mutator and accessor functions for these members. (Hint: there would be 3 Accessor and 3 Mutator functions).arrow_forwardIn C++, create a class named rational with two integer variables including numerator and denominator. 1) Write a constructor that creates an objęct of this class. wwm ww 2) Write the set and get methods for each variable, including the numerator and denominator. wmww 3) Write a member function named toRasyonelString () that returns a string in the form "a / b" where a is the numerator and b is the denominator. 4) Write a function named printRational () that prints the rational number as "a / b". 5) Write a main method to test the above functions.arrow_forwardCreate a class called Complex for performing arithmetic with complex numbers. Write a driver program to test your class. Complex numbers have the form realPart + imaginaryPart * i Where √i is Use double variables to represent the private data of the class. Provide a constructor function that enables an object of this Class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide Public member functions for each of the following: 1. Addition of two Complex numbers: The real parts are added together and the imaginary parts are added together. 2. Subtraction of two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand and the imaginary part of the right operand is subtracted from the imaginary part of the left operand. 3. Printing Complex numbers in the form (a, b) where a is the real part and b is the imaginary part.arrow_forward
- Create a class called Bill that a hardware store might use to represent a bill for an item sold at the store. A Bill should include three data members—a part number (type string), a quantity of the item being purchased (type int) and a price per item (type double). Your class should have a constructor that initializes the three data members. Provide a set and a get function for each data member. In addition, provide a member function named getBillAmount that calculates the bill amount (i.e., multiplies the quantity by the price per item), then returns the amount as an int value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0. Write a C++ test program that demonstrates class Bill’s capabilities.arrow_forwardSummary In this lab, you create a derived class from a base class, and then use the derived class in a Python program. The program should create two Motorcycle objects, and then set the Motorcycle’s speed, accelerate the Motorcycle object, and check its sidecar status. Instructions Open the file named Motorcycle.py. Create the Motorcycle class by deriving it from the Vehicle class. Call the parent class __init()__ method inside the Motorcycle class's __init()__ method. In theMotorcycle class, create an attribute named sidecar. Write a public set method to set the value for sidecar. Write a public get method to retrieve the value of sidecar. Write a public accelerate method. This method overrides the accelerate method inherited from the Vehicle class. Change the message in the accelerate method so the following is displayed when the Motorcycle tries to accelerate beyond its maximum speed: "This motorcycle cannot go that fast". Open the file named MyMotorcycleClassProgram.py. In the…arrow_forward#this is a python program #topic: OOP Design a class called Pokemon using a parameterized constructor so that after executing the following line of code the desired result shown in the output box will be printed. First object along with print has been done for you, you also need to create other objects and print accordingly to get the output correctly. [You are not allowed to change the code below] #Write your code for class here team_pika = Pokemon('pikachu', 'charmander', 90, 60, 10) print('=======Team 1=======') print('Pokemon 1:',team_pika.pokemon1_name, team_pika.pokemon1_power) print('Pokemon 2:',team_pika.pokemon2_name, team_pika.pokemon2_power) pika_combined_power = (team_pika.pokemon1_power + team_pika.pokemon2_power) * team_pika.damage_rate print('Combined Power:', pika_combined_power) #Write your code for subtask 2,3,4 here Output: =======Team 1======= Pokemon 1: pikachu 90 Pokemon 2: charmander 60 Combined Power: 1500 =======Team 2======= Pokemon 1:…arrow_forward
- Python program for this project: Patient Charges Write a class named Patient that has attributes for the following data: First name, middle name, and last name Address, city, state, and ZIP code Phone number Name and phone number of emergency contact The Patient class’s _ _init_ _ method should accept an argument for each attribute. The Patient class should also have accessor and mutator methods for each attribute. Next, write a class named Procedure that represents a medical procedure that has been performed on a patient. The Procedure class should have attributes for the following data: Name of the procedure Date of the procedure Name of the practitioner who performed the procedure Charges for the procedure The Procedure class’s _ _init_ _ method should accept an argument for each attribute. The Procedure class should also have accessor and mutator methods for each attribute. Next, write a program that creates an instance of the Patient class, initialized with sample data. Then,…arrow_forwardPPM (Portable Pixmap) use three integers to represent a pixel – this means we can have images with RGB colors. You will create a Pixel class in C++ which has three attributes: red: int green: int blue: int You will create a default constructor that initializes those values to 255, and an overloaded constructor that takes user input to assign the values. The class will also have the following functions: changeRGB (): Takes in three integers to update the red, green, and blue attributes. Returns nothing. printRGB (): Takes in nothing. Prints the red, green, and blue attributes in order with a single space in-between each value. Returns nothing. You will then recreate the art program from Assignment 5 with the following changes: Instead of a 2D array of integers, you will create a 2D array of Pixel object. Don’t be scared! This is similar to creating a 2D array of strings. You will prompt for three color values instead of one – red, green, and blue. These must be stored in a Pixel…arrow_forwardCreate two classes named Mammals and MarineAnimals. Create another class named BlueWhale which inherits both the above classes. Now, create a function in each of these classes which prints "I am mammal", "I am a marine animal" and "I belong to both the categories: Mammals as well as Marine Animals" respectively. Now, create an object for each of the above class and try calling function of Mammals by the object of Mammal function of BlueWhale by the object of BlueWhale function of each of its parent by the object of BlueWhale function of MarineAnimal by the object of MarineAnimalarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning