鱼C论坛

 找回密码
 立即注册

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;
}

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 19:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部