书籍: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
请按任意键继续. . .