小甲鱼 发表于 2014-1-12 17:38:54

第三十七讲 高级强制类型转换(课件+源代码)

《C++快速入门》037 - 课件+典型例题源代码


本讲教学视频下载地址:高级强制类型转换 -《C++快速入门》037


附件包含:课件+典型例题源代码






注:VIP会员享有免费下载本站所有资源的特权!

友情提示:通过购买鱼C光盘(具体内容)赞助鱼C工作室均可加入VIP会员^_^



lukeigun 发表于 2014-12-27 16:17:45

强烈支持楼主ing……

fisheryao 发表于 2017-12-4 11:14:48

好资料

hogen 发表于 2019-11-7 14:10:56

#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 TechCompany : public Company
{
public:
    TechCompany(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 << "\n";
}

TechCompany::TechCompany(std::string theName, std::string product) : Company(theName, product)
{
}

void TechCompany::printInfo()
{
    std::cout << name << "公司大量生产了 " << product << "这款产品!\n";
}

int main()
{
    Company *company = new TechCompany("APPLE", "Iphone");
    TechCompany *tecCompany = (TechCompany *)company;

    tecCompany->printInfo();

    delete company;//只要删除company或者tecCompany这个指针因为它们两个是同一个地址不能重复释放
    delete tecCompany;
    company = NULL;
    tecCompany = NULL;

    return 0;
}
页: [1]
查看完整版本: 第三十七讲 高级强制类型转换(课件+源代码)