cpp虚函数全解与实测

前言…
说来许久没更新博客,但小张并没有荒废,意志也没有消退。
此刻正被老板派到河北某村儿出差,手里还没有心仪的offer,实惨。

言归正传,直接给出用于测试cpp虚函数的纯虚函数虚函数动态多态构造函数析构函数的代码,各位看官应该一看便知其中的奥妙:

show you my code

// 虚函数动态多态
// Created by wbzhang on 2020/9/19.
//

#include <iostream>
#include <string>
using namespace std;

struct Point2i
{
	int x,y;
};

// 基类 Shape
class Shape
{
protected:
	int width;
	int height;
public:
	static string ShapeName; // 静态成员

	static void shapePrint()
	{
		std::printf("ShapeName is %s in stacic.","shape");
	}

	void setWidth(int w)
	{
		width = w;
	}
	void setHeight(int h)
	{
		height = h;
	}
	virtual void print()
	{
		cout<<"This is shape"<<endl;
	}

	// 构造函数
	Shape():width(0),height(0)
	{
		cout<<"This Shape is constructed."<<endl;
	}

	// 父类析构函数申明为虚函数
	virtual ~Shape()
	{
		cout<<"This Shape is destroyed."<<endl;
	}
};

// 接口类,仅包含纯虚函数,并且不包含其他任何成员与方法
// 无法进行实例化
class PaintMethod
{
public:
	virtual void getCube()=0;
	virtual void getPencil()=0;
	virtual void getBlank()=0;
};

// 基类 PaintCost,抽象类;无法进行实例化
class PaintCost
{
public:
	int getCost(int area)
	{
		return area * 70;
	}

	// 声明一个纯虚函数
	virtual void getCube() =0;
};


// 还有一种所有方法均public给外界调用的叫啥...给忘了,集成类??


// 派生类
class Rectangle: public Shape, public PaintCost
{
private:
	Point2i startPt;
	Point2i endPt;
public:
	int getArea()
	{
		return (width * height);
	}
	void print()
	{
		cout<<"This is Rectangle"<<endl;
	}

	// 继承了抽象类的纯虚函数,则必须将所有纯虚函数均实现才可以实例化
	void getCube()
	{
		cout<<"Just test for pure_virtual_class."<<endl;
	}

	// 子类构造函数
	Rectangle()
	{
		cout<<"This Rectangle is constructed."<<endl;
	}

	// 子类析构函数
	~Rectangle()
	{
		cout<<"This Rectangle is destroyed."<<endl;
	}
};

int main(void)
{
	// 1.构造函数
	cout<<"------ Shape constructor func ------"<<endl;
	Shape *shape = new Shape();
	cout<<"------ Rectangle constructor func ------"<<endl;
	Rectangle *rectangle = new Rectangle();

	// 1.1 调用自身打印
	cout<<"------ Self print ------"<<endl;
	cout<<"Shape print: ";
	shape->print();
	cout<<"Rectangle print: ";
	rectangle->print();

	// 1.2 类的静态成员与静态方法,通过::获取
//	Shape::ShapeName = "I'm BJTUer.";
//	cout<<Shape::ShapeName<<endl;
//	Shape::shapePrint();

	// 2 动态多态打印
	cout<<"------ Dynamic constructor func ------"<<endl;
	Shape *shape1 = new Rectangle();
	cout<<"------ Dynamic bind print ------"<<endl;
	shape1->print();

	// 3 调用析构函数
	cout<<"------ shape destructor func------"<<endl;
	delete shape;
	cout<<"------ rectangle destructor func------"<<endl;
	delete rectangle;
	cout<<"------ shape1 destructor func------"<<endl;
	delete shape1;

	return 0;
}

/* 总结:
 * 1) 虚函数原理:虚表指针与虚函数表
 * 2) 构造函数:先基类,再派生类的成员,再调用派生类构造函数
 * 3) 析构函数:先调用子类析构函数,再调用父类析构函数
 * 4) 纯虚函数-->接口类,抽象类
 * */
 

测试结果如下:

在这里插入图片描述

总结

  1. 虚函数原理:虚表指针与虚函数表

  2. 构造函数:先基类,再派生类的成员,再调用派生类构造函数

  3. 析构函数:先调用子类析构函数,再调用父类析构函数

  4. 纯虚函数–>接口类,抽象类

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值