C++学习,第三十七课
已有 711 次阅读2014-8-26 11:40
|个人分类:作业
#include<iostream>
#include<string>
class Company
{
public:
Company(std::string theName, std::string theProduct);
virtual void print();
protected:
std::string name;
std::string product;
};
class TecCompany :public Company
{
public:
TecCompany(std::string name, std::string theproduct);
virtual void print();
};
Company::Company(std::string theName, std::string theProduct)
{
name = theName;
product=theProduct;
}
TecCompany::TecCompany(std::string theName, std::string product) :Company(theName,product)
{
}
void Company::print()
{
std::cout << "我是" << name << "公司,我的产品是:" << product << "\n\n";
}
void TecCompany::print()
{
std::cout << "我是" << name << "公司,我们生产:" << product << "\n\n";
}
int main()
{
Company *company = new Company("Apple", "iPad");
TecCompany *teccompany = dynamic_cast<TecCompany *>(company);
if (teccompany != NULL)
{
teccompany->print();
}
else
{
std::cout << "悲催\n";
}
delete company;
company = NULL;
teccompany = NULL;
system("pause");
return 0;
}