In addition to access methods, also define the method display, which outputs the data members of an apartment.. ■ Define a class derived from the CarandHomeclasses called MotorHome, whic
Trang 1Exercise 1
The multiply-derived class MotorHomeneeds to be fully implemented and tested.
■ Define an enumeration type CATEGORYthat represents the categories
“Luxury,” “First Class,” “Middle Class,” and “Economy”.
■ Develop a class called Homewith 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 CarandHomeclasses called MotorHome, which is used to represent motorhomes Inheritance of publicbase classes is used.The MotorHomeclass contains a new data member used to store one value of the CATEGORYtype 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 HomeandMotorHomeclasses in a separate header file, which includes the existing header file car.h.
■ Write a mainfunction that first fully initializes a MotorHometype object and then outputs the object.
Then create a second instance of the MotorHometype without initial val-ues and display the object on screen Call all the set methods in the MotorHomeclass and its base classes to set your own values for the objects.Then output the object once more.
Trang 2600 C H A P T E R 2 7 M U L T I P L E I N H E R I T A N C E
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)
Trang 3Exercise 2
Now fully define the SUVclass for testing virtual base classes.
■ Change the definition of the PassCarclass in the car.hheader file to make Cara virtual base class of the PassCarclass.
■ Then define the Vanclass using the Carclass 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
methoddisplay(), you still need to define methods for screen output.
■ Create the class SUV, which is derived from PassCarandVan, to repre-sent a station wagon Store the number of seats available in the station wagon as a data member.
The constructor in the SUVclass 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 mainfunction to test the SUVclass; the function should create sta-tion wagons with and without default values and display them on screen.
Trang 4602 C H A P T E R 2 7 M U L T I P L E I N H E R I T A N C E
■ 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; }
};
Trang 5class 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();
Trang 6604 C H A P T E R 2 7 M U L T I P L E I N H E R I T A N C E
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;
Trang 7Van(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
Trang 8606 C H A P T E R 2 7 M U L T I P L E I N H E R I T A N C E
// -// 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;
}
Trang 96 0 7
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
Trang 10608 C H A P T E R 2 8 E X C E P T I O N H A N D L I N G
■ 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;
}