多态性与虚函数Ⅱ

虚函数

对多态具有决定性作用,有虚函数才能构成多态

1只需要在虚函数声明处(类里面)加上virtual关键字,函数定义处可以加也可以不加。

2构成多态的条件

1)必须存在继承关系

2)继承关系中必须有同名的虚函数,并且他们是覆盖关系

3)存在基类指针,通过该指针调用虚函数

#include<iostream>
using namespace std;
//基类Base
class Base{
public:
      virtual void func():
      virtual void func(int);
};
void Base::func()
{
cout<<"Base::func()"<<endl;
}
void Base::func(int n)
{
cout<<"Base::func(int n)"<<endl;
}
//派生类Derived
class Derived:public Base{
public:
      void func();
      void func(string);
};
void Derived::func(){
cout<<"void Derived::func()"<<endl;
}
void Derived::func(string){
cout<<"void Derived::unc(string)"<<endl;
}
int main(){
Base *p=nes Derived();
p->func();//输出void Derived::func()
p->func(10);//输出void Base::func(int)
p->func("http://c.biancheng.net");//compile error
return 0;

3虚析构函数

用new运算符建立一个动态对象,如基类中有析构函数,并且定义了一个指向基类的指针变量。

在程序中用带指针参数的delete运算符撤销对象时,

#include<iostream>
using namespace std;
//基类
class Point{
public:
point(){cout<<"Point()"<<endl;};;
~point(){cout<<"~Point()"<<endl;};
};
//派生类
class Circle:public Point{
public:
     Circle(){cout<<"Circle()"<<endl;};
     ~Circle(){cout<<"~Circle()"<<endl;};
}:
int main()
{
//基类指针初始化为基类对象
cout<<"Test 1:"<<endl;
Point *p1=new Point;
delete p1;
//派生类定义派生类对象
cout<<"Test 2:"<<endl;
Point *p2=new Circle;
delete p2;
//基类指针初始化为派生类对象
cout<<"Test 3:"<<endl;
Point *p3=new Circle;
delete p3;




return 0;

纯虚函数与抽象类

语法格式:

virtual  返回值类型 函数名(函数参数)=0;

#include<iostream>
using namespace std;
class Line{//含有纯虚函数的类称为抽象类,不能实例化。
public:((len){cout<<"Line 构造函数"<<endl;};
virtual float Area()=0;
virtual float Volume()=0;
protected: 
       int m_len;
};
//矩形类
class Rec:public Line
{
public:
    Rec(int len,int width):Line(len),m_width(width){};
    float area(){return m_len*m_width;};
   // float volume(){return 0;};
protected:
    int m_width;//宽度
};

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

 
int main()
{
    Line *p =  new Rec(5, 6);
    cout<<"Rec area:"<<p->area()<<endl;
}
class Line
{
protected:
	int m_len;
public:
	Line(int len) :m_len(len) {};
	virtual float Area() = 0;
	virtual float Volume() = 0;
};
class Rec :public Line
{
protected:
	int m_width;
public:
	Rec(int len, int width) :Line(len), m_width(width) {};
	float Area() { return m_len * m_width; };
	float Volume() { return 0; };
};
class Cub :public Rec
{
protected:
	int m_hight;
public:
	Cub(int len, int width, int height) :Rec(len, width), m_hight(height) {};
	float Area() { return (m_len * m_width+m_len*m_hight+m_hight*m_width)*2; };
	float Volume() { return m_len * m_width * m_hight; };
};
int main()
{
	Rec* a = new Rec(3, 4);
	cout << "Rec" << a->Area() << endl;
	cout << endl;
	Cub* p = new Cub(2,3,4);
	cout << "表面积:" << p->Area() << endl;
	cout << "体积:" << p->Volume() << endl;
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值