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 小时前
Qt C++ 插件开发指南:插件架构设计与动态加载实战
开发语言·c++·qt
π同学1 小时前
遗传算法学习一之求函数的最值
学习
灰灰勇闯IT1 小时前
RN性能优化实战:从卡顿到丝滑的进阶之路
学习·性能优化
CC.GG1 小时前
【C++】STL容器----map和set的使用
开发语言·c++
CS Beginner1 小时前
【单片机】orange prime pi开发板与单片机的区别
笔记·嵌入式硬件·学习
八个程序员1 小时前
汉字古诗生成c++
开发语言·c++
zore_c1 小时前
【C语言】数据结构——顺序表超详解!!!(包含顺序表的实现)
c语言·开发语言·数据结构·c++·经验分享·笔记·线性回归
l木本I1 小时前
OpenArm开源项目总结(移植lerobot框架)
c++·人工智能·python·机器人
乌萨奇也要立志学C++1 小时前
【Linux】线程控制 POSIX 线程库详解与 C++ 线程库封装实践
linux·c++