// tv.h -- Tv and Remote classes#ifndef TV_H_#define TV_H_classTv{public:friendclassRemote;// Remote can access Tv private partsenum{Off,On};enum{MinVal,MaxVal=20};enum{Antenna,Cable};enum{TV,DVD};Tv(ints=Off,intmc=125):state(s),volume(5),maxchannel(mc),channel(2),mode(Cable),input(TV){}voidonoff(){state=(state==On)?Off:On;}boolison()const{returnstate==On;}boolvolup();boolvoldown();voidchanup();voidchandown();voidset_mode(){mode=(mode==Antenna)?Cable:Antenna;}voidset_input(){input=(input==TV)?DVD:TV;}voidsettings()const;// display all settingsprivate:intstate;// on or offintvolume;// assumed to be digitizedintmaxchannel;// maximum number of channelsintchannel;// current channel settingintmode;// broadcast or cableintinput;// TV or DVD};classRemote{private:intmode;// controls TV or DVDpublic:Remote(intm=Tv::TV):mode(m){}boolvolup(Tv&t){returnt.volup();}boolvoldown(Tv&t){returnt.voldown();}voidonoff(Tv&t){t.onoff();}voidchanup(Tv&t){t.chanup();}voidchandown(Tv&t){t.chandown();}voidset_chan(Tv&t,intc){t.channel=c;}voidset_mode(Tv&t){t.set_mode();}voidset_input(Tv&t){t.set_input();}};#endif
// tv.cpp -- methods for the Tv class (Remote methods are inline)#include<iostream>#include"tv.h"boolTv::volup(){if(volume<MaxVal){volume++;returntrue;}elsereturnfalse;}boolTv::voldown(){if(volume>MinVal){volume--;returntrue;}elsereturnfalse;}voidTv::chanup(){if(channel<maxchannel)channel++;elsechannel=1;}voidTv::chandown(){if(channel>1)channel--;elsechannel=maxchannel;}voidTv::settings()const{usingstd::cout;usingstd::endl;cout<<"TV is "<<(state==Off?"Off":"On")<<endl;if(state==On){cout<<"Volume setting = "<<volume<<endl;cout<<"Channel setting = "<<channel<<endl;cout<<"Mode = "<<(mode==Antenna?"antenna":"cable")<<endl;cout<<"Input = "<<(input==TV?"TV":"DVD")<<endl;}}
//error1.cpp -- using the abort() function#include<iostream>#include<cstdlib>doublehmean(doublea,doubleb);intmain(){doublex,y,z;std::cout<<"Enter two numbers: ";while(std::cin>>x>>y){z=hmean(x,y);std::cout<<"Harmonic mean of "<<x<<" and "<<y<<" is "<<z<<std::endl;std::cout<<"Enter next set of numbers <q to quit>: ";}std::cout<<"Bye!\n";return0;}doublehmean(doublea,doubleb){if(a==-b){std::cout<<"untenable arguments to hmean()\n";std::abort();}return2.0*a*b/(a+b);}result:Entertwonumbers:1-1untenableargumentstohmean()
//error2.cpp -- returning an error code#include<iostream>#include<cfloat> // (or float.h) for DBL_MAXboolhmean(doublea,doubleb,double*ans);intmain(){doublex,y,z;std::cout<<"Enter two numbers: ";while(std::cin>>x>>y){if(hmean(x,y,&z))std::cout<<"Harmonic mean of "<<x<<" and "<<y<<" is "<<z<<std::endl;elsestd::cout<<"One value should not be the negative "<<"of the other - try again.\n";std::cout<<"Enter next set of numbers <q to quit>: ";}std::cout<<"Bye!\n";return0;}boolhmean(doublea,doubleb,double*ans){if(a==-b){*ans=DBL_MAX;returnfalse;}else{*ans=2.0*a*b/(a+b);returntrue;}}
// error3.cpp -- using an exception#include<iostream>doublehmean(doublea,doubleb);intmain(){doublex,y,z;std::cout<<"Enter two numbers: ";while(std::cin>>x>>y){try{// start of try blockz=hmean(x,y);}// end of try blockcatch(constchar*s)----------------------------这里constchar*与“人定特征”string吻合{// start of exception handler std::cout<<s<<std::endl;std::cout<<"Enter a new pair of numbers: ";continue;}// end of handlerstd::cout<<"Harmonic mean of "<<x<<" and "<<y<<" is "<<z<<std::endl;std::cout<<"Enter next set of numbers <q to quit>: ";}std::cout<<"Bye!\n";return0;}doublehmean(doublea,doubleb){if(a==-b)throw"bad hmean() arguments: a = -b not allowed";----------------------------碰到异常时将“人定特征”设置为这个string语句return2.0*a*b/(a+b);}
// exc_mean.h -- exception classes for hmean(), gmean()#include<iostream>classbad_hmean{private:doublev1;doublev2;public:bad_hmean(doublea=0,doubleb=0):v1(a),v2(b){}voidmesg();};inlinevoidbad_hmean::mesg(){std::cout<<"hmean("<<v1<<", "<<v2<<"): "<<"invalid arguments: a = -b\n";}classbad_gmean{public:doublev1;doublev2;bad_gmean(doublea=0,doubleb=0):v1(a),v2(b){}constchar*mesg();};inlineconstchar*bad_gmean::mesg(){return"gmean() arguments should be >= 0\n";}
//error4.cpp � using exception classes#include<iostream>#include<cmath> // or math.h, unix users may need -lm flag#include"exc_mean.h"// function prototypesdoublehmean(doublea,doubleb);doublegmean(doublea,doubleb);intmain(){usingstd::cout;usingstd::cin;usingstd::endl;doublex,y,z;cout<<"Enter two numbers: ";while(cin>>x>>y){try{// start of try blockz=hmean(x,y);cout<<"Harmonic mean of "<<x<<" and "<<y<<" is "<<z<<endl;cout<<"Geometric mean of "<<x<<" and "<<y<<" is "<<gmean(x,y)<<endl;cout<<"Enter next set of numbers <q to quit>: ";}// end of try blockcatch(bad_hmean&bg)// start of catch block{bg.mesg();cout<<"Try again.\n";continue;}catch(bad_gmean&hg){cout<<hg.mesg();cout<<"Values used: "<<hg.v1<<", "<<hg.v2<<endl;cout<<"Sorry, you don't get to play any more.\n";break;}// end of catch block}cout<<"Bye!\n";// cin.get();// cin.get();return0;}doublehmean(doublea,doubleb){if(a==-b)throwbad_hmean(a,b);return2.0*a*b/(a+b);}doublegmean(doublea,doubleb){if(a<0||b<0)throwbad_gmean(a,b);returnstd::sqrt(a*b);}