第15章 友元、异常和其他

1.将类作为友元,则友元类的所有方法都可以访问原始类的私有成员和保护成员。

#ifndef TV_H_
#define TV_H_
class Tv
{
private:
    int state;
    int volume;
    int maxchannel;
    int channel;
    int mode;
    int input;
public:
    friend class Remote;
    enum{Off,On};
    enum{MinVal,MaxVal=20};
    enum{Antenna,Cable};
    enum{TV,DVD};
    Tv();             //初始化参数 其中一些用枚举类型进行初始化
    void onoff();
    bool ison();
    bool volup();
    bool voldown();
    void chanup();
    void chandown();
    void set_mode();
    void set_input();
    void settings();
};
class Remote
{
private:
    int mode;
public:
    Remote();
    bool volup(Tv &t);
    bool voldown(Tv &t);
    void onoff(Tv &t);
    void chanup(Tv &t);
    void chandown(Tv &t);
    void set_chan(Tv &t,int c);
    void set_mode(Tv &t);
    void set_input(Tv &t);
};
#endif  TV_H_
#include"tv.h"
#include<iostream>
using namespace std;
Tv::Tv()             //初始化参数 其中一些用枚举类型进行初始化
{
    state=Off;
    volume=5;
    maxchannel=125;
    channel=12;
    mode=Cable;
    input=TV;
}
void Tv::onoff()
{
    state=(state==On)?Off:On;
}
bool Tv::ison()
{
    return state==On;
}
bool Tv::volup()
{
    if(volume<MaxVal)
    {
        volume++;
        return true;
    }
    else
        return false;
}
bool Tv::voldown()
{  if(volume>MinVal)
    {
        volume--;
        return true;
    }
    else
        return false;
}
void Tv::chanup()
{
    if(channel<maxchannel)
      channel++;
    else
        channel=1;
}
void Tv::chandown()
{
    if(channel>1)
      channel--;
    else
        channel=maxchannel;
}
void Tv::set_mode()
{
    mode=(mode==Antenna)?Cable:Antenna;
}
void Tv::set_input()
{
    input=(input==TV)?DVD : TV;
}
void Tv::settings()
{
    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;
    }
}
Remote::Remote()
{
    mode=Tv::TV;
}
bool Remote::volup(Tv &t)
{
    return t.volup();
}
bool Remote::voldown(Tv &t)
{
    return t.voldown();
}
void Remote::onoff(Tv &t)
{
    t.onoff();
}
void Remote::chanup(Tv &t)
{
    t.chanup();
}
void Remote::chandown(Tv &t)
{
    t.chandown();
}
void Remote::set_chan(Tv &t,int c)
{
    t.channel=c;   //如果不是友元类 则不能访问Tv 的channel属性
}
void Remote::set_mode(Tv &t)
{
    t.set_mode();
}
void Remote::set_input(Tv &t)
{
    t.set_input();
}
#include<iostream>
#include"tv.h"
using namespace std;
int main()
{
    Tv s42;
    cout<<"Initial settings for 42\" TV: \n ";
    s42.settings();
    s42.onoff();
    s42.chanup();
    cout<<"\nAdjusted settings for 42\" TV: \n";
    s42.settings();
    s42.chanup();
    cout<<"\nAdjusted settings for 42\" TV: \n";
    s42.settings();

    Remote grey;
    grey.set_chan(s42,10);
    grey.volup(s42);
    cout<<"\nAdjusted settings for 42\" TV: \n";
    s42.settings();
    return 0;
}

2.上一个例子只有将channel的设置作为友元,并不需要将整个类作为友元。
将set_chan()作为友元:

class Tv
{
 friend void Remote::set_chan(Tv &t,int c);
 ....
}

class Tv;
class Remote{….};//只写方法的声明 而不定义它们,因为里面用到了
class Tv{….};
3.异常:

#include<iostream>
#include<cstring>
using namespace std;
double hmean(double a,double b);
int main()
{
    double x,y,r;
    cout<<"Enter two numbers:";
    while(cin>>x>>y)
    {
        r=hmean(x,y);
        cout<<"Result is:"<<r<<endl;
    }
    cout<<"Bye!"<<endl;
    return 0;
}
double hmean(double a,double b)
{
    if((a+b)==0)
    {   cout<<"error!";
        abort();
    }
    return 2.0*a*b/(a+b);
}

使用abort的情况
不使用abort的情况
3.将对象当做异常类型:

#include<iostream>
#include<string>
using namespace std;
class bad_hmean
{
private:
    double v1;
    double v2;
public:
    bad_hmean(double a,double b)
    {
        v1=a;
        v2=b;
    }
    void message()
    {
        cout<<"hmean("<<v1<<","<<v2<<"):"<<"invalid arguments:a=-b\n";
    }
};
class bad_gmean
{
private:
    double v1;
    double v2;
public:
    bad_gmean(double a,double b)
    {
        v1=a;
        v2=b;
    }
   string mesg()     //char *mesg() 也可以 ! char * 代表字符串
    {
        return "gmean() arguments should be >=0\n";
    }
};
#include<iostream>
#include<cmath>
#include<string>
#include"exc_mean.h"
using namespace std;
double hmean(double a,double b);
double gmean(double a,double b);
int main()
{
    double x,y,z;
    cout<<"Enter two number:";
    while(cin>>x>>y)
    {
        try
        {
            z=hmean(x,y);
            cout<<x<<" hmean "<<y<<"is:"<<z;
            z=gmean(x,y);
            cout<<x<<" gmean "<<y<<"is:"<<z;
        }
        catch(bad_hmean &bh)
        {
            bh.message();
            cout<<"Enter two number:";
            continue;
        }
        catch(bad_gmean &bg)
        {
            cout<<bg.mesg();
            cout<<"Enter two number:";
            continue;
        }
    }
    cout<<"Bye!"<<endl;
    return 0;
}
double hmean(double a,double b)
{
    if(a==-b)
        throw bad_hmean(a,b);
    else
        return 2.0*a*b/(a+b);
}
double gmean(double a,double b)
{
    if(a<0||b<0)
        throw bad_gmean(a,b);
    else
        return sqrt(a*b);
}
  1. 4.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值