1. class Sample { public: Sample(); void show() const; void one(int); int two(const string&); private: int first; float second; string third; }; 2. Sample::Sample() { first = 2; second = -10.5; } void Sample::one(int val) { second = val + first; } int Sample::two(const string &newstr) { third = newstr; if (second < 0) return 1; else return 0; } void Sample::show() const { cout << "First is " << first << " Second is " << second << " Third is "; third.print(); cout << endl; } 3. class Other { public: Other(); void othone(int,int); void othtwo(const string&); void print() const; private: string alpha; Sample beta; Sample gamma; int delta; }; 4. Other::Other() { delta = -1; } void Other::othone(int num1, int num2) { beta.one(num1); gamma.one(num2); } void Other::othtwo(const string& text) { alpha = text; } void Other::print() const { cout << "Alpha is "; alpha.print(); cout << endl << "Beta is "; beta.show(); cout << endl << "Gamma is "; gamma.show(); cout << endl << "Delta is " << delta << endl; } 5. Suppose the user enters mouse pad as the input to the program. Then the output will be: Not yet Alpha is Beta is First is 2 Second is -10.5 Third is Gamma is First is 2 Second is -10.5 Third is Delta is -1 Alpha is mouse pad Beta is First is 2 Second is 7.0 Third is Gamma is First is 2 Second is 8.0 Third is Delta is -1 First is 2 Second is 10.0 Third is 6. class Book { public: void setBook(const string&, const string&, float, int); void print() const; void update(char); private: string title; string author; float price; int copies; }; 7. void Book::setBook(const string& auth, const string& ti, float pr, int numcop) { author = auth; title = ti; price = pr; copies = numcop; } void Book::print() const { cout << "The author is "; author.print(); cout << "\nThe title is "; title.print(); cout << "\nThe price is " << price; cout << "\nThere are " << copies << " copies in stock\n\n"; } void Book::update(char which) { if (which == ‘T’ || which == ‘t’) { cout << "Enter the new book title: "; title.read(); } if (which == ‘A’ || which == ‘a’) { cout << "Enter the new author: "; author.read(); } if (which == ‘P’ || which == ‘p’) { cout << "Enter the new price: "; cin >> price; } if (which == ‘C’ || which == ‘c’) { cout << "Enter the new number of copies on hand: "; cin >> copies; } } 8. const int MAXBOOKS = 100; class BookStore { public: BookStore(); int addBook(const Book&); void printAll() const; int update(const string&); private: Book books[MAXBOOKS]; int numOfBooks; }; 9. BookStore::BookStore() { numOfBooks = 0; } int BookStore::addBook(const Book& newbook) { int ok = 0; if (numOfBooks < MAXBOOKS) { ok = 1; books[numOfBooks] = newbook; numOfBooks++; } return ok; } void BookStore::printAll() const { cout << "The books in the bookstore are:\n"; for (int i = 0 ; i < numOfBooks ; i++) books[i].print(); } The update function cannot be written for the BookStore class because there is no way for a BookStore member function to check the title of a book.
Email Me | Office Hours | My Home Page | Department Home | MCC Home Page
© Copyright Emmi Schatz 2003