C++ 继承详解及实例代码
C++继承可以是单一继承或多重继承,每一个继承连接可以是public,protected,private也可以是virtual或non-virtual。然后是各个成员函数选项可以是virtual或non-virtual或purevirtual。本文仅仅作出一些关键点的验证。
public继承,例如下:
1classbase
2{...}
3classderived:publicbase
4{...}
如果这样写,编译器会理解成类型为derived的对象同时也是类型为base的对象,但类型为base的对象不是类型为derived的对象。这点很重要。那么函数形参为base类型适用于derived,形参为derived不适用于base。下面是验证代码,一个参数为base的函数,传入derived应该成功执行,相反,一个参数为derived的函数
#include<iostream>
#include<stdio.h>
classbase
{
public:
base()
:baseName(""),baseData(0)
{}
base(std::stringbn,intbd)
:baseName(bn),baseData(bd)
{}
std::stringgetBaseName()const
{
returnbaseName;
}
intgetBaseData()const
{
returnbaseData;
}
private:
std::stringbaseName;
intbaseData;
};
classderived:publicbase
{
public:
derived():base(),derivedName("")
{}
derived(std::stringbn,intbd,std::stringdn)
:base(bn,bd),derivedName(dn)
{}
std::stringgetDerivedName()const
{
returnderivedName;
}
private:
std::stringderivedName;
};
voidshow(std::string&info,constbase&b)
{
info.append("Nameis");
info.append(b.getBaseName());
info.append(",baseDatais");
charbuffer[10];
sprintf(buffer,"%d",b.getBaseData());
info.append(buffer);
}
intmain(intargc,char*argv[])
{
baseb("test",10);
std::strings;
show(s,b);
std::cout<<s<<std::endl;
derivedd("btest",5,"dtest");
std::stringss;
show(ss,d);
std::cout<<ss<<std::endl;
return0;
}
运行结果为:
base:baseNameistest,baseDatais10
base:baseNameisbtest,baseDatais5
下面改改代码,将函数参数变为derived
voidshow2(std::string&info,constderived&d)
{
info.append("Nameis");
info.append(d.getBaseName());
info.append(",baseDatais");
charbuffer[10];
sprintf(buffer,"%d",d.getBaseData());
info.append(buffer);
}
调用show(ss,d);编译器报错
1derived_class.cpp:Infunction`intmain(int,char**)': 2derived_class.cpp:84:error:invalidinitializationofreferenceoftype'constderived&'fromexpressionoftype'base' 3derived_class.cpp:70:error:inpassingargument2of`voidshow2(std::string&,constderived&)'
第二点对各种形式的继承作出验证,首先给出表格
继承方式\成员类型
public
protected
private
public
public
protected
无法继承
protected
protected
protected
无法继承
private
private
private
无法继承
这里解释一下,这里仅仅表达基类的成员,被public,protected,private三种方式继承后,在原基类为public,protectedc,private的成员在继承类里类型为表格里内容
classbase
{
public:
std::stringtestPublic()
{
returnstd::string("thisispublicbase");
}
protected:
std::stringtestProtected()
{
returnstd::string("thisisprotectedbase");
}
private:
std::stringtestPrivate()
{
returnstd::string("thisisprivatebase");
}
};
classderivedPublic:publicbase
{
public:
std::stringtestPubPublic()
{
returntestPublic()+="inderived";
}
std::stringtestProPublic()
{
returntestProtected()+="inderived";
}
std::stringtestPriPublic()
{
returntestPrivate()+="inderived";
}
};
intmain(intargc,char*argv[])
{
derivedPublicdpub;
std::cout<<dpub.testPublic()<<std::endl;
}
报下面错误,说明testPrivate()不是derived私有函数而是base的私有函数
derived11.cpp:16:error:`std::stringbase::testPrivate()'isprivate derived11.cpp:36:error:withinthiscontext
这样验证private类型成员无法被继承(public,private,protected)注:private,protected略去不做证明
下面只要验证testProtected能被第三层继承类继承,但是无法被第三层类直接调用就说明是public继承后继承类型为protected,而基类为Public类型成员则即可被继承又可以直接调用。
#include<iostream>
#include<string>
classbase
{
public:
std::stringtestPublic()
{
returnstd::string("thisispublicbase");
}
protected:
std::stringtestProtected()
{
returnstd::string("thisisprotectedbase");
}
private:
std::stringtestPrivate()
{
returnstd::string("thisisprivatebase");
}
};
classderivedPublic:publicbase
{
public:
std::stringtestPubPublic()
{
returntestPublic()+="inderived";
}
std::stringtestProPublic()
{
returntestProtected()+="inderived";
}
//std::stringtestPriPublic()
//{
//returntestPrivate()+="inderived";
//}
};
classdeepDerived:publicderivedPublic
{
public:
std::stringdeepProtected()
{
returntestProtected()+="indeep";
}
std::stringdeepPublic()
{
returntestPublic()+="indeep";
}
};
intmain(intargc,char*argv[])
{
derivedPublicdpub;
std::cout<<dpub.testProtected()<<std::endl;
deepDeriveddeepdpub;
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::stringbase::testProtected()'isprotected derived12.cpp:62:error:withinthiscontext
这样就验证了一个是public,一个是protected,protected是不能直接调用的,但是被继承后是可以被public成员调用的。
下面的已经证明,详细步骤就略去如果对该部分验证感兴趣,可以看下面代码。
#include<iostream>
#include<string>
classbase
{
public:
std::stringtestPublic()
{
returnstd::string("thisispublicbase");
}
protected:
std::stringtestProtected()
{
returnstd::string("thisisprotectedbase");
}
private:
std::stringtestPrivate()
{
returnstd::string("thisisprivatebase");
}
};
classderivedPublic:publicbase
{
public:
std::stringtestPubPublic()
{
returntestPublic()+="inderived";
}
std::stringtestProPublic()
{
returntestProtected()+="inderived";
}
//std::stringtestPriPublic()//私有成员并没有被继承下来
//{
//returntestPrivate()+="inderived";
//}
};
classdeepDerived:publicderivedPublic
{
public:
std::stringtest()
{
returntestPublic()+="in3";
}
};
classderivedProtected:protectedbase
{
public:
std::stringtestPubProtected()
{
returntestPublic()+="inderived";
}
std::stringtestProProtected()
{
returntestProtected()+="inderived";
}
};
classdeepDerived2:publicderivedProtected
{
public:
std::stringtest()
{
returntestPublic()+="in3";
}
};
classderivedPrivate:privatebase
{
public:
std::stringtestPubPirvate()
{
returntestPublic()+="inderived";
}
std::stringtestProPrivate()
{
returntestProtected()+="inderived";
}
};
//classdeepDerived3:publicderivedPrivate
//{
//public:
//std::stringtest()
//{
//returntestPublic()+="in3";
//}
//};
intmain(intargc,char*argv[])
{
derivedPublicdpub;
//derivedProtecteddpro;
//derivedPrivatedpri;
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;//没有被继承
deepDeriveddd;
std::cout<<dd.test()<<std::endl;
derivedProtecteddpro;
//std::cout<<dpro.testPublic()<<std::endl;//变成protected类型
std::cout<<dpro.testPubProtected()<<std::endl;
std::cout<<dpro.testProProtected()<<std::endl;
deepDerived2dd2;
std::cout<<dd2.test()<<std::endl;
derivedPrivatedpri;
std::cout<<dpri.testPubPirvate()<<std::endl;
std::cout<<dpri.testProPrivate()<<std::endl;
//deepDerived3dd3;
//std::cout<<dd3.test()<<std::endl;
}
以上就是对C++j继承的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!