|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <iostream>
#include <string>
class Company
{
public:
Company(std::string theName);
virtual void printInfo();
protected:
std::string name;
};
class TechCompany:public Company
{
public:
TechCompany(std::string theName,std::string product);
virtual void printInfo();
private:
std::string product;
};
Company::Company(std::string theName)
{
name = theName;
}
void Company::printInfo()
{
std::cout<<"这个公司的名字叫:"<<name<<"。\n";
}
TechCompany::TechCompany(std::string theName,std::string product):Company(theName)
{
this->product = product;
}
void TechCompany::printInfo()
{
std::cout<< name << "公司大量生产了" << product << "这款产品!\n";
}
int main(int argc, char *argv[])
{
Company *company = new Company("Apple");
company ->printInfo();
delete company;
company = NULL;
company = new TechCompany("Apple","Iphone 5");
company ->printInfo();
delete company;
company = NULL;
return 0;
}
新手学习C++,请教一下,上面红色的一段,我大概知道是为了使得调用子类的构造函数时使用从基类继承来的theName这个属性,请问这个东西为什么用一个:加上函数这么写,究竟是为了实现什么,求达人解答!
|
|