A Complete Guide to Programming in C++ part 55 potx

10 1.4K 18
A Complete Guide to Programming in C++ part 55 potx

Đang tải... (xem toàn văn)

Thông tin tài liệu

EXERCISES ■ 519 Exercise 3 A supermarket chain has asked you to develop an automatic checkout system. All products are identifiable by means of a barcode and the product name. Groceries are either sold in packages or by weight. Packed goods have fixed prices.The price of groceries sold by weight is calculated by multiplying the weight by the current price per kilo. Develop the classes needed to represent the products first and organize them hierarchically.The Product class, which contains generic information on all products (barcode, name, etc.), can be used as a base class. ■ The Product class contains two data members of type long used for storing barcodes and the product name. Define a constructor with parameters for both data members.Add default values for the para- meters to provide a default constructor for the class. In addition to the access methods setCode() and getCode(), also define the methods scanner() and printer(). For test purposes, these methods will simply output product data on screen or read the data of a product from the keyboard. ■ The next step involves developing special cases of the Product class. Define two classes derived from Product, PrepackedFood and Fresh- Food . In addition to the product data, the PrepackedFood class should contain the unit price and the FreshFood class should contain a weight and a price per kilo as data members. In both classes define a constructor with parameters providing default-values for all data members. Use both the base and member ini- tializer. Define the access methods needed for the new data members.Also redefine the methods scanner() and printer() to take the new data members into consideration. ■ Test the various classes in a main function that creates two objects each of the types Product, PrepackedFood and FreshFood. One object of each type is fully initialized in the object definition. Use the default con- structor to create the other object.Test the get and set methods and the scanner() method and display the products on screen. solutions 520 ■ CHAPTER 23 INHERITANCE ■ SOLUTIONS Exercise 1 // // Car.h : Defines the base class Car and // the derived classes PassCar and Truck // #ifndef _CAR_H_ #define _CAR_H_ #include <iostream> #include <string> using namespace std; class Car { // See previous definition in this chapter }; class PassCar : public Car { // See previous definition in this chapter }; class Truck : public Car { private: int axles; double tons; public: Truck( int a, double t, int n, const string& hs); ~Truck(); void setAxles(int l){ axles = l;} int getAxles() const { return axles; } void setCapacity( double t) { tons = t;} double getCapacity() const { return tons; } void display() const; }; #endif SOLUTIONS ■ 521 // // car.cpp // Implements the methods of Car, PassCar, and Truck // #include "car.h" // // The methods of base class Car: Car::Car( long n, const string& prod) { cout << "Creating an object of type Car." << endl; nr = n; producer = prod; } Car::~Car() { cout << "Destroying an object of type Car" << endl; } void Car::display() const { cout << "\n " << "\nCar number: " << nr << "\nProducer: " << producer << endl; } // // The methods of the derived class PassCar: PassCar::PassCar(const string& tp, bool sd, int n, const string& hs) : Car( n, hs), PassCarTyp( tp ), sunRoof( sd ) { cout << "I create an object of type PassCar." << endl; } PassCar::~PassCar() { cout << "\nDestroying an object of type PassCar" << endl; } void PassCar::display( void) const { Car::display(); // Base class method cout << "Type: " << passCarType << "\nSunroof: "; if(sunRoof) cout << "yes "<< endl; else cout << "no " << endl; } 522 ■ CHAPTER 23 INHERITANCE // // The methods of the derived class Truck: Truck::Truck( int a, double t, int n, const string& hs) : Car( n, hs), axles(a), tons(t) { cout << "Creating an object of type Truck." << endl; } Truck::~Truck() { cout << "\nDestroying an object of type Truck\n"; } void Truck::display() const { Car::display(); cout << "Axles: " << axles << "\nCapacity: " << tons << " long tons\n"; } // // Car_t.cpp : Tests the base class Car and // the derived classes PassCar and Truck. // #include "car.h" int main() { Truck toy(5, 7.5, 1111, "Volvo"); toy.display(); char c; cout << "\nDo you want to create an object of type " << " PassCar? (y/n) "; cin >> c; if( c == 'y' || c == 'Y') { const PassCar beetle("Beetle", false, 3421, "VW"); beetle.display(); } cout << "\nDo you want to create an object " << " of type car? (y/n) "; cin >> c; if( c == 'y' || c == 'Y') { const Car oldy(3421, "Rolls Royce"); oldy.display(); } return 0; } SOLUTIONS ■ 523 Exercise 2 // // account.h: // Defines the classes Account, DepAcc, and SavAcc. // #ifndef _ACCOUNT_H #define _ACCOUNT_H #include <iostream> #include <iomanip> #include <string> using namespace std; class Account { private: string name; unsigned long nr; double balance; public: Account(const string& s="X", unsigned long n = 1111111L, double st = 0.0) : name(s), nr(n), balance(st) { } const string& getName() const { return name; } void setName(const string& n) { name = n;} unsigned long getNr() const { return nr; } void setNr(unsigned long n) { nr = n; } double getBalance() const { return balance; } void setBalance(double st){ balance = st; } void display() { cout << fixed << setprecision(2) << " \n" << "Account holder: " << name << endl << "Account number: " << nr << endl << "Balance of the account:" << balance <<endl; } }; class DepAcc : public Account { private: double limit; // Overdraft limit double interest; // Interest public: DepAcc(const string& s = "X", unsigned long n = 1111111L, double st = 0.0, double li = 0.0, double ra = 0.0) : Account(s, n, st), limit(li), interest(ra) { } 524 ■ CHAPTER 23 INHERITANCE // Access methods: double getLimit() const { return limit; } void setLimit(double lt){ limit = lt; } double getInterest() const { return interest; } void setInterest(double sl){ interest = sl; } void display() { Account::display(); cout << fixed << setprecision(2) << "Overdraft limit: " << limit << endl << "Interest rate: " << interest << endl << " \n" << endl << endl; } }; class SavAcc: public Account { private: double interest; // compound interest public: SavAcc(const string& s = "X", unsigned long n = 1111111L, double st = 0.0, double in = 0.0) : Account(s, n, st), interest(in) { } // Access methods. double getInterest() const { return interest; } void setInterest(double in){ interest = in; } void display() { Account::display(); cout << fixed << setprecision(2) << "Interest rate: " << interest << endl << " \n" << endl << endl; } }; #endif SOLUTIONS ■ 525 // // account_t.cpp // Tests the classes DepAcc and SavAcc // derived from class Account // #include "account.h" int main() { string s; double db; SavAcc mickey("Mickey Mouse", 1234567, 2.40, 3.5); mickey.display(); cout << "New name: "; getline(cin, s); cout << "New interest rate: "; cin >> db; mickey.setName(s); mickey.setInterest(db); mickey.display(); DepAcc dag("Donald Duck", 7654321, -1245.56, 10000, 12.9); dag.display(); cout << "New limit: "; cin >> db; dag.setLimit(db); dag.display(); return 0; } 526 ■ CHAPTER 23 INHERITANCE Exercise 3 // // product.h : Defines the classes // Product, PrepackedFood, and FreshFood // #ifndef _PRODUCT_H #define _PRODUCT_H #include <iostream> #include <iomanip> #include <string> using namespace std; class Product { private: long bar; string name; public: Product(long b = 0L, const string& s = "") : bar(b), name(s) { } void setCode(long b) { bar = b; } long getCode() const { return bar; } void setName(const string& s){ name = s; } const string& getName() const { return name; } void scanner() { cout << "\nBarcode: "; cin >> bar; cout << "Name: "; cin >> name; cin.sync(); cin.clear(); } void printer() const { cout << "\n " << "\nBarcode: " << bar << "\nName: " << name << endl; } }; class PrepackedFood : public Product { private: double pce_price; SOLUTIONS ■ 527 public: PrepackedFood(double p = 0.0,long b = 0L, const string& s = "") : Product(b, s), pce_price(p) {} void setPrice(double p){ pce_price = p;} double getPrice()const { return pce_price; } void scanner() { Product::scanner(); cout << "Price per piece: "; cin >> pce_price; } void printer() const { Product::printer(); cout << fixed << setprecision(2) << "Price per piece: " << pce_price << endl; } }; class FreshFood : public Product { private: double wght; double lbs_price; public: FreshFood(double g = 0.0, double p = 0.0, long b = 0L, const string& s = "") : Product(b, s), wght(g), lbs_price(p) {} void setWght(double g) { wght = g;} double getWght()const { return wght; } void setPrice(double p) { lbs_price = p;} double getPrice()const { return lbs_price; } void scanner() { Product::scanner(); cout << "Weight(lbs): "; cin >> wght; cout << "Price/lbs: "; cin >> lbs_price; cin.sync(); cin.clear(); } void printer() const { Product::printer(); cout << fixed << setprecision(2) << "Price per Lbs: " << lbs_price << "\nWeight: " << wght << "\nTotal: " << lbs_price * wght << endl; } }; #endif 528 ■ CHAPTER 23 INHERITANCE // // product_t.cpp // Tests classes Product, PrepackedFood, and FreshFood. // #include "product.h" int main() { Product p1(12345L, "Flour"), p2; p1.printer(); // Output the first product p2.setName("Sugar"); // Set the data members p2.setCode(543221); p2.printer(); // Output the second product // Prepacked products: PrepackedFood pf1(0.49, 23456, "Salt"), pf2; pf1.printer(); // Output the first // prepacked product cout << "\n Input data of a prepacked product: "; pf2.scanner(); // Input and output pf2.printer(); // data of 2nd product FreshFood pu1(1.5, 1.69, 98765, "Grapes"), pu2; pu1.printer(); // Output first item // fresh food cout <<"\n Input data for a prepacked product: "; pu2.scanner(); // Input and output pu2.printer(); // data of 2nd product. cout << "\n " << "\n " << "\nAgain in detail: \n" << fixed << setprecision(2) << "\nBarcode: " << pu2.getCode() << "\nName: " << pu2.getName() << "\nPrice per Lbs: " << pu2.getPrice() << "\nWeight: " << pu2.getWght() << "\nEnd price: " << pu2.getPrice() * pu2.getWght() << endl; return 0; } . EXERCISES ■ 519 Exercise 3 A supermarket chain has asked you to develop an automatic checkout system. All products are identifiable by means of a barcode and the product name. Groceries are either sold in packages. and organize them hierarchically.The Product class, which contains generic information on all products (barcode, name, etc.), can be used as a base class. ■ The Product class contains two data. data members of type long used for storing barcodes and the product name. Define a constructor with parameters for both data members.Add default values for the para- meters to provide a default

Ngày đăng: 06/07/2014, 17:21

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan