A Complete Guide to Programming in C++ part 63 pps

10 229 0
A Complete Guide to Programming in C++ part 63 pps

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

Thông tin tài liệu

EXERCISES ■ 599 Exercise 1 The multiply-derived class MotorHome needs to be fully implemented and tested. ■ Define an enumeration type CATEGORY that represents the categories “Luxury,” “First Class,” “Middle Class,” and “Economy”. ■ Develop a class called Home with two data members used to store the number of rooms and the size in square meters. Supply default values for your constructor definition to create a default constructor. In addition to access methods, also define the method display(), which outputs the data members of an apartment. ■ Define a class derived from the Car and Home classes called MotorHome, which is used to represent motorhomes. Inheritance of public base classes is used.The MotorHome class contains a new data member used to store one value of the CATEGORY type. In addition to defining a construc- tor with default values, also define appropriate access methods and a display() method for output. Place your definitions of the Home and MotorHome classes in a separate header file, which includes the existing header file car.h. ■ Write a main function that first fully initializes a MotorHome type object and then outputs the object. Then create a second instance of the MotorHome type without initial val- ues and display the object on screen. Call all the set methods in the MotorHome class and its base classes to set your own values for the objects.Then output the object once more. 600 ■ CHAPTER 27 MULTIPLE INHERITANCE Class hierarchy for the multiply-derived class SUV Car Data members: car number producer SUV Data members: number of seats PassCar Data members: car type sun roof (y/n) Van Data members: capacity (lbs) EXERCISES ■ 601 Exercise 2 Now fully define the SUV class for testing virtual base classes. ■ Change the definition of the PassCar class in the car.h header file to make Car a virtual base class of the PassCar class. ■ Then define the Van class using the Car class as a virtual base class.The new class should contain an additional data member used to represent the payload of the van in kilograms.A maximum of 750 kg applies to vans. The constructor should use default values to initialize the data members with defaults, thus providing a default constructor for the class.A maxi- mum value of 750 applies for the payload. In addition to the access method display(), you still need to define methods for screen output. ■ Create the class SUV, which is derived from PassCar and Van, to repre- sent a station wagon. Store the number of seats available in the station wagon as a data member. The constructor in the SUV class should use the base initializer to set all the values of a station wagon using default values for every data member. Additionally, define access methods and a display() to define output. ■ Use a main function to test the SUV class; the function should create sta- tion wagons with and without default values and display them on screen. solutions 602 ■ CHAPTER 27 MULTIPLE INHERITANCE ■ SOLUTIONS Exercise 1 // // car.h : Definition of base class Car and // derived classes PassCar and Truck // // car.cpp // Implementing methods of Car, PassCar, and Truck // // // These files have been left unchanged // from Chapters 23 and 25. // // // motorHome.h : Definition of the class Home and the // multiply-derived class MotorHome // #ifndef _MOTORHOME_H_ #define _MOTORHOME_H_ #include "car.h" #include <iomanip> #include <iostream> using namespace std; enum CATEGORY {LUXURY, FIRSTCLASS, SECONDCLASS, ECONOMY}; class Home { private: int room; double ft2; public: Home(int r = 0, double m2 = 0.0) { room = r; ft2 = m2;} void setRoom(int n){ room = n;} int getRoom() const { return room; } void setSquareFeet(double m2){ ft2 = m2;} double getSquareFeet() const { return ft2; } void display() const { cout << "Number of rooms: " << room << "\nSquare feet: " << fixed << setprecision(2) << ft2 << endl; } }; SOLUTIONS ■ 603 class MotorHome : public Car, public Home { private: CATEGORY cat; public: MotorHome(long n=0L, const string& prod="", int ro=0, double m2=0.0, CATEGORY k=ECONOMY) : Car(n, prod), Home(ro, m2), cat(k) {} void setCategory(CATEGORY c){cat = c;} CATEGORY getCategory() const { return cat;} void display() const { cout << "\nMotorHome: "; Car::display(); Home::display(); cout << "Category: "; switch(cat) { case LUXURY: cout << " Luxury"; break; case FIRSTCLASS: cout << " First class"; break; case SECONDCLASS: cout << " Second class"; break; case ECONOMY: cout << " Economy"; break; } cout << endl; } }; #endif // // motorHome_t.cpp // Testing the multiply-derived class MotorHome // #include "motorHome.h" int main() { MotorHome rv(12345L, "Texaco", 2, 40.5, LUXURY); rv.display(); MotorHome holiday; holiday.display(); // Default values cin.get(); 604 ■ CHAPTER 27 MULTIPLE INHERITANCE holiday.setNr(54321); holiday.setProd("VW"); holiday.setRoom(1); holiday.setSquareFeet(11.5); holiday.setCategory(SECONDCLASS); holiday.display(); return 0; } Exercise 2 // // car.h : Definition of base class Car and // the derived classes PassCar and Truck // // car.cpp // Implementing the methods of Car, PassCar, and Truck // // // These files are carried over from Chapter 23 and 25, // with the following changes: // // Class Car is a virtual base class now class PassCar : public virtual Car { // }; class Truck : public virtual Car { // }; // // suv.h : Defines the class Van and // the multiply-derived class SUV // #ifndef _SUV_H #define _SUV_H #include "car.h" class Van : public virtual Car { private: double capacity; SOLUTIONS ■ 605 public: Van(long n=0L, const string& prod="", double l=0.0) : Car(n,prod) { if(l > 750) l = 750; capacity = l; } void setCapacity(double l) { if(l > 750) capacity= 750; else capacity = l; } double getCapacity() const { return capacity; } void display() const { cout << "Capacity: " << capacity << " kg" << endl; } }; class SUV : public PassCar, public Van { private: int cnt; // Number of seats public: SUV(const string& tp="without type", bool sb=false, long n=0L, const string& prod=" none ", double l=0.0, int z = 1) : PassCar(tp,sb), Car(n,prod), Van(n,prod,l), cnt(z) { } void display() const { PassCar::display(); Van::display(); cout << "Number of seats: " << cnt << endl; } }; #endif 606 ■ CHAPTER 27 MULTIPLE INHERITANCE // // suv_t.cpp : Tests the class SUV // #include "suv.h" int main() { SUV mobil("Bravada", true, 120345, "Oldsmobile",350,6); mobil.display(); SUV trucky; trucky.display(); trucky.setNr(543221); trucky.setProd("Renault"); trucky.setCapacity(1000.); trucky.display(); return 0; } 607 Exception Handling This chapter describes how a C++ program uses error-handling techniques to resolve error conditions. In addition to throwing and catching exceptions, we also examine how exception specifications are declared and exception classes are defined, additionally looking into the use of standard exception classes. chapter 28 608 ■ CHAPTER 28 EXCEPTION HANDLING ■ TRADITIONAL ERROR HANDLING Error checking after leaving a function First calling function Second calling function Third calling function Called function . . . { . . . if(func()>0) // every- // thing // is ok else exit(-1); . . . } . . . { . . . if(func()<=0) // All errors // are // handled . . . } . . . { . . x= func(); if(x == 0) //1. error else if (x == -1) //2. error } int func( void) { . . . if(dilemma) return 0; . . . if(catastrophe) return -1; . . . } . file to make Car a virtual base class of the PassCar class. ■ Then define the Van class using the Car class as a virtual base class.The new class should contain an additional data member used to. PassCar and Van, to repre- sent a station wagon. Store the number of seats available in the station wagon as a data member. The constructor in the SUV class should use the base initializer to. set all the values of a station wagon using default values for every data member. Additionally, define access methods and a display() to define output. ■ Use a main function to test the SUV class;

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

Từ khóa liên quan

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

  • Đang cập nhật ...

Tài liệu liên quan