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
请按任意键继续. . .
复制代码
相关推荐
czy87874752 小时前
深入了解 C++ 中的 `std::bind` 函数
开发语言·c++
我在人间贩卖青春2 小时前
C++之继承的方式
c++·private·public·protected·继承方式
驭渊的小故事2 小时前
简单模板笔记
数据结构·笔记·算法
野犬寒鸦3 小时前
从零起步学习并发编程 || 第七章:ThreadLocal深层解析及常见问题解决方案
java·服务器·开发语言·jvm·后端·学习
陈桴浮海3 小时前
【Linux&Ansible】学习笔记合集二
linux·学习·ansible
xhbaitxl4 小时前
算法学习day39-动态规划
学习·算法·动态规划
智者知已应修善业4 小时前
【洛谷P9975奶牛被病毒传染最少数量推导,导出多样例】2025-2-26
c语言·c++·经验分享·笔记·算法·推荐算法
Trouvaille ~4 小时前
【Linux】应用层协议设计实战(一):自定义协议与网络计算器
linux·运维·服务器·网络·c++·http·应用层协议
ZH15455891314 小时前
Flutter for OpenHarmony Python学习助手实战:数据库操作与管理的实现
python·学习·flutter
Junlan274 小时前
Cursor使用入门及连接服务器方法(更新中)
服务器·人工智能·笔记