多态的实现

概念

在继承中,有时候基类的一些函数在派生类中也是有用的,但是功能不够全或者两者的功能实现方式就是不一样的,这个时候就希望重载那个基类的函数。

为了在调用函数时,出现不知道调用基类的还是子类的情况出现,就提出了多态。

实现方式

多态是依赖于虚函数实现的。

虚函数可以实现堕胎的原因:基类和派生类均有自己的虚函数表,在调用虚函数时,通过虚函数表调用对应的函数。

注意事项: 

虚函数本质上是一种迟后联编,生成虚函数表会增大空间上的消耗(除非两个函数的返回类型,参数类型,参数个数都相同,否则起不到多态的作用,也就不需要用虚函数了)。

有一种特殊情况,如果基类中虚函数返回一个基类指针或引用,派生类中返回一个派生类的指针或引用,则c++将其视为同名虚函数而进行迟后联编。

代码

#include
using namespace std;

class A{
public :
    void print(){ cout << "基类priint" << endl; }
    void print1(){ cout << "基类print1" << endl; }
	virtual void print2(){ cout << "基类print2" << endl; }
	virtual void print3(){ cout << "基类print3" << endl; }

};

class B:public A{
public:
    void print() { cout << "继承类print" << endl; }
    virtual void print1() { cout << "继承类print1" << endl; }
	void print2() { cout << "继承类print2" << endl; }
	virtual void print3() { cout << "继承类print3" << endl; }
};

void fn(A &s){//传引用
    s.print();
    s.print1();
	s.print2();
	s.print3();
}
void fn1(B &s){//传引用
    s.print();
    s.print1();
	s.print2();
	s.print3();
}
int main(){
    A a;
    B b;
    fn(a);//均调用基类的函数
	cout<

实现结果:

多态的实现_第1张图片

基类和派生类均返回各自的指针和引用

#include
#include
using namespace std;

class A{
public :
    virtual A * fun(){
        cout << "基类fun" << endl;
        return this;
    }
	virtual A * fun1(){
        cout << "基类fun1" << endl;
        return this;
    }
	A * fun2(){
        cout << "基类fun2" << endl;
        return this;
    }
};
class B :public A{
public :
    virtual B * fun(){
        cout << "派生类fun" << endl;
        return this;
    }
	B * fun1(){
        cout << "派生类fun1" << endl;
        return this;
    }
	B * fun2(){
        cout << "派生类fun2" << endl;
        return this;
    }
};

void test(A & x){
    x.fun();
	x.fun1();
	x.fun2();
}

int main()
{
    A a;
    B b;
    test(a);
	cout<

 执行结果

多态的实现_第2张图片

使用虚函数的限制 

(1)只有类成员函数才能声明为虚函数(因为虚函数只适用于有继承关系的类对象中)
(2)静态成员函数不能声明为虚函数,(因为静态成员函数不受限于某个对象,整个内存中只有一个,故不会混淆)
(3)内联函数、构造函数不可以被继承
(5)析构函数可以被继承,而且通常声明为虚函数。

纯虚函数

纯虚函数声明格式:virtual 返回值类型成员函数名(参数表)=0;

必要性:很多情况下,基类本身生成对象是不合理的;此时将函数定义为纯虚函数,则编译器要求派生类中必须予以重载实现多态性。

举例:动物作为一个基类可以派生出老虎、孔雀等子类,但动物本身生成对象明显不合常理。

#include
#include
using namespace std;

class A{
public :
    virtual void fun() = 0;
};
class B :public A{
public :
    virtual void fun(){cout << "继承类" << endl;}
};

int main(){
    B b;
    b.fun();
    return 0;
}

抽象类

含有纯虚函函数的类;这种类不能生产对象。

抽象类只能用于被继承,而不能直接创建对象的类,只能作为接口使用。

抽象类(接口)

#include
using namespace std;

class Shape{//抽象类
public:
	virtual int getArea()=0;//提供接口框架的纯虚函数
	void setWidth(int w){
		width=w;
	}
	void setHeight(int h){
		height=h;
	}
protected:
	int width;
	int height;
};
class Rectangle:public Shape{
public:
	int getArea(){
		return(width*height);
	}
};
class Triangle:public Shape{
public:
	int getArea(){
		return(width*height)/2;
	}
};
int main(){
	Rectangle Rect;
	Triangle Tri;
	
	Rect.setWidth(5);
	Rect.setHeight(7);
	cout<<"长方形面积="<

你可能感兴趣的:(C++,c++,开发语言)