Effective C++ 条款34 区分接口继承和实现继承

时间:2021-01-04 10:06:20

关于接口继承和实现继承:

所谓接口继承,就是派生类只继承函数的接口,也就是声明;而实现继承,就是派生类同时继承函数的接口和实现。

纯虚函数:要求继承类必须含有某个接口,并对接口函数实现。 虚函数:继承类必须含有某个接口,可以自己实现,也可以不实现,而采用基类定义的缺省实现。 非虚函数:继承类必须含有某个接口,必须使用基类的实现。


关于飞机问题代码如下:

#include <iostream>
#include <cstring>
using namespace std;
class Airport
{
public:
Airport(){}
Airport(char *p)
{
int len=strlen(p);
delete [] des;
des=NULL;
des=new char[len+1];
strcpy(des,p);
}

Airport(const Airport& Ap )
{
int len=strlen(Ap.des);
delete [] des;
des=NULL;
des=new char[len+1];
strcpy(des,Ap.des);

}


void SetDestination(char *p)
{
int len=strlen(p);
delete [] des;
des=NULL;
des=new char[len+1];
strcpy(des,p);
}

Airport& operator=(const Airport& Ap )
{
if (this==&Ap)
return *this;
int len=strlen(Ap.des);
delete [] des;
des=NULL;
des=new char[len+1];
strcpy(des,Ap.des);
return *this;
}

void show() const//这里一定要加const,因为若不加const,在调用destination.show()时会报错,
//因为destination为const Airport的引用
{
cout<<"The destination is :"<<des<<endl;
}

private:
char* des;

};


class Airplane
{

public:
virtual void fly(const Airport& destination);

};

void Airplane::fly(const Airport& destination)
{
destination.show();
}

class ModelA:public Airplane
{

};

class ModelB:public Airplane
{

};


class ModelC:public Airplane
{

};




int main()
{

Airport PDX("ChengDu");
Airplane* pa=new ModelC;
pa->fly(PDX);
Airplane* pb=new ModelA;
pb->fly(PDX);
return 0;
}
Effective C++ 条款34  区分接口继承和实现继承

由于C也为声明fly函数,所以会调用缺省的的Airplane::fly,从而本该飞行方式不同的C采用了和A、B相同的飞行方式;


#include <iostream>
#include <cstring>
using namespace std;
class Airport
{
public:
Airport(){}
Airport(char *p)
{
int len=strlen(p);
delete [] des;
des=NULL;
des=new char[len+1];
strcpy(des,p);
}

Airport(const Airport& Ap )
{
int len=strlen(Ap.des);
delete [] des;
des=NULL;
des=new char[len+1];
strcpy(des,Ap.des);

}


void SetDestination(char *p)
{
int len=strlen(p);
delete [] des;
des=NULL;
des=new char[len+1];
strcpy(des,p);
}

Airport& operator=(const Airport& Ap )
{
if (this==&Ap)
return *this;
int len=strlen(Ap.des);
delete [] des;
des=NULL;
des=new char[len+1];
strcpy(des,Ap.des);
return *this;
}

void show() const//这里一定要加const,因为若不加const,在调用destination.show()时会报错,
//因为destination为const Airport的引用
{
cout<<"The destination is :"<<des<<endl;
}

private:
char* des;

};


class Airplane
{

public:
virtual void fly(const Airport& destination)=0;

};

void Airplane::fly(const Airport& destination)
{
destination.show();
}

class ModelA:public Airplane
{

};

class ModelB:public Airplane
{

};


class ModelC:public Airplane
{

};




int main()
{

Airport PDX("ChengDu");
Airplane* pa=new ModelC;
pa->fly(PDX);
Airplane* pb=new ModelA;
pb->fly(PDX);
return 0;
}

可是如果将类Airplane中的fly声明为纯虚函数,虽然有定义,但是此时ModelA等就不能缺省了,因为

纯虚函数最突出的特产就是它必须被任何继承了它们的类重新声明,通常纯虚函数在抽象类没有定义,但是也可以定义,但是调用它的唯一途径是“调用时明确指出其类的名称”如下所示:

#include <iostream>
#include <cstring>
using namespace std;
class Airport
{
public:
Airport(){}
Airport(char *p)
{
int len=strlen(p);
delete [] des;
des=NULL;
des=new char[len+1];
strcpy(des,p);
}

Airport(const Airport& Ap )
{
int len=strlen(Ap.des);
delete [] des;
des=NULL;
des=new char[len+1];
strcpy(des,Ap.des);

}


void SetDestination(char *p)
{
int len=strlen(p);
delete [] des;
des=NULL;
des=new char[len+1];
strcpy(des,p);
}

Airport& operator=(const Airport& Ap )
{
if (this==&Ap)
return *this;
int len=strlen(Ap.des);
delete [] des;
des=NULL;
des=new char[len+1];
strcpy(des,Ap.des);
return *this;
}

void show() const//这里一定要加const,因为若不加const,在调用destination.show()时会报错,
//因为destination为const Airport的引用
{
cout<<"The destination is :"<<des<<endl;
}

private:
char* des;

};


class Airplane
{

public:
virtual void fly(const Airport& destination)=0;

};

void Airplane::fly(const Airport& destination)
{
destination.show();
}

class ModelA:public Airplane
{
virtual void fly(const Airport& destination)
{
cout<<"ModelA"<<endl;
Airplane::fly(destination);
}
};

class ModelB:public Airplane
{
virtual void fly(const Airport& destination)
{
cout<<"ModelB"<<endl;
Airplane::fly(destination);
}
};


class ModelC:public Airplane
{
virtual void fly(const Airport& destination)
{
cout<<"ModelC"<<endl;
Airplane::fly(destination);
}
};

int main()
{

Airport PDX("ChengDu");
Airplane* pa=new ModelC;
cout<<"1: "<<endl;
pa->Airplane::fly(PDX);
cout<<"2: "<<endl;
pa->fly(PDX);
return 0;
}

Effective C++ 条款34  区分接口继承和实现继承