C++——虚函数

析构函数声明为虚函数
#include <iostream>
using namespace std;

class Base{
public:
	Base();
	virtual ~Base();//声明为virtual,保证调用子类的析构函数,释放内存空间
protected:
	char *str;
};

Base::Base(){
	str = new char[100];
	cout<<"Base contructor"<<endl;
}

Base::~Base(){
	delete[] str;
	cout<<"Base destructor"<<endl;
}

//派生类
class Derived:public Base{
public:
	Derived();
	~Derived();
private:
	char *name;
};

Derived::Derived(){
	name = new char[100];
	cout<<"Derived construcor"<<endl;
}

Derived::~Derived(){
	delete[] name;
	cout<<"Derived destructor"<<endl;
}

int main(){
	Base *pb = new Derived;
	delete pb;

	cout<<"-------------------"<<endl;

	Derived *pd = new Derived;
	delete pd;


	system("pause");
	return 0;
}
纯虚函数举例
#include <iostream>
using namespace std;

//线性,抽象类
class Line{
public:
	Line(float len);
	virtual float area() = 0;//纯虚函数
	virtual float volume() = 0;//纯虚函数
protected:
	float m_len;
};
Line::Line(float len):m_len(len){}

//矩形
class Rec:public Line{
public:
	Rec(float len,float width);
	float area();
protected:
	float m_width;
};
Rec::Rec(float len,float width):Line(len),m_width(width){}
float Rec::area(){return m_len*m_width;}

//长方体
class Cuboid:public Rec{
public:
	Cuboid(float len,float width,float height);
	float area();
	float volume();
protected:
	float m_height;
};
Cuboid::Cuboid(float len,float width,float height):Rec(len,width),m_height(height){}
float Cuboid::area(){return 2*(m_len*m_width+m_len*m_height+m_width*m_height);}
float Cuboid::volume(){return m_len*m_width*m_height;}

//正方体
class Cube:public Cuboid{
public:
	Cube(float len);
	float area();
	float volume();
};
Cube::Cube(float len):Cuboid(len,len,len){}
float Cube::area(){return 6*m_len*m_len;}
float Cube::volume(){return m_len*m_len*m_len;}

int main(){
	Line *p = new Cuboid(10,20,30);
	cout<<"The area of Cuboid is :"<<p->area()<<endl;
	cout<<"The volume of Cuboid is :"<<p->volume()<<endl;

	p = new Cube(15);
	cout<<"The area of Cube is :"<<p->area()<<endl;
	cout<<"The volume of Cube is :"<<p->volume()<<endl;

	system("pause");
	return 0;
}
理解虚函数对象模型
#include <iostream>
#include <string>
using namespace std;

//People
class People{
public:
	People(string name,int age);
public:
	virtual void display();
	virtual void eating();
protected:
	string m_name;
	int m_age;
};

People::People(string name,int age):m_name(name),m_age(age){}
void People::display(){
	cout<<"class People :"<<m_name<<"  今年:"<<m_age<<"岁了"<<endl;
}
void People::eating(){
	cout<<"Class People:我正在吃饭...."<<endl;
}

//Student类
class Student:public People{
public:
	Student(string name,int age,float score);
public:
	virtual void display();
	virtual void examing();
protected:
	float m_score;
};
Student::Student(string name ,int age,float score):
	People(name,age),m_score(score){ }

void Student::display(){
	cout<<"Class Student: "<<m_name<<"今年"<<m_age<<"岁了,考了"<<m_score<<"分。"<<endl;
}
void Student::examing(){
	cout<<"Class Student: "<<m_name<<"正在考试"<<endl;
}

//Senior类
class Senior:public Student{
public:
	Senior(string name,int age,float score,bool hasJob);
public:
	virtual void display();
	virtual void partying();
private:
	bool m_hasJob;
};
Senior::Senior(string name,int age,float score,bool hasJob):
	Student(name,age,score),m_hasJob(hasJob){}
void Senior::display(){
	if(m_hasJob){
		cout<<"Class Senior: "<<m_name<<"以"<<m_score<<"的成绩从大学毕业,找到了我工作,今年"<<m_age<<"岁!"<<endl;
	}else{
		cout<<"Class Senior: "<<m_name<<"以"<<m_score<<"的成绩从大学毕业,没找到工作,几年"<<m_age<<"岁"<<endl;
	}
}
void Senior::partying(){
	cout<<"Class Senior: 快毕业了,大家吃饭。。。。"<<endl;
}

int main(){
	People *p = new People("赵红",20);
	p->display();

	p = new Student("WANG",16,87);
	p->display();

	p = new Senior("LI",22,93,true);
	p->display();

	system("pause");
	return 0;
}

在这里插入图片描述
ps:摘自c语言中文网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值