如果A是基类,B是A的派生类,那么B将继承A的数据和函数。例如:
class A
{
public:
void Func1(void);
void Func2(void);
};
class B:public A
{
public:
void Func3(void);
void Func4(void);
};
void main()
{
B b;
b.Func1(); //B从A继承了函数Func1
b.Func2(); //B从A继承了函数Func2
b.Func3();
b.Func4();
return;
}
上面的代码运行后将显示如下的错误信息:
error LNK2001: unresolved external symbol "public: void __thiscall B::Func4(void)" (?Func4@B@@QAEXXZ)
error LNK2001: unresolved external symbol "public: void __thiscall B::Func3(void)" (?Func3@B@@QAEXXZ)
error LNK2001: unresolved external symbol "public: void __thiscall A::Func2(void)" (?Func2@A@@QAEXXZ)
error LNK2001: unresolved external symbol "public: void __thiscall A::Func1(void)" (?Func1@A@@QAEXXZ)
Debug/继承.exe : fatal error LNK1120: 4 unresolved externals
原因是声明了Func1-4这4个函数,却没有定义他们
只需将程序改为:
class A
{
public:
void Func1(void){};
void Func2(void){};
};
class B:public A
{
public:
void Func3(void){};
void Func4(void){};
};
void main()
{
B b;
b.Func1(); //B从A继承了函数Func1
b.Func2(); //B从A继承了函数Func2
b.Func3();
b.Func4();
return;
}