#include <iostream>
#include <stdio.h>
class base
{
public:
base()
:baseName(""),baseData(0)
{}
base(std::string bn,int bd)
:baseName(bn),baseData(bd)
{}
std::string getBaseName() const
{
return baseName;
}
int getBaseData()const
{
return baseData;
}
private:
std::string baseName;
int baseData;
};
class derived:public base
{
public:
derived():base(),derivedName("")
{}
derived(std::string bn,int bd,std::string dn)
:base(bn,bd),derivedName(dn)
{}
std::string getDerivedName() const
{
return derivedName;
}
private:
std::string derivedName;
};
void show(std::string& info,const base& b)
{
info.append("Name is ");
info.append(b.getBaseName());
info.append(", baseData is ");
char buffer[10];
sprintf(buffer,"%d",b.getBaseData());
info.append(buffer);
}
int main(int argc,char* argv[])
{
base b("test",10);
std::string s;
show(s,b);
std::cout<<s<<std::endl;
derived d("btest",5,"dtest");
std::string ss;
show(ss,d);
std::cout<<ss<<std::endl;
return 0;
}
void show2(std::string& info,const derived& d)
{
info.append("Name is ");
info.append(d.getBaseName());
info.append(", baseData is ");
char buffer[10];
sprintf(buffer,"%d",d.getBaseData());
info.append(buffer);
}
1 derived_class.cpp: In function `int main(int, char**)': 2 derived_class.cpp:84: error: invalid initialization of reference of type 'const derived&' from expression of type 'base' 3 derived_class.cpp:70: error: in passing argument 2 of `void show2(std::string&, const derived&)'
| 继承方式\成员类型 | public | protected | private |
| public | public | protected | 无法继承 |
| protected | protected | protected | 无法继承 |
| private | private | private | 无法继承 |
class base
{
public:
std::string testPublic()
{
return std::string("this is public base");
}
protected:
std::string testProtected()
{
return std::string("this is protected base");
}
private:
std::string testPrivate()
{
return std::string("this is private base");
}
};
class derivedPublic:public base
{
public:
std::string testPubPublic()
{
return testPublic()+= "in derived";
}
std::string testProPublic()
{
return testProtected()+= "in derived";
}
std::string testPriPublic()
{
return testPrivate()+= "in derived";
}
};
int main(int argc,char* argv[])
{
derivedPublic dpub;
std::cout << dpub.testPublic() << std::endl;
}
derived11.cpp:16: error: `std::string base::testPrivate()' is private derived11.cpp:36: error: within this context
#include <iostream>
#include <string>
class base
{
public:
std::string testPublic()
{
return std::string("this is public base");
}
protected:
std::string testProtected()
{
return std::string("this is protected base");
}
private:
std::string testPrivate()
{
return std::string("this is private base");
}
};
class derivedPublic:public base
{
public:
std::string testPubPublic()
{
return testPublic()+= "in derived";
}
std::string testProPublic()
{
return testProtected()+= "in derived";
}
// std::string testPriPublic()
// {
// return testPrivate()+= "in derived";
// }
};
class deepDerived:public derivedPublic
{
public:
std::string deepProtected()
{
return testProtected() +="in deep";
}
std::string deepPublic()
{
return testPublic() +="indeep";
}
};
int main(int argc,char* argv[])
{
derivedPublic dpub;
std::cout << dpub.testProtected() << std::endl;
deepDerived deepdpub;
std::cout<<deepdpub.testPublic() <<std::endl;
std::cout<<deepdpub.testProtected() <<std::endl;
std::cout<<deepdpub.deepProtected() <<std::endl;
std::cout<<deepdpub.deepPublic() <<std::endl;
}
derived12.cpp:13: error: `std::string base::testProtected()' is protected derived12.cpp:62: error: within this context
#include <iostream>
#include <string>
class base
{
public:
std::string testPublic()
{
return std::string("this is public base");
}
protected:
std::string testProtected()
{
return std::string("this is protected base");
}
private:
std::string testPrivate()
{
return std::string("this is private base");
}
};
class derivedPublic:public base
{
public:
std::string testPubPublic()
{
return testPublic()+= "in derived";
}
std::string testProPublic()
{
return testProtected()+= "in derived";
}
// std::string testPriPublic() //私有成员并没有被继承下来
// {
// return testPrivate()+= "in derived";
// }
};
class deepDerived:public derivedPublic
{
public:
std::string test()
{
return testPublic() +="in 3";
}
};
class derivedProtected:protected base
{
public:
std::string testPubProtected()
{
return testPublic()+= "in derived";
}
std::string testProProtected()
{
return testProtected()+= "in derived";
}
};
class deepDerived2:public derivedProtected
{
public:
std::string test()
{
return testPublic() +="in 3";
}
};
class derivedPrivate:private base
{
public:
std::string testPubPirvate()
{
return testPublic()+= "in derived";
}
std::string testProPrivate()
{
return testProtected()+= "in derived";
}
};
//class deepDerived3:public derivedPrivate
//{
// public:
// std::string test()
// {
// return testPublic() +="in 3";
// }
//};
int main(int argc,char* argv[])
{
derivedPublic dpub;
//derivedProtected dpro;
//derivedPrivate dpri;
std::cout<<dpub.testPublic()<<std::endl; //
//std::cout<<dpub.testProtected()<<std::endl; //用户被继承也是无法使用
//cout<<dpub.testPrivate()<<std::endl; //基类都是私有函数
std::cout<<dpub.testPubPublic()<<std::endl;
std::cout<<dpub.testProPublic()<<std::endl;
//std::cout<<dpub.testPriPrivate()<<std::endl; //没有被继承
deepDerived dd;
std::cout<<dd.test()<<std::endl;
derivedProtected dpro;
//std::cout<<dpro.testPublic()<<std::endl; //变成protected类型
std::cout<<dpro.testPubProtected()<<std::endl;
std::cout<<dpro.testProProtected()<<std::endl;
deepDerived2 dd2;
std::cout<<dd2.test()<<std::endl;
derivedPrivate dpri;
std::cout<<dpri.testPubPirvate()<<std::endl;
std::cout<<dpri.testProPrivate()<<std::endl;
// deepDerived3 dd3;
// std::cout<<dd3.test()<<std::endl;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有