C++ Prime Plus 学习笔记037

书籍:C++ Primer Plus (第六版)(中文版)

工具:Dev-C++ 5.11

电脑信息:Intel® Xeon® CPU E5-2603 v3 @ 1.60GHz

系统类型:64位操作系统,基于X64的处理器 windows10 专业版

第15章 友元、异常和其它

15.3 异常

实例15.10

sales.h

sales.cpp

use_sales.cpp

之前编译失败,是因为没有建project。新建project,将三个文件添加进去

编译运行结果:

15.4 RTTI

实例15.17

rtti1.cpp

cpp 复制代码
// rtti1.cpp -- using the RTTI dynamic_cast operator
#include<iostream>
#include<cstdlib>
#include<ctime>

using std::cout;

class Grand{
private:
    int hold;
public:
    Grand(int h = 0) : hold(h) {}
    virtual void Speak() const { cout << "I am a grand class!\n"; }
    virtual int Value() const { return hold; }
};

class Superb : public Grand {
public:
    Superb(int h = 0) : Grand(h) {}
    void Speak() const { cout << "I am a superb class!!\n"; }
    virtual void Say() const{
        cout << "I hold the superb value of " << Value() << "!\n";
    }
};

class Magnificent : public Superb{
private:
    char ch;
public:
    Magnificent(int h = 0, char c = 'A') : Superb(h), ch(c) {}
    void Speak() const { cout << "I am a magnificent class!!!\n"; }
    void Say() const { cout << "I hold the character " << ch << 
                    " and the integer " << Value() << "!\n"; }
};

Grand * GetOne();

int main(){
    std::srand(std::time(0));
    Grand * pg;
    Superb * ps;
    for (int i = 0; i < 5; i++){
        pg = GetOne();
        pg->Speak();
        if((ps = dynamic_cast<Superb *>(pg))){
            ps->Say();
        }
    }
    return 0;
}

Grand * GetOne(){   // generate one of three kinds of objects randomly
    Grand * p;
    switch (std::rand()%3) {
    case 0 : p = new Grand(std::rand() % 100);
                break;
    case 1 : p = new Superb(std::rand() % 100);
                break;
    case 2 : p = new Magnificent(std::rand() % 100, 
                            'A' + std::rand()%26);
                break;
    }
    return p;
}

编译运行结果:

cpp 复制代码
I am a magnificent class!!!
I hold the character X and the integer 13!
I am a grand class!
I am a magnificent class!!!
I hold the character C and the integer 97!
I am a magnificent class!!!
I hold the character Y and the integer 22!
I am a grand class!

--------------------------------
Process exited after 0.3622 seconds with return value 0
请按任意键继续. . .

实例15.18

rtti2.cpp

cpp 复制代码
// rtti2.cpp -- using dynamic_cast, typeid, and type_info

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<typeinfo>

using namespace std;

class Grand{
private:
    int hold;
public:
    Grand(int h = 0) : hold(h) {}
    virtual void Speak() const { cout << "I am a grand class!\n"; }
    virtual int Value() const { return hold; }
};

class Superb : public Grand{
public:
    Superb(int h = 0) : Grand(h) {}
    void Speak() const { cout << "I am a superb class!!\n"; }
    virtual void Say() const {
        cout << "I hold the superb value of " << Value() << "!\n";
    }
};

class Magnificent : public Superb{
private:
    char ch;
public:
    Magnificent(int h=0, char cv = 'A') : Superb(h), ch(cv) {}
    void Speak() const { cout << "I am a magnificent class!!!\n"; }
    void Say() const{
        cout << "I hold the character " << ch << " and the integer " << Value() << "!\n";
    }
};

Grand * GetOne();

int main(){
    srand(time(0));
    Grand * pg;
    Superb * ps;
    for (int i=0; i<5; i++){
        pg = GetOne();
        cout << "Now processing type " << typeid(*pg).name() << ".\n";
        pg->Speak();
        if ((ps = dynamic_cast<Superb *>(pg))){
            ps->Say();
        }
        if (typeid(Magnificent) == typeid(*pg)){
            cout << "Yes, you're really magnificent.\n";
        }
    }
    return 0;
}

Grand * GetOne(){
    Grand * p;
    switch(rand()%3){
        case 0: p = new Grand(rand()%100);
            break;
        case 1: p = new Superb(rand()%100);
            break;
        case 2: p = new Magnificent(rand()%100, 'A'+rand()%26);
            break;
    }
    return p;
}

编译运行结果:

cpp 复制代码
Now processing type 11Magnificent.
I am a magnificent class!!!
I hold the character A and the integer 1!
Yes, you're really magnificent.
Now processing type 6Superb.
I am a superb class!!
I hold the superb value of 31!
Now processing type 5Grand.
I am a grand class!
Now processing type 11Magnificent.
I am a magnificent class!!!
I hold the character I and the integer 4!
Yes, you're really magnificent.
Now processing type 6Superb.
I am a superb class!!
I hold the superb value of 50!

--------------------------------
Process exited after 0.3719 seconds with return value 0
请按任意键继续. . .

15.5 类型转换运算符

实例15.19

constcast.cpp

cpp 复制代码
// constcast.cpp -- using const_cast<>
#include <iostream>
using std::cout;
using std::endl;

void change(const int * pt, int n);

int main()
{
    int pop1 = 38383;
    const int pop2 = 2000;

    cout << "pop1, pop2: " << pop1 << ", " << pop2 << endl;
    change(&pop1, -103);
    change(&pop2, -103);
    cout << "pop1, pop2: " << pop1 << ", " << pop2 << endl;
    // std::cin.get();
    return 0;
}

void change(const int * pt, int n)
{
    int * pc;
  
    pc = const_cast<int *>(pt);
    *pc += n;
 
}

编译运行结果:

```cpp
pop1, pop2: 38383, 2000
pop1, pop2: 38280, 2000

--------------------------------
Process exited after 0.3696 seconds with return value 0
请按任意键继续. . .
复制代码
相关推荐
知识分享小能手1 分钟前
Flask入门学习教程,从入门到精通, 认识Flask路由 — 知识点详解 (2)
python·学习·flask
XS0301063 分钟前
MyBatis基础实战笔记一
笔记·mybatis
清平乐的技术专栏6 分钟前
【Flink学习】(六)Flink 三大时间语义 + 水位线 Watermark
大数据·学习·flink
vKd0Ff21L6 分钟前
如何在Dev-C++中设置TDM-GCC为默认编译器第九十一篇
java·jvm·c++
Oll Correct10 分钟前
实验二十五:从IPv4向IPv6过渡所使用的隧道技术
网络·笔记
cany100014 分钟前
C++ -- 型号比对和constexpr
c++
楼兰公子14 分钟前
《深入理解Linux网络技术内幕》配套学习大纲 + 源码Demo + 进阶实战实例
linux·arm开发·学习
楼田莉子14 分钟前
C++17新特性:结构化绑定/inline变量/if相关的变化
c++·后端·学习
翎沣19 分钟前
C++面向对象三大特性
开发语言·c++
无限进步_31 分钟前
【C++】C++11的类功能增强与STL变化
java·前端·数据结构·c++·后端·算法