C++之多态和虚函数

多态是指具有不同功能的函数可以用同一个函数名。

主要探索:

1 虚函数(纯虚函数)

2 虚基类

3 抽象类

4 虚析构函数


虚函数:

用相同形式能同时调用派生类和基类一样函数。

第一个实例,非虚函数,基类和派生类具有相同函数:

#include <iostream>
#include <string>

using namespace std;

//声明基类
class student
{
protected:
	int _num;
	string _name;
	float _score;

public :
	student(int ,string,float);
    void display();
};
//定义基类构造函数
student::student(int num,string name,float score):_num(num),_name(name),_score(score){}
void student::display(){
	cout<<"num :"<<_num<<endl
		<<"name:"<<_name<<endl
		<<"score:"<<_score<<endl<<endl;
}

//派生类
class graduate:public student
{
private:
	float _pay;
public:
	graduate(int,string,float,float);
	//声明和基类相同函数
	void display();
};
graduate::graduate(int num,string name,float score,float pay):_pay(pay),student(num,name,score){}
void graduate::display(){
		cout<<"num :"<<_num<<endl
		<<"name :"<<_name<<endl
		<<"score:"<<_score<<endl
		<<"pay  :"<<_pay<<endl<<endl;
}

int main(){
	student stu(100,"buyingfei",100);
	graduate gra(1000,"buyafei",120,10000);
	student *pt=&stu;
	pt->display();
	pt=&gra;
	pt->display();  //输出结果是基类一部分
	stu.display();
	return 0;
}

运行结果:


第二个实例,使用虚函数

#include <iostream>
#include <string>

using namespace std;

//声明基类
class student
{
protected:
	int _num;
	string _name;
	float _score;

public :
	student(int ,string,float);
   virtual void display();
};
//定义基类构造函数
student::student(int num,string name,float score):_num(num),_name(name),_score(score){}
 void student::display(){
	cout<<"num :"<<_num<<endl
		<<"name:"<<_name<<endl
		<<"score:"<<_score<<endl<<endl;
}

//派生类
class graduate:public student
{
private:
	float _pay;
public:
	graduate(int,string,float,float);
	//声明和基类相同函数
       virtual	void display();
};
graduate::graduate(int num,string name,float score,float pay):_pay(pay),student(num,name,score){}
void graduate::display(){
		cout<<"num :"<<_num<<endl
		<<"name :"<<_name<<endl
		<<"score:"<<_score<<endl
		<<"pay  :"<<_pay<<endl<<endl;
}

int main(){
	student stu(100,"buyingfei",100);
	graduate gra(1000,"buyafei",120,10000);
	student *pt=&stu;
	pt->display();
	pt=&gra;
	pt->display();  
	stu.display();
	return 0;
}

运行结果:


抽象类:

不用它生成对象,作为基类去建立派生类,凡是包含纯虚函数的类都是抽象类。

#include <iostream>
#include <string>

using namespace std;

//定义抽象类
class people
{
public :
	virtual void getMessage()const=0; //定义纯虚构函数
};


class student:public people
{
protected:
	int _num;
	string _name;
	float _score;

public :
	student(int ,string,float);
    virtual void getMessage() const;
};
student::student(int num,string name,float score):_num(num),_name(name),_score(score){}
 void student::getMessage() const{
	cout<<"num :"<<_num<<endl
		<<"name:"<<_name<<endl
		<<"score:"<<_score<<endl<<endl;	
}


class graduate:public student
{
private:
	float _pay;
public:
	graduate(int,string,float,float);
	//声明和基类相同函数
	virtual	void getMessage() const{
		cout<<"num :"<<_num<<endl
		<<"name :"<<_name<<endl
		<<"score:"<<_score<<endl
		<<"pay  :"<<_pay<<endl<<endl;	
	}
};
graduate::graduate(int num,string name,float score,float pay):_pay(pay),student(num,name,score){}

int main(){
	student stu(100,"buyingfei",100);
	graduate gra(1000,"buyafei",120,10000);
	student *pt=&stu;
	pt->getMessage();
	pt=&gra;
	pt->getMessage();  
	stu.getMessage(); 
	return 0;
}

运行结果:


虚基类:

如果一个派生类有多个直接基类,而这些基类又有一个共同基类,则派生类简介保存共同基类的多份数据成员,虚基类则解决这问题。不是在声明基类时说明的,而是在声明派生类继承方式时指出的。

如class 派生类名 :virtual 继承方式 基类名。

虚析构函数:

派生类的对象从内存中撤销,一般先调用派生类析构函数,然后在调用基类析构函数,

但是通过new 运算符建立临时对象,若基类存在析构函数,并且定义指向基类的指针变量,通过delete 撤销对象时,不会执行派生类的析构函数。

这时建立虚析构函数。


普通对象析构函数调用

#include <iostream>
#include <string>

using namespace std;




class student
{
protected:
	int _num;
	string _name;
	float _score;

public :
	student(int ,string,float);
    virtual void getMessage() const;
	~student(){
	  cout<<"destructor student"<<endl;
	}
};
student::student(int num,string name,float score):_num(num),_name(name),_score(score){}
 void student::getMessage() const{
	cout<<"num :"<<_num<<endl
		<<"name:"<<_name<<endl
		<<"score:"<<_score<<endl<<endl;	
}


class graduate:public student
{
private:
	float _pay;
public:
	graduate(int,string,float,float);
	//声明和基类相同函数
	virtual	void getMessage() const{
		cout<<"num :"<<_num<<endl
		<<"name :"<<_name<<endl
		<<"score:"<<_score<<endl
		<<"pay  :"<<_pay<<endl<<endl;	
	}
	~graduate(){
	 cout<<"destory graduate"<<endl;
	}
};
graduate::graduate(int num,string name,float score,float pay):_pay(pay),student(num,name,score){}

int main(){
	student stu(100,"buyingfei",100);
	graduate gra(1000,"buyafei",120,10000);
//	student *pt=&stu;
//	pt->getMessage();
//	pt=&gra;
//	pt->getMessage();  
	gra.getMessage();
	stu.getMessage(); 
	return 0;
}

运行结果:



析构函数不是虚函数:

#include <iostream>
#include <string>

using namespace std;
 class point
 {
 public :
	 point(){}
      ~point(){
	  cout<<"destory point"<<endl;
	 }
 };
 class circle:public point
 {
 public:
	 circle(){}
	 ~circle(){
	 cout<<"destory circle"<<endl;
	 }
 };
int main(){
   point *p=new circle;
   delete p;  //这时并没有调用派生类析构函数
	return 0;
}

运行结果:


基类析构函数是虚构函数:

#include <iostream>
#include <string>

using namespace std;
 class point
 {
 public :
	 point(){}
     virtual  ~point(){
	  cout<<"destory point"<<endl;
	 }
 };
 class circle:public point
 {
 public:
	 circle(){}
	 virtual ~circle(){
	 cout<<"destory circle"<<endl;
	 }
 };
int main(){
   point *p=new circle;
   delete p;
	return 0;
}

运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值