|
2鱼币
本帖最后由 吃肉的考拉 于 2014-3-31 10:29 编辑
#include <iostream>
#include <string>
class Company
{
public:
Company(std::string theName,std::string product);
virtual void printInfo(); //打印信息
protected:
std::string name;
std::string product;
};
class TeachCompany:public Company
{
public:
TeachCompany(std::string theName,std::string product);
virtual void printInfo();
};
Company::Company(std::string theName,std::string product)
{
name = theName;
this->product = product;
}
void Company::printInfo()
{
std::cout << "这个公司的名字叫:" << name << ",正在生产"
<< product << std::endl;
}
TeachCompany::TeachCompany(std::string theName,std::string product):Company(theName,product)
{
}
void TeachCompany::printInfo ()
{
std::cout << name << "公司大量生产了" << product << std::endl ;
}
int main()
{
//Company *company = new Company( "Apple" ,"iphone");
Company *company = new TeachCompany( "Apple","iphone");
TeachCompany *teachcompany = dynamic_cast<TeachCompany *>(company); //注意加括号
if(teachcompany != NULL)
{
std::cout << "OK\n";
}
else
{
std::cout << "Oh,on\n";
}
// teachcompany->printInfo ();
//两个指针变量指向了同一个地址
delete company;
company = NULL;
teachcompany = NULL;
return 0;
}
代码是我照着视频敲的,看视频是可以执行的,我在VC++6.0里却无法执行
|
最佳答案
查看完整内容
在VC6.0中使用显示的强制类型dynamic_cast的时候,需要在VC6.0中
进行设置才可以使用,其实这就是所谓的RTTI技术。
设置步骤为:
选择:工程(Project)->设置(Setting)->C++->分类->C++语言->
(勾选)允许时间类型信息(RTTI),然后确定就可以了。
这样你的程序就实现了。
|