C++ Primer Plus习题及答案-第十五章

习题选自:C++ Primer Plus(第六版)
内容仅供参考,如有错误,欢迎指正 !

友元类和嵌套类

RTTI和类型转换运算符

复习题

1. 下面建立友元的尝试有什么错误?

a.

class snap {
 friend clasp;
 ...
};
class clasp { ... };

b.

class cuff {
 public:
 void snip(muff &) { ... }
 ...
};
class muff {
 friend void cuff::snip(muff &);
 ...
};

c.

class muff {
friend void cuff::snip(muff &);
...
};
class cuff {
public:
void snip(muff &) { ... }
...
};

a. friend clasp;缺少class声明,应该改为 friend class clasp;

b. 缺少muff的前项声明,应该在第一行添加class muff

c. cuff类声明应在muff类之前,以便编译器可以理解cuff::snip( )。同时编译器需要muff的一个前向声明,以便可以理解snip(muff &)。修改后应该为:

class muff; // forward declaration
class cuff {
    public:
    void snip(muff &) { ... }
    ...
};
class muff {
    friend void cuff::snip(muff &);
    ...
};

2. 您知道了如何建立相互类友元的方法。能够创建一种更为严格的友情关系,即类B只有部分成员是类A的友元,而类A只有部分成员是类B的友元吗?请解释原因。

不能,为了使类B部分成员是类A的友元,需要将类B的声明位于类A的前面,并且要在类A指出类B中要作为类A友元的成员。同样的使类A部分成员成为类A的友元,需要同样的要求,而这两个要求是互斥的,因此无法创建该友情关系。

3. 下面的嵌套类声明中可能存在什么问题

class Ribs
{
 private:
 class Sauce
 {
     int soy;
     int sugar;
     public:
     Sauce(int s1, int s2) : soy(s1), sugar(s2) { }
 };
 ...
};

嵌套类Sauce中的soysugar都是私有的,Ribs只能通过Sauce的构造函数创建它们。

4. throwreturn之间的区别何在?

假设函数f1( )调用函数f2( )。f2( )中的返回语句导致程序执行在函数f1( )中调用函数f2( )后面的一条语句。throw语句导致程序沿函数调用的当前序列回溯,直到找到直接或间接包含对f2( )的调用的try语句块为止。它可能在f1( )中、调用f1( )的函数中或其他函数中。找到这样的try语句块后,将执行下一个匹配的catch语句块,而不是函数调用后的语句。

5. 假设有一个从异常基类派生来的异常类层次结构,则应按什么样的顺序放置catch块?

应按从子孙到祖先的顺序排列catch语句块。

6. 对于本章定义的Grand、SuperbMagnificent类,假设pgGrand *指针,并将其中某个类的对象的地址赋给了它,而ps为Superb *指针,则下面两个代码示例的行为有什么不同?

if (ps = dynamic_cast<Superb *>(pg))
ps->say(); // sample #1
if (typeid(*pg) == typeid(Superb))
(Superb *) pg)->say(); // sample #2

对于dynamic_cast(pg),如果pg指向一个Superb对象或从Superb派生而来的任何类的对象,则if条件为true。具体地说,如果pg指向Magnificent对象,则if条件也为true。

对于typeid(*pg) == typeid(Superb),仅当指向Superb对象时,if条件才为true,如果指向的是从Superb派生出来的对象,则if条件不为true

7. static_cast运算符与dynamic_cast运算符有什么不同?

dynamic_cast运算符只允许沿类层次结构向上转换,而static_cast运算符允许向上转换和向下转换。static_cast运算符还允许枚

举类型和整型之间以及数值类型之间的转换。

编程练习

1. 对TvRemote类做如下修改:

  • a. 让它们互为友元;
  • b. 在Remote类中添加一个状态变量成员,该成员描述遥控器是处于常规模式还是互动模式;
  • c. 在Remote中添加一个显示模式的方法;
  • d. 在Tv类中添加一个对Remote中新成员进行切换的方法,该方法应仅当TV处于打开状态时才能运行。

编写一个小程序来测试这些新特性。

tv.h:

#ifndef TV_H_
#define TV_H_

#include 

class Remote;  // forward declaration

class Tv {
    public:
    friend class Remote;
    enum { Off, On };
    enum { MinVal, MaxVal = 20 };
    enum { Antenna, Cable };
    enum { TV, DVD };

    private:
    int state;
    int volume;
    int maxchannel;
    int channel;
    int mode;
    int input;

    public:
    Tv(int s = Off, int mc = 125)
        : state(s),
    volume(5),
    maxchannel(mc),
    channel(2),
    mode(Cable),
    input(DVD) {}
    void onoff() { state = (state == On) ? Off : On; }
    bool ison() const { return state == On; }
    bool volup();
    bool voldown();
    void chanup();
    void chandown();
    void set_mode() { mode = (mode == Antenna) ? Cable : Antenna; }
    void set_input() { input = (input == TV) ? DVD : TV; }
    void show_settings() const;
    void set_remote_mode(Remote& r) const;
};

class Remote {
    public:
    friend class Tv;
    enum { Normal, Interactive };

    private:
    int mode;
    int gmode;

    public:
    Remote(int m = Tv::TV) : mode(m), gmode(Normal) {}
    bool volup(Tv& t) { return t.volup(); }
    bool voldown(Tv& t) { return t.voldown(); }
    void onoff(Tv& t) { t.onoff(); }
    void chanup(Tv& t) { t.chanup(); }
    void chandown(Tv& t) { t.chandown(); }
    void set_chan(Tv& t, int c) { t.channel = c; }
    void set_mode(Tv& t) { t.set_mode(); }
    void set_input(Tv& t) { t.set_input(); }
    void show_remote_mode() const;
    void set_remote_mode();
};

inline void Remote::show_remote_mode() const {
    std::cout << (gmode == Normal ? "Normal" : "Interactive") << std::endl;
}

inline void Remote::set_remote_mode() {
    gmode = (gmode == Normal) ? Interactive : Normal;
}

#endif  // TV_H_

tv.cpp:

#include "tv.h"

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::show_settings() const {
    std::cout << "TV is " << (state == On ? "On" : "Off") << std::endl;
    if (state == On) {
        std::cout << "Volume setting = " << volume << std::endl;
        std::cout << "Channel setting = " << channel << std::endl;
        std::cout << "Mode = " << (mode == Antenna ? "Antenna" : "Cable")
            << std::endl;
        std::cout << "Input = " << (input == TV ? "TV" : "DVD") << std::endl;
    }
}

void Tv::set_remote_mode(Remote& r) const {
    if (state == On) r.set_remote_mode();
}

main.cpp:

#include 

#include "tv.h"

int main() {
    Tv t;
    t.show_settings();

    Remote r;
    r.show_remote_mode();

    r.onoff(t);
    t.show_settings();

    r.voldown(t);
    t.show_settings();

    t.set_remote_mode(r);
    r.show_remote_mode();

    return 0;
}

2. 修改程序清单15.11,使两种异常类型都是从头文件提供的logic_error类派生出来的类。让每个what()方法都报告函数名和问题的性质。异常对象不用存储错误的参数值,而只需支持what()方法。

exc_mean.h:

#ifndef EXC_MEAN_H_
#define EXC_MEAN_H_

#include 

class bad_hmean : public std::logic_error {
    public:
    bad_hmean() : std::logic_error("hmean() invalid arguments: a = -b\n") {}
};

class bad_gmean : public std::logic_error {
    public:
    bad_gmean() : std::logic_error("gmean() arguments should be >= 0\n") {}
};

#endif  // EXC_MEAN_H_

error4.cpp:

// error4.cpp ?using exception classes
#include   // or math.h, unix users may need -lm flag
#include 

#include "exc_mean.h"
// function prototypes
double hmean(double a, double b);
double gmean(double a, double b);
int main() {
    using std::cin;
    using std::cout;
    using std::endl;

    double x, y, z;

    cout << "Enter two numbers: ";
    while (cin >> x >> y) {
        try {  // start of try block
            z = 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 : ";
        }                      // end of try block
        catch (bad_hmean& he)  // start of catch block
        {
            cout << he.what();
            cout << "Try again.\n";
            continue;
        } catch (bad_gmean& ge) {
            cout << ge.what();
            cout << "Sorry, you don't get to play any more.\n";
            break;
        }  // end of catch block
    }
    cout << "Bye!\n";
    return 0;
}

double hmean(double a, double b) {
    if (a == -b) throw bad_hmean();
    return 2.0 * a * b / (a + b);
}

double gmean(double a, double b) {
    if (a < 0 || b < 0) throw bad_gmean();
    return std::sqrt(a * b);
}

3. 这个练习与编程练习2相同,但异常类是从一个这样的基类派生而来的:它是从logic_error派生而来的,并存储两个参数值。异常类应该有一个这样的方法:报告这些值以及函数名。程序使用一个catch块来捕获基类异常,其中任何一种从该基类异常派生而来的异常都将导致循环结束。

exc_mean.h:

#ifndef EXC_MEAN_H_
#define EXC_MEAN_H_

#include 
#include 
#include 

class bad_base : public std::logic_error {
    private:
    double v1;
    double v2;
    std::string fun_name;

    public:
    bad_base(double a = 0, double b = 0, std::string name = "none")
        : std::logic_error("hmean() invalid arguments: a = -b\n"),
    v1(a),
    v2(b),
    fun_name(name) {}

    void mesg() {
        std::cout << v1 << ", " << v2 << ", " << fun_name << std::endl;
    }
};

class bad_hmean : public bad_base {
    public:
    bad_hmean(double a = 0, double b = 0, std::string name = "none")
        : bad_base(a, b, name) {}
};

class bad_gmean : public bad_base {
    public:
    bad_gmean(double a = 0, double b = 0, std::string name = "none")
        : bad_base(a, b, name) {}
};

#endif  // EXC_MEAN_H_

error4.cpp:

// error4.cpp ?using exception classes
#include   // or math.h, unix users may need -lm flag
#include 

#include "exc_mean.h"
// function prototypes
double hmean(double a, double b);
double gmean(double a, double b);
int main() {
    using std::cin;
    using std::cout;
    using std::endl;

    double x, y, z;

    cout << "Enter two numbers: ";
    while (cin >> x >> y) {
        try {  // start of try block
            z = 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 : ";
        }                      // end of try block
        catch (bad_hmean& he)  // start of catch block
        {
            he.mesg();
            cout << "Try again.\n";
            continue;
        } catch (bad_gmean& ge) {
            ge.mesg();
            cout << "Sorry, you don't get to play any more.\n";
            break;
        }  // end of catch block
        catch (bad_base& ge) {
            ge.mesg();
            cout << "Sorry, you don't get to play any more.\n";
            break;
        }  // end of catch block
    }
    cout << "Bye!\n";
    return 0;
}

double hmean(double a, double b) {
    if (a == -b) throw bad_hmean(a, b, "hmean()");
    return 2.0 * a * b / (a + b);
}

double gmean(double a, double b) {
    if (a < 0 || b < 0) throw bad_gmean(a, b, "gmean()");
    return std::sqrt(a * b);
}

4. 程序清单15.16在每个try后面都使用两个catch块,以确保nbad_index异常导致方法label_val( )被调用。请修改该程序,在每个try块后面只使用一个catch块,并使用RTTI来确保合适时调用label_val( )

sales.h:

#ifndef SALES_H_
#define SALES_H_

// sales.h  -- exceptions and inheritance
#include 
#include 

class Sales {
    public:
    enum { MONTHS = 12 };  // could be a static const
    class bad_index : public std::logic_error {
        private:
        int bi;  // bad index value
        public:
        explicit bad_index(int ix,
                           const std::string& s = "Index error in Sales object\n");
        int bi_val() const { return bi; }
        virtual ~bad_index() throw() {}
    };
    explicit Sales(int yy = 0);
    Sales(int yy, const double* gr, int n);
    virtual ~Sales() {}
    int Year() const { return year; }
    virtual double operator[](int i) const;
    virtual double& operator[](int i);

    private:
    double gross[MONTHS];
    int year;
};

class LabeledSales : public Sales {
    public:
    class nbad_index : public Sales::bad_index {
        private:
        std::string lbl;

        public:
        nbad_index(const std::string& lb, int ix,
                   const std::string& s = "Index error in LabeledSales object\n");
        const std::string& label_val() const { return lbl; }
        virtual ~nbad_index() throw() {}
    };
    explicit LabeledSales(const std::string& lb = "none", int yy = 0);
    LabeledSales(const std::string& lb, int yy, const double* gr, int n);
    virtual ~LabeledSales() {}
    const std::string& Label() const { return label; }
    virtual double operator[](int i) const;
    virtual double& operator[](int i);

    private:
    std::string label;
};

#endif  // SALES_H_

sales.cpp:

// sales.cpp -- Sales implementation
#include "sales.h"
using std::string;

Sales::bad_index::bad_index(int ix, const string& s)
    : std::logic_error(s), bi(ix) {}

Sales::Sales(int yy) {
    year = yy;
    for (int i = 0; i < MONTHS; ++i) gross[i] = 0;
}

Sales::Sales(int yy, const double* gr, int n) {
    year = yy;
    int lim = (n < MONTHS) ? n : MONTHS;
    int i;
    for (i = 0; i < lim; ++i) gross[i] = gr[i];
    // for i > n and i < MONTHS
    for (; i < MONTHS; ++i) gross[i] = 0;
}

double Sales::operator[](int i) const {
    if (i < 0 || i >= MONTHS) throw bad_index(i);
    return gross[i];
}

double& Sales::operator[](int i) {
    if (i < 0 || i >= MONTHS) throw bad_index(i);
    return gross[i];
}

LabeledSales::nbad_index::nbad_index(const string& lb, int ix, const string& s)
    : Sales::bad_index(ix, s) {
        lbl = lb;
    }

LabeledSales::LabeledSales(const string& lb, int yy) : Sales(yy) { label = lb; }

LabeledSales::LabeledSales(const string& lb, int yy, const double* gr, int n)
    : Sales(yy, gr, n) {
        label = lb;
    }

double LabeledSales::operator[](int i) const {
    if (i < 0 || i >= MONTHS) throw nbad_index(Label(), i);
    return Sales::operator[](i);
}

double& LabeledSales::operator[](int i) {
    if (i < 0 || i >= MONTHS) throw nbad_index(Label(), i);
    return Sales::operator[](i);
}

use_sales.cpp:

// use_sales.cpp  -- nested exceptions
#include 
#include 

#include "sales.h"

int main() {
    using std::cin;
    using std::cout;
    using std::endl;

    double vals1[12] = {1220, 1100, 1122, 2212, 1232, 2334,
                        2884, 2393, 3302, 2922, 3002, 3544};

    double vals2[12] = {12, 11, 22, 21, 32, 34, 28, 29, 33, 29, 32, 35};

    Sales sales1(2011, vals1, 12);
    LabeledSales sales2("Blogstar", 2012, vals2, 12);

    cout << "First try block:\n";
    try {
        int i;
        cout << "Year = " << sales1.Year() << endl;
        for (i = 0; i < 12; ++i) {
            cout << sales1[i] << ' ';
            if (i % 6 == 5) cout << endl;
        }
        cout << "Year = " << sales2.Year() << endl;
        cout << "Label = " << sales2.Label() << endl;
        for (i = 0; i <= 12; ++i) {
            cout << sales2[i] << ' ';
            if (i % 6 == 5) cout << endl;
        }
        cout << "End of try block 1.\n";
    } catch (Sales::bad_index &bad) {
        cout << bad.what();
        if (typeid(bad) == typeid(LabeledSales::nbad_index)) {
            cout << "Company: " << ((LabeledSales::nbad_index &)bad).label_val()
                << endl;
        }
        cout << "bad index: " << bad.bi_val() << endl;
    }
    cout << "\nNext try block:\n";
    try {
        sales2[2] = 37.5;
        sales1[20] = 23345;
        cout << "End of try block 2.\n";
    } catch (Sales::bad_index &bad) {
        cout << bad.what();
        if (typeid(bad) == typeid(LabeledSales::nbad_index)) {
            cout << "Company: " << ((LabeledSales::nbad_index &)bad).label_val()
                << endl;
        }
        cout << "bad index: " << bad.bi_val() << endl;
    }
    cout << "done\n";
    return 0;
}

你可能感兴趣的:(C++,Primer,Plus习题及答案,c++,算法)