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
请按任意键继续. . .
复制代码
相关推荐
apocelipes1 天前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
郝学胜_神的一滴3 天前
CMake 034:生成器表达式:解耦构建时序、精简分支逻辑的终极利器
c++·cmake
见过夏天3 天前
C++ 基础入门完全指南
c++
用户805533698035 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
RainCity5 天前
Java Swing 自定义组件库分享(十二)
java·笔记·后端
BadBadBad__AK5 天前
线段树维护区间 k 次方和
c++·数学·算法·stl
卷无止境6 天前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
卷无止境6 天前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
郝学胜_神的一滴7 天前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境9 天前
C++ 的Eigen 库全解析
c++