New Semester
Started
Get
50% OFF
Study Help!
--h --m --s
Claim Now
Question Answers
Textbooks
Find textbooks, questions and answers
Oops, something went wrong!
Change your search query and then try again
S
Books
FREE
Study Help
Expert Questions
Accounting
General Management
Mathematics
Finance
Organizational Behaviour
Law
Physics
Operating System
Management Leadership
Sociology
Programming
Marketing
Database
Computer Network
Economics
Textbooks Solutions
Accounting
Managerial Accounting
Management Leadership
Cost Accounting
Statistics
Business Law
Corporate Finance
Finance
Economics
Auditing
Tutors
Online Tutors
Find a Tutor
Hire a Tutor
Become a Tutor
AI Tutor
AI Study Planner
NEW
Sell Books
Search
Search
Sign In
Register
study help
computer science
introduction java program
C++ Primer Plus 6th Edition Stephen Prata - Solutions
Given the template of Chapter Review Question 7 and the box structure of Chapter Review Question 4, provide a template specialization that takes two box arguments and returns the one with the larger volume.Data From Question 4The following is a structure template:struct box{char maker[40];float
Describe the differences between a using declaration and a using directive.
What types are assigned to v1, v2, v3, v4, and v5 in the following code (assuming the code is part of a complete program)?int g(int x);...float m = 5.5f;float & rm = m;decltype(m) v1 = m;decltype(rm) v2 = m;decltype((m)) v3 = m;decltype (g(100)) v4;decltype (2.0 * m) v5;
What storage scheme would you use for the following situations?a. Homer is a formal argument (parameter) to a function.b. The secret variable is to be shared by two files.c. The topsecret variable is to be shared by the functions in one file but hidden from other files.d. Beencalled keeps track of
Here is a header file:// golf.h -- for pe9-1.cppconst int Len = 40;struct golf{char fullname[Len];int handicap;};// non-interactive version:// function sets golf structure to provided name, handicap// using values passed as arguments to the functionvoid setgolf(golf & g, const char * name, int
Begin with the following structure declaration:struct chaff{char dross[20];int slag;};Write a program that uses placement new to place an array of two such structures in a buffer.Then assign values to the structure members (remembering to use strcpy() for the char array) and use a loop to display
Rewrite the following so that it uses using declarations instead of the using directive:#includeusing namespace std;int main(){double x;cout << "Enter value: ";while (! (cin >> x) ){cout << "Bad input. Please enter a number: ";cin.clear();while (cin.get() != '\n')continue;}cout
Write a three-file program based on the following namespace:namespace SALES{const int QUARTERS = 4;struct Sales{double sales[QUARTERS];double average;double max;double min;};// copies the lesser of 4 or n items from the array ar// to the sales member of s and computes and stores the// average,
Suppose you want the average(3,6) function to return an int average of the two int arguments when it is called in one file, and you want it to return a double average of the two int arguments when it is called in a second file in the same program. How could you set this up?
What will the following two-file program display?// file1.cpp#includeusing namespace std;void other();void another();int x = 10;int y;int main(){cout << x << endl;{int x = 4;cout << x << endl;cout << y << endl;}other();another();return 0;}void other(){int y =
What will the following program display?#includeusing namespace std;void other();namespace n1{int x = 1;}namespace n2{int x = 2;}int main(){using namespace n1;cout << x << endl;{int x = 4;cout << x << ", " << n1::x << ", " << n2::x << endl;}using
Provide method definitions for the class described in Chapter Review Question 5 and write a short program that illustrates all the features.Data From Question 5Define a class to represent a bank account. Data members should include the depositor’s name, the account number (use a string), and the
What is a class?
Here is a rather simple class definition:class Person {private:static const LIMIT = 25;string lname; // Person’s last namechar fname[LIMIT]; // Person’s first namepublic:Person() {lname = ""; fname[0] = ‘\0’; } // #1Person(const string & ln, const char * fn = "Heyyou"); // #2// the
How does a class accomplish abstraction, encapsulation, and data hiding?
Do Programming Exercise 1 from Chapter 9 but replace the code shown there with an appropriate golf class declaration. Replace setgolf(golf &, const char*, int) with a constructor with the appropriate argument for providing initial values. Retain the interactive version of setgolf() but
What is the relationship between an object and a class?
In what way, aside from being functions, are class function members different from class data members?
Define a class to represent a bank account. Data members should include the depositor’s name, the account number (use a string), and the balance. Member functions should allow the following:Creating an object and initializing it.Displaying the depositor’s name, account number, and
Consider the following structure declaration:struct customer {char fullname[35];double payment;};Write a program that adds and removes customer structures from a stack, represented by a Stack class declaration. Each time a customer is removed, his or her payment should be added to a running total,
When are class constructors called? When are class destructors called?
Here’s a class declaration:class Move{private:double x;double y;public:Move(double a = 0, double b = 0); // sets x, y to a, bshowmove() const; // shows current x, y valuesMove add(const Move & m) const;// this function adds x of m to x of invoking object to get new x,// adds y of m to y of
Provide code for a constructor for the bank account class from Chapter Review Question 5.Data From Question 5Define a class to represent a bank account. Data members should include the depositor’s name, the account number (use a string), and the balance. Member functions should allow the
A Betelgeusean plorg has these properties:DataA plorg has a name with no more than 19 letters.A plorg has a contentment index (CI), which is an integer.OperationsA new plorg starts out with a name and a CI of 50.A plorg’s CI can change.A plorg can report its name and CI.The default plorg has the
What is a default constructor? What is the advantage of having one?
You can describe a simple list as follows:The simple list can hold zero or more items of some particular type.You can create an empty list.You can add items to the list.You can determine whether the list is empty.You can determine whether the list is full.You can visit each item in the list and
Modify the Stock class definition (the version in stock20.h) so that it has member functions that return the values of the individual data members. A member that returns the company name should not provide a weapon for altering the array. That is, it can’t simply return a string reference. It
Modify the Vector class header and implementation files (Listings 11.13 and 11.14) so that the magnitude and angle are no longer stored as data components. Instead, they should be calculated on demand when the magval() and angval() methods are called.You should leave the public interface unchanged
What are this and *this?
Use a member function to overload the multiplication operator for the Stonewt class; have the operator multiply the data members by a type double value. Note that this will require carryover for the stone–pound representation. That is, twice 10 stone 8 pounds is 21 stone 2 pounds.
What are the differences between a friend function and a member function?
Does a nonmember function have to be a friend to access a class’s members?
Use a friend function to overload the multiplication operator for the Stonewt class; have the operator multiply the double value by the Stone value.
Which operators cannot be overloaded?
What restriction applies to overloading the following operators? =, (), [], and ->
Define a conversion function for the Vector class that converts a Vector object to a type double value that represents the vector’s magnitude.
A complex number has two parts: a real part and an imaginary part. One way to write an imaginary number is this: (3.0, 4.0). Here 3.0 is the real part and 4.0 is the imaginary part. Suppose a = (A,Bi) and c = (C,Di).Here are some complex operations:n Addition: a + c = (A + C, (B + D)i)n
Suppose a String class has the following private members:class String{private:char * str; // points to string allocated by newint len; // holds length of string//...};a. What’s wrong with this default constructor?String::String() {}b. What’s wrong with this constructor?String::String(const char
Consider the following class declaration:class Cow {char name[20];char * hobby;double weight;public:Cow();Cow(const char * nm, const char * ho, double wt);Cow(const Cow c&);~Cow();Cow & operator=(const Cow & c);void ShowCow() const; // display all cow data};Provide the implementation
Name three problems that may arise if you define a class in which a pointer member is initialized by using new. Indicate how they can be remedied.
Enhance the String class declaration (that is, upgrade string1.h to string2.h) by doing the following:a. Overload the + operator to allow you to join two strings into one.b. Provide a stringlow() member function that converts all alphabetic characters in a string to lowercase. (Don’t forget the
Rewrite the Stock class, as described in Listings 10.7 and 10.8 in Chapter 10 so that it uses dynamically allocated memory directly instead of using string class objects to hold the stock names. Also replace the show() member function with an overloaded operator<<() definition. Test the new
What class methods does the compiler generate automatically if you don’t provide them explicitly? Describe how these implicitly generated functions behave.
Identify and correct the errors in the following class declaration:class nifty{// datachar personality[];int talents;// methodsnifty();nifty(char * s);ostream & operator<<(ostream & os, nifty & n);}nifty:nifty(){personality = NULL;talents = 0;}nifty:nifty(char * s){personality =
Consider the following class declaration:class Golfer{private:char * fullname; // points to string containing golfer's nameint games; // holds number of golf games playedint * scores; // points to first element of array of golf scorespublic:Golfer();Golfer(const char * name, int g= 0);// creates
The Bank of Heather would like to know what would happen if it added a second ATM. Modify the simulation in this chapter so that it has two queues. Assume that a customer will join the first queue if it has fewer people in it than the second queue and that the customer will join the second queue
In Listing 6.2, what is the effect of replacing ++ch with ch+1?Here’s some sample output from the program in Listing 6.2:Type, and I shall repeat.An ineffable joy suffused me as I beheldBo!jofggbcmf!kpz!tvggvtfe!nf!bt!J!cfifmethe wonders of modern computing.uif!xpoefst!pg!npefso!dpnqvujohPlease
Redo Listing 5.4 using a type array object instead of a built-in array and type long double instead of long long. Find the value of 100!Here is the output from the program in Listing 5.4:0! = 11! = 12! = 23! = 64! = 245! = 1206! = 7207! = 50408! = 403209! = 36288010! = 362880011! = 3991680012! =
Listing 4.6 illustrates a problem created by following numeric input with line-oriented string input. How would replacing this:cin.getline(address,80);with this:cin >> address;affect the working of this program?Running the program in Listing 4.6 would look something like this:What year was
Write a code fragment that dynamically allocates a structure of the type described in Question 8 and then reads a value for the kind member of the structure.Data From Question 8Devise a structure declaration that describes a fish. The structure should include the kind, the weight in whole ounces,
Do Programming Exercise 6, but instead of declaring an array of three CandyBar structures, use new to allocate the array dynamically.Data From Exercise 6The CandyBar structure contains three members, as described in Programming Exercise 5.Write a program that creates an array of three CandyBar
Declare a variable of the type defined in Question 8 and initialize it.Data From Question 8Devise a structure declaration that describes a fish.The structure should include the kind, the weight in whole ounces, and the length in fractional inches.
Do Programming Exercise 7 but use new to allocate a structure instead of declaring a structure variable. Also have the program request the pizza diameter before it requests the pizza company name.Data From Exercise 7William Wingate runs a pizza-analysis service. For each pizza, he needs to record
Write a statement that assigns the sum of the first and last elements of the array in Question 3 to the variable even.Data From Question 3Declare an array of five ints and initialize it to the first five odd positive integers.
Rewrite Listing 4.4, using the C++ string class instead of char arrays.Here is some sample output for Listing 4.4:Enter your name:Dirk HammernoseEnter your favorite dessert:Radish TorteI have some delicious Radish Torte for you, Dirk Hammernose. The program now reads complete names and delivers the
What are the modules of C++ programs called?
Write a C++ program that displays your name and address (or if you value your privacy, a fictitious name and address).
What does the following preprocessor directive do? #include
Write a C++ program that asks for a distance in furlongs and converts it to yards. (One furlong is 220 yards.)
What does the following statement do? using namespace std;
Write a C++ program that uses three user-defined functions (counting main() as one) and produces the following output:Three blind miceThree blind miceSee how they runSee how they runOne function, called two times, should produce the first two lines, and the remaining function, also called twice,
What statement would you use to print the phrase “Hello, world” and then start a new line?
Write a program that asks the user to enter his or her age. The program then should display the age in months:Enter your age: 29Your age in months is 384.
What statement would you use to create an integer variable with the name cheeses?
Write a program that has main() call a user-defined function that takes a Celsius temperature value as an argument and then returns the equivalent Fahrenheit value. The program should request the Celsius value as input from the user and display the result, as shown in the following code:Please
What statement would you use to assign the value 32 to the variable cheeses?
Write a program that has main() call a user-defined function that takes a distance in light years as an argument and then returns the distance in astronomical units. The program should request the light year value as input from the user and display the result, as shown in the following code:Enter
What statement would you use to read a value from keyboard input into the variable cheeses?
Write a program that asks the user to enter an hour value and a minute value.The main() function should then pass these two values to a type void function that displays the two values in the format shown in the following sample run:Enter the number of hours: 9Enter the number of minutes: 28Time:
What statement would you use to print “We have X varieties of cheese,” where the current value of the cheeses variable replaces X?
What do the following function prototypes tell you about the functions?int froop(double t);void rattle(int n);int prune(void);
When do you not have to use the keyword return when you define a function?
Suppose your main() function has the following line: cout << “Please enter your PIN: “;And suppose the compiler complains that cout is an unknown identifier.What is the likely cause of this complaint, and what are three ways to fix the problem?
Why does C++ have more than one integer type?
Write a short program that asks for your height in integer inches and then converts your height to feet and inches. Have the program use the underscore character to indicate where to type the response. Also use a const symbolic constant to represent the conversion factor.
Declare variables matching the following descriptions:a. A short integer with the value 80b. An unsigned int integer with the value 42,110c. An integer with the value 3,000,000,000
Write a short program that asks for your height in feet and inches and your weight in pounds. (Use three variables to store the information.) Have the program report your body mass index (BMI).To calculate the BMI, first convert your height in feet and inches to your height in inches (1 foot = 12
What safeguards does C++ provide to keep you from exceeding the limits of an integer type?
Write a program that asks the user to enter a latitude in degrees, minutes, and seconds and that then displays the latitude in decimal format.There are 60 seconds of arc to a minute and 60 minutes of arc to a degree; represent these values with symbolic constants.You should use a separate variable
What is the distinction between 33L and 33?
Write a program that asks the user to enter the number of seconds as an integer value (use type long, or, if available, long long) and that then displays the equivalent time in days, hours, minutes, and seconds. Use symbolic constants to represent the number of hours in the day, the number of
Consider the two C++ statements that follow:char grade = 65;char grade = 'A';Are they equivalent?
Write a program that requests the user to enter the current world population and the current population of the U.S. (or of some other nation of your choice). Store the information in variables of type long long. Have the program display the percent that the U.S. (or other nation’s) population is
How could you use C++ to find out which character the code 88 represents? Come up with at least two ways.
Write a program that asks how many miles you have driven and how many gallons of gasoline you have used and then reports the miles per gallon your car has gotten. Or, if you prefer, the program can request distance in kilometers and petrol in liters and then report the result European style, in
Assigning a long value to a float can result in a rounding error.What about assigning long to double? long long to double?
Write a program that asks you to enter an automobile gasoline consumption figure in the European style (liters per 100 kilometers) and converts to the U.S. style of miles per gallon. Note that in addition to using different units of measurement, the U.S. approach (distance / fuel) is the inverse of
Evaluate the following expressions as C++ would:a. 8 * 9 + 2b. 6 * 3 / 4c. 3 / 4 * 6d. 6.0 * 3 / 4e. 15 % 4
Suppose x1 and x2 are two type double variables that you want to add as integers and assign to an integer variable. Construct a C++ statement for doing so. What if you want to add them as type double and then convert to int?
Write a C++ program that requests and displays information as shown in the following example of output:What is your first name? Betty SueWhat is your last name? YeweWhat letter grade do you deserve? BWhat is your age? 22Name: Yewe, Betty SueGrade: CAge: 22Note that the program should be able to
Declare an array of five ints and initialize it to the first five odd positive integers.
Write a program that asks the user to enter his or her first name and then last name, and that then constructs, stores, and displays a third string, consisting of the user’s last name followed by a comma, a space, and first name. Use char arrays and functions from the cstring header file.A sample
Write a program that asks the user to enter his or her first name and then last name, and that then constructs, stores, and displays a third string consisting of the user’s last name followed by a comma, a space, and first name. Use string objects and methods from the string header file.A sample
Write a statement that displays the value of the second element in the float array ideas.
The CandyBar structure contains three members.The first member holds the brand name of a candy bar.The second member holds the weight (which may have a fractional part) of the candy bar, and the third member holds the number of calories (an integer value) in the candy bar.Write a program that
The CandyBar structure contains three members, as described in Programming Exercise 5.Write a program that creates an array of three CandyBar structures, initializes them to values of your choice, and then displays the contents of each structure.
What is the variable type for each of the following declarations?a. auto cars = 15;b. auto iou = 150.37f;c. auto level = 'B';d. auto crat = U'/U00002155';e. auto fract = 8.25f/2.5;
How would you declare each of the following?a. Actors is an array of 30 char.b. Betsie is an array of 100 short.c. Chuck is an array of 13 float.d. Dipsea is an array of 64 long double.
Declare an array of char and initialize it to the string "cheeseburger".
Declare a string object and initialize it to the string "Waldorf Salad".
Showing 600 - 700
of 741
1
2
3
4
5
6
7
8
Step by Step Answers