当前位置:首页 > C++Primer - Plus(第五版)习题解答
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
else
std::cout << \}
void BankAccount::withdraw(double cash) {
if (cash < 0)
std::cout << \ else if (cash <= balance) balance -=cash; else
std::cout << \}
// sample use
int main() {
BankAccount bird;
BankAccount frog(\
bird.show(); frog.show();
bird = BankAccount(\ bird.show();
frog.deposit(20); frog.show();
frog.withdraw(4000); frog.show();
frog.withdraw(50); frog.show(); }
PE10-4
// pe10-4.h
#ifndef SALES__ #define SALES__
namespace SALES {
const int QUARTERS = 4; class Sales {
private:
double sales[QUARTERS]; double average; double max; double min; public:
// default constructor Sales();
// copies the lesser of 4 or n items from the array ar // to the sales member and computes and stores the
SP 21 of 73 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
// average, maximum, and minimum values of the entered items; // remaining elements of sales, if any, set to 0 Sales(const double ar[], int n);
// gathers sales for 4 quarters interactively, stores them // in the sales member of object and computes and stores the // average, maximum, and minumum values void setSales();
// display all information in object void showSales(); }; }
#endif
// pe10-4a.cpp
#include
int main() {
using SALES::Sales;
double vals[3] = {2000, 3000, 5000}; Sales forFiji(vals, 3); forFiji.showSales();
Sales red;
red.showSales(); red.setSales(); red.showSales();
return 0; }
// pe10-4b.cpp
#include
namespace SALES {
using std::cin; using std::cout; using std::endl;
Sales::Sales(const double ar[], int n) {
if (n < 0) n = 0;
int limit = n < QUARTERS ? n : QUARTERS; double total = 0; min = 0; max = 0;
average = 0;
SP 22 of 73 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
if (limit > 0)
min = max = ar[0]; int i;
for (i = 0; i < limit; i++) {
sales[i] = ar[i]; total += ar[i]; if (ar[i] > max) max = ar[i];
else if (ar[i] < min) min = ar[i]; }
for (i = limit; i < QUARTERS; i++) sales[i] = 0; if (limit > 0)
average = total / limit; }
Sales::Sales() {
min = 0; max = 0;
average = 0;
for (int i = 0; i < QUARTERS; i++) sales[i] =0; }
void Sales::setSales() {
double sa[QUARTERS]; int i;
for (i = 0; i < QUARTERS; i++) {
cout << \ cin >> sa[i]; }
// create temporary object, copy to invoking object *this = Sales(sa, QUARTERS); }
void Sales::showSales() {
cout << \
for (int i = 0; i < QUARTERS; i++)
cout << \ << sales[i] << endl;
cout << \ cout << \ cout << \ } }
SP 23 of 73 September 2, 2004
Solutions for Programming Exercises in C++ Primer Plus, 5th Edition
PE 10-5
// pe10stack.h -- class definition for the stack ADT // for use with pe10-5.cpp #ifndef _STACK_H_ #define _STACK_H_
struct customer {
char fullname[35]; double payment; };
typedef customer Item;
class Stack {
private:
enum {MAX = 10}; // constant specific to class Item items[MAX]; // holds stack items int top; // index for top stack item public:
Stack();
bool isempty() const; bool isfull() const;
// push() returns false if stack already is full, true otherwise bool push(const Item & item); // add item to stack
// pop() returns false if stack already is empty, true otherwise bool pop(Item & item); // pop top into item };
#endif
// pe10stack.cpp -- Stack member functions // for use with pe10-5.cpp
// exactly the same as stack.cpp in the text
#include \
Stack::Stack() // create an empty stack {
top = 0; }
bool Stack::isempty() const {
return top == 0; }
bool Stack::isfull() const {
return top == MAX; }
bool Stack::push(const Item & item) {
if (top < MAX) {
SP 24 of 73 September 2, 2004
共分享92篇相关文档