C++ assigment:
It's a Number Guesser assignment. It asks to write a derived class of the NumberGuesser class named RandomNumberGuesser. The derived class should override the behavior of the getCurrentGuess method. It may also add member data and its own constructor. It may also override the higher(), lower() and reset() methods as you see fit.
This is the code for NumberGuesser class. It is defined entirely in-line in the following file: NumberGuesser.h
// NumberGuesser.h
// #ifndef NUMBERGUESSER_H
#define NUMBERGUESSER_H
#include <iostream>
class NumberGuesser
{
protected:
int low;
int originalLow;
int high;
int originalHigh;
public:
NumberGuesser(int l, int h) {
low = originalLow = l; high = originalHigh = h;
}
virtual int getCurrentGuess() {
return (high + low) / 2;
}
virtual void higher() {
low = getCurrentGuess() + 1;
}
virtual void lower() {
high = getCurrentGuess() - 1;
}
virtual void reset() { low = originalLow; high = originalHigh;
}
};
#endif
In the current NumberGuesser class the getCurrentGuess() method returns the midpoint of the range of possible values. In your RandomNumberGuesser class the getCurrentGuess() method should return a randomly generated number in the range of possible values.
Note that repeated calls to getCurrentGuess() should always return the same value for both classes if neither the higher() or the lower() functions are called. Consider the following example:
NumberGuesser *ng = new NumberGuesser(1, 10);
cout << ng->getCurrentGuess();
cout << ng->getCurrentGuess();
cout << ng->getCurrentGuess();
Each invocation of getCurrentGuess should return 5. This value does not change until the higher() or lower() function is called.
Now consider a RandomNumberGuesser example. The first call to getCurrentGuess() should return a random number between 1 and 10, inclusive. Each subsequent call to getCurrentGuess() should return the same number, until higher() or lower() is called, at which point a new random number within the new range should be generated. For example:
NumberGuesser *ng = new RandomNumberGuesser(1, 10);
// picks a random number between 1 and 10, let’s say it is 3, and outputs it.
cout << ng->getCurrentGuess();
// this line prints out 3 again, because it is still the current guess
cout << ng->getCurrentGuess();
// this line changes low to 4 because we now know the number is higher than 3.
// high remains unchanged at 10.
ng->higher();
// this line picks a random number between 4 and 10 and prints it out,
// let’s say it is 9
cout << ng->getCurrentGuess();
// this line prints out 9 again, because it is still the current guess
cout << ng->getCurrentGuess();
Make careful note of how the word “virtual” is used in NumberGuesser.h.
There should be a RandomNumberGuesser.h and RandomNumberGuesser.cpp file at the end. Thanks
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images
- What is the difference between a friend function of a class and a member function of a class? (3)arrow_forwardIn previous chapters, you have created programs for the Greenville Idol competition. Now create a Contestant class with the following characteristics: The Contestant class contains public static arrays that hold talent codes and descriptions. Recall that the talent categories are Singing Dancing, Musical instrument, and Other. The class contains an auto-implemented property that holds a contestants name. The class contains fields for a talent code and description. The set accessor for the code assigns a code only if it is valid. Otherwise, it assigns I for Invalid. The talent description is a read-only property that is assigned a value when the code is set. Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this years competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. The program prompts the user for names and talent codes for each contestant entered. Along with the prompt for a talent code, display a list of the valid categories. After data entry is complete, the program displays the valid talent categories and then continuously prompts the user for talent codes and displays the names of all contestants in the category. Appropriate messages are displayed if the entered code is not a character or a valid code.arrow_forwardA(n) ____________ is one instance or variable of a class.arrow_forward
- At most, a class can contain ____________ method(S). 0 1 2 any number ofarrow_forwardIn Chapter 10, the class clockType was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derive the class extclockType from the class clockType by adding a member variable to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors. Finally, write a test program to test your class.arrow_forwardUsing classes, design an online address book to keep track of the names, addresses, phone numbers, and dates of birth of family members, close friends, and certain business associates. Your program should be able to handle a maximum of 500 entries. Define a class addressType that can store a street address, city, state, and ZIP code. Use the appropriate functions to print and store the address. Also, use constructors to automatically initialize the member variables. Define a class extPersonType using the class personType (as defined in Example 10-10, Chapter 10), the class dateType (as designed in this chapters Programming Exercise 2), and the class addressType. Add a member variable to this class to classify the person as a family member, friend, or business associate. Also, add a member variable to store the phone number. Add (or override) the functions to print and store the appropriate information. Use constructors to automatically initialize the member variables. Define the class addressBookType using the previously defined classes. An object of the type addressBookType should be able to process a maximum of 500 entries. The program should perform the following operations: Load the data into the address book from a disk. Sort the address book by last name. Search for a person by last name. Print the address, phone number, and date of birth (if it exists) of a given person. Print the names of the people whose birthdays are in a given month. Print the names of all the people between two last names. Depending on the users request, print the names of all family members, friends, or business associates.arrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning