鱼C论坛

 找回密码
 立即注册
查看: 373|回复: 1

[学习笔记] C++快速入门P34动态内存管理课上代码

[复制链接]
发表于 2024-1-15 11:25:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <iostream>
#include <string>

/*  基类  --------------------------------------------------------------------*/
class Company
{
public:
        Company(std::string theName);
        virtual void printInfo(void);  //基类也需要加virtual

protected:
    std::string name;       
};

/*  子类  --------------------------------------------------------------------*/
class TechCompany : public Company  //class xxx : public xxx
{
public:
        TechCompany(std::string theName, std::string theProduct);
        virtual void printInfo(void);

private:
    std::string product;       
};

/*  函数  --------------------------------------------------------------------*/
//Company
Company::Company(std::string theName)
{
        name = theName;
}

void Company::printInfo(void)
{
        std::cout << "这家公司名字叫:" << name << "\n";        
}

//TechCompany
TechCompany::TechCompany(std::string theName, std::string theProduct):Company(theName)  //:Company(theName)
{
        product = theProduct;
}
void TechCompany::printInfo(void)
{
        std::cout << "这家公司的产品是:" << product << "\n";                
}


int main()
{
       
        Company *company = new Company("APPLE");
        company -> printInfo();
       
        delete company;
        company = NULL;
       
        company = new TechCompany("APPLE","iPhone");
        company -> printInfo();
       
        delete company;
        company = NULL;
       
        return 0;
}


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-1-15 11:25:37 | 显示全部楼层
您的代码中缺少一些头文件的引用,包括iostream和string。此外,在printInfo函数中,需要在输出语句的末尾添加一个结束符号。下面是修改后的代码:

  1. #include <iostream>
  2. #include <string>

  3. /*  基类  --------------------------------------------------------------------*/
  4. class Company
  5. {
  6. public:
  7.     Company(std::string theName);
  8.     virtual void printInfo(void);  //基类也需要加virtual

  9. protected:
  10.     std::string name;
  11. };

  12. /*  子类  --------------------------------------------------------------------*/
  13. class TechCompany : public Company  //class xxx : public xxx
  14. {
  15. public:
  16.     TechCompany(std::string theName, std::string theProduct);
  17.     virtual void printInfo(void);

  18. private:
  19.     std::string product;
  20. };

  21. /*  函数  --------------------------------------------------------------------*/
  22. //Company
  23. Company::Company(std::string theName)
  24. {
  25.     name = theName;
  26. }

  27. void Company::printInfo(void)
  28. {
  29.     std::cout << "Company name: " << name << std::endl;
  30. }

  31. //TechCompany
  32. TechCompany::TechCompany(std::string theName, std::string theProduct) : Company(theName)
  33. {
  34.     product = theProduct;
  35. }

  36. void TechCompany::printInfo(void)
  37. {
  38.     std::cout << "Company name: " << name << std::endl;
  39.     std::cout << "Product: " << product << std::endl;
  40. }

  41. int main()
  42. {
  43.     Company* myCompany = new TechCompany("FishC", "Programming Education");
  44.     myCompany->printInfo();
  45.     delete myCompany;

  46.     return 0;
  47. }
复制代码


在主函数中,创建了一个TechCompany对象,并将其地址赋给指向基类Company的指针myCompany。通过调用指针的printInfo方法,可以触发动态绑定,调用到子类TechCompany的printInfo方法。最后使用delete关键字释放了动态分配的内存。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-20 05:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表