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++ (9th Edition)
Additional Engineering Textbook Solutions
Starting Out with Python (3rd Edition)
Starting Out with C++ from Control Structures to Objects (8th Edition)
Starting Out with Java: Early Objects (6th Edition)
Starting Out with C++: Early Objects
Starting Out with C++ from Control Structures to Objects (9th Edition)
- Write a class named Car with member variables: yearModel, Make, Speed as private.Use a Constructor that accepts year model and make as arguments. It should also assign 0 to thespeed use accelerate and brake functions that add and subtract 5 from speed each time it is called.Demonstrate the class in a program that creates a Car object, and then calls the accelerate function5 times. After each call to the accelerate function, get the current speed of the car and display it.Then, call the brake function five times. After each call to the brake function, get the current speedof the car and display it.arrow_forwardCreate a class called Complex for performing arithmetic with complex numbers. Write a 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 that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks: a) Adding two Complex numbers: The real parts are added together and the imaginary parts are added together. b) Subtracting 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. c) Printing Complex numbers in the form (a, b)., where a is the real part and b is the imaginary part. #include #include "Complex.h" using namespace std; int main() {…arrow_forwardAssume 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_forward
- Mark 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_forwardiN C++ Write a class named Car that has the following member variables:yearModel. An int that holds the car’s year model.make. A string 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 constructor and other member functions. Constructor. The constructor should accept the car’s year model and make as arguments. These values should be assigned to the object’s yearModel and make member variables. The constructor should also assign 0 to the speed member variables. Accessor. Appropriate accessor functions to get the values stored in an object’s yearModel, make, and speed member variables. accelerate. The accelerate function should add 5 to the speed member variable each time it is called.brake. The brake function should subtract 5 from the speed member variable each time it is called. Demonstrate the class in a program that creates a Car object, and then calls the accelerate function five times. After each call…arrow_forwardWrite 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_forward
- In 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_forwardSOLVE IN "C#" Write a program to create a class employee, it consist of ID, name, department and address. All employees belongs to “Computer Science” department and it can never be changed by any means. Employee ID is initialized only once when Employee object is created, any further attempt to change ID should be failed. Class must have a 3 parameterized constructor to set values and two methods: print(): to display all the data of a particular employee totalObjects(): to count and print total number of objects that has been created In Main(), create atleast two objects of employee class, display their records by calling print() function and also print the total number of objects that has been created. Sample Main Method: static void Main(string[] args) { Employee obj1 = new Employee(1, "AMMAD", "Karachi"); obj1.print(); Employee obj2 = new Employee(2, "ALI", "Islamabad"); obj2.print(); Employee.totalObjects(); }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_forwardWrite a class called Pet that contains an animal’s name, type, and weight. Include a default constructor and destructor for the class. The constructor should print out the following message: “Creating a new pet”. The destructor should print out the following message: “In the Pet destructor.” Include appropriate get/set functions for the class. In main(), prompt the user to enter the number of pets in his or her household, using a constant. Dynamically create a built-in array based on this number (not a vector or object of the array class) to hold pointers to Pet objects. Construct a loop in main() that executes once for each of the number of pets that the user indicated. Within this loop, ask the user to enter the name and type of pet. Using a random number generator, generate a weight between 1-100 pounds. Seed this random number generator with 100. Next, dynamically create a Pet object (remember that this requires the use of the “new” keyword which returns a pointer to…arrow_forwardWrite a class called Pet that contains an animal’s name, type, and weight. Include a default constructor and destructor for the class. The constructor should print out the following message: “Creating a new pet”. The destructor should print out the following message: “In the Pet destructor.” Include appropriate get/set functions for the class. In main(), prompt the user to enter the number of pets in his or her household. Dynamically create a built-in array based on this number (not a vector or object of the array class) to hold pointers to Pet objects. Construct a loop in main() that executes once for each of the number of pets that the user indicated. Within this loop, ask the user to enter the name and type of pet. Using a random number generator, generate a weight between 1-100 pounds. Seed this random number generator with 100. Next, dynamically create a Pet object (remember that this requires the use of the “new” keyword which returns a pointer to the location in…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning