2023-08-01 22:54:37 +08:00
|
|
|
|
/********************************************************************
|
|
|
|
|
假设有一个基类 Animal 和两个派生类 Cat 和 Dog,它们的定义如下 : class Animal
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual ~Animal() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Cat : public Animal
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
void meow()
|
|
|
|
|
{
|
|
|
|
|
std::cout << "Meow!" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Dog : public Animal
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
void bark()
|
|
|
|
|
{
|
|
|
|
|
std::cout << "Woof!" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
请完成以下任务:
|
|
|
|
|
|
|
|
|
|
1) 在 main 函数中创建一个 Animal 类型的指针,并使用 dynamic_cast 将其转换为 Cat 类型的指针。如果转换成功,调用 meow 函数让猫发出"Meow!"声音;如果转换失败,输出转换失败的提示信息。
|
|
|
|
|
|
|
|
|
|
2) 在 main 函数中创建一个 Animal 类型的指针,并使用 dynamic_cast 将其转换为 Dog 类型的指针。如果转换成功,调用 bark 函数让狗发出"Woof!"声音;如果转换失败,输出转换失败的提示信息。
|
|
|
|
|
|
|
|
|
|
3) 在 main 函数中创建一个 Cat 类型的指针,并使用 dynamic_cast 将其转换为 Animal 类型的指针。输出转换成功的提示信息。
|
|
|
|
|
|
|
|
|
|
4) 在 main 函数中创建一个 Dog 类型的指针,并使用 dynamic_cast 将其转换为 Animal 类型的指针。输出转换成功的提示信息。
|
|
|
|
|
|
|
|
|
|
【提示】可以尝试
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
catch (int e)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
异常尝试捕获语法确定转换是否成功。
|
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
2023-08-03 09:30:34 +08:00
|
|
|
|
#include <typeinfo>
|
2023-08-01 22:54:37 +08:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
class Animal
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual ~Animal() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Cat : public Animal
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
void meow()
|
|
|
|
|
{
|
|
|
|
|
cout << "Meow!" << endl;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Dog : public Animal
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
void bark()
|
|
|
|
|
{
|
|
|
|
|
cout << "Woof!" << endl;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
Animal *ap = new Animal(); // 创建一个 Animal 类型的指针
|
2023-08-03 09:30:34 +08:00
|
|
|
|
|
2023-08-01 22:54:37 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Cat *cp = dynamic_cast<Cat *>(ap); // 将其转换为 Cat 类型的指针
|
2023-08-03 09:30:34 +08:00
|
|
|
|
if (cp)
|
|
|
|
|
cp->meow();
|
|
|
|
|
else
|
|
|
|
|
throw 1;
|
2023-08-01 22:54:37 +08:00
|
|
|
|
}
|
2023-08-03 09:30:34 +08:00
|
|
|
|
catch (int &e)
|
2023-08-01 22:54:37 +08:00
|
|
|
|
{
|
2023-08-03 09:30:34 +08:00
|
|
|
|
cout << "dynamic_cast 将 Animal 转换为 Cat 失败" << endl;
|
2023-08-01 22:54:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Animal *ap2 = new Animal(); // 创建一个 Animal 类型的指针
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Dog *dp = dynamic_cast<Dog *>(ap2); // 将其转换为 Dog 类型的指针
|
2023-08-03 09:30:34 +08:00
|
|
|
|
if (dp)
|
|
|
|
|
dp->bark();
|
|
|
|
|
else
|
|
|
|
|
throw 1;
|
2023-08-01 22:54:37 +08:00
|
|
|
|
}
|
2023-08-03 09:30:34 +08:00
|
|
|
|
catch (int &e)
|
2023-08-01 22:54:37 +08:00
|
|
|
|
{
|
2023-08-03 09:30:34 +08:00
|
|
|
|
cout << "dynamic_cast 将 Animal 转换为 Dog 失败" << endl;
|
2023-08-01 22:54:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Cat *cp2 = new Cat(); // 创建一个 Cat 类型的指针
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Animal *ap3 = dynamic_cast<Animal *>(cp2); // 将其转换为 Animal 类型的指针
|
2023-08-03 09:30:34 +08:00
|
|
|
|
if (ap3)
|
|
|
|
|
cout << "dynamic_cast 将 Cat 转换为 Animal 成功" << endl;
|
|
|
|
|
else
|
|
|
|
|
throw 1;
|
2023-08-01 22:54:37 +08:00
|
|
|
|
}
|
2023-08-03 09:30:34 +08:00
|
|
|
|
catch (int &e)
|
2023-08-01 22:54:37 +08:00
|
|
|
|
{
|
2023-08-03 09:30:34 +08:00
|
|
|
|
cout << "dynamic_cast 将 Cat 转换为 Animal 失败" << endl;
|
2023-08-01 22:54:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dog *dp2 = new Dog(); // 创建一个 Dog 类型的指针
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Animal *ap4 = dynamic_cast<Animal *>(dp2); // 将其转换为 Animal 类型的指针
|
2023-08-03 09:30:34 +08:00
|
|
|
|
if (ap4)
|
|
|
|
|
cout << "dynamic_cast 将 Dog 转换为 Animal 成功" << endl;
|
|
|
|
|
else
|
|
|
|
|
throw 1;
|
2023-08-01 22:54:37 +08:00
|
|
|
|
}
|
2023-08-03 09:30:34 +08:00
|
|
|
|
catch (int &e)
|
2023-08-01 22:54:37 +08:00
|
|
|
|
{
|
2023-08-03 09:30:34 +08:00
|
|
|
|
cout << "dynamic_cast 将 Dog 转换为 Animal 失败" << endl;
|
2023-08-01 22:54:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|