多态性包括:
*重载--包含多态
*强制--强制多态
*虚函数--抽象多态
*模板--参数多态
*编译时多态:静态多态
*运行时多态:动态多态
一.虚函数
虚函数是c++实现动态多态性的机制,通过虚函数可以使基类和派生类的同名函数具有不同的功能.
函数的覆盖
在派生类中可以对基类中的成员进行覆盖(重定义)
注意在派生类中对虚函数进行重定义时,要保证名称,参数,返回值与基类的虚函数一致.
举个栗子:
class Element
{
*重载--包含多态
*强制--强制多态
*虚函数--抽象多态
*模板--参数多态
*编译时多态:静态多态
*运行时多态:动态多态
一.虚函数
虚函数是c++实现动态多态性的机制,通过虚函数可以使基类和派生类的同名函数具有不同的功能.
函数的覆盖
在派生类中可以对基类中的成员进行覆盖(重定义)
注意在派生类中对虚函数进行重定义时,要保证名称,参数,返回值与基类的虚函数一致.
举个栗子:
class Element
{
};
ELement* p;
class Circle :public Element
{
};
Circle cir;
1).Element *p=○
2).Element &p=cir;//1.2的本质一样都是派生类的对象可以给基类对象赋值
ELement* p;
class Circle :public Element
{
};
Circle cir;
1).Element *p=○
2).Element &p=cir;//1.2的本质一样都是派生类的对象可以给基类对象赋值
2.虚函数成员
在成员函数前面加上virtual修饰
注意点: 1).virtual只能用于类体中,不能用在类体外的虚函数的实现代码
2).虚函数不得是静态函数
3).在派生类中重定义虚函数时,无论是否用virtual修饰都还是虚函数
4).虚函数要付出开销,花费时间和空间要多一些
5).调用虚函数时为动态绑定(运行时确定),非虚函数为静态绑定(编译时确定)
6).构造函数不能声明为虚函数,在构造函数中调用函数时,被调用的是那个所在类的虚函数版本.
通过基类指针(或引用)可以用于指向它的子类型对象,通过这样的指针调用虚函数时,被调用的是该指针(或引用)实际指向的对象类的那个重定义版本,类中的其他成员函数也可以调用虚函数,通过this指针来调用虚函数,只不过在调用格式中一般都省略前缀this->;
举个栗子:
#include<iostream>
using namespace std;
class A
{
public:
void show()
{
cout<<"this is a"<<endl;
}
void f()
{
this->show();
}
在成员函数前面加上virtual修饰
注意点: 1).virtual只能用于类体中,不能用在类体外的虚函数的实现代码
2).虚函数不得是静态函数
3).在派生类中重定义虚函数时,无论是否用virtual修饰都还是虚函数
4).虚函数要付出开销,花费时间和空间要多一些
5).调用虚函数时为动态绑定(运行时确定),非虚函数为静态绑定(编译时确定)
6).构造函数不能声明为虚函数,在构造函数中调用函数时,被调用的是那个所在类的虚函数版本.
通过基类指针(或引用)可以用于指向它的子类型对象,通过这样的指针调用虚函数时,被调用的是该指针(或引用)实际指向的对象类的那个重定义版本,类中的其他成员函数也可以调用虚函数,通过this指针来调用虚函数,只不过在调用格式中一般都省略前缀this->;
举个栗子:
#include<iostream>
using namespace std;
class A
{
public:
void show()
{
cout<<"this is a"<<endl;
}
void f()
{
this->show();
}
};
class B :public A
{
public:
void show()
{
cout<<"this is b"<<endl;
}
void f()
{
show();
}
};
int main()
{
A* a;
B b;
a=&b;
a->show();
a->f();
}
运行结果:
this is a
this is a
class B :public A
{
public:
void show()
{
cout<<"this is b"<<endl;
}
void f()
{
show();
}
};
int main()
{
A* a;
B b;
a=&b;
a->show();
a->f();
}
运行结果:
this is a
this is a
运用虚函数后:
#include<iostream>
using namespace std;
class A
{
public:
virtual void show()
{
cout<<"this is a"<<endl;
}
void f()
{
this->show();
}
#include<iostream>
using namespace std;
class A
{
public:
virtual void show()
{
cout<<"this is a"<<endl;
}
void f()
{
this->show();
}
};
class B :public A
{
public:
void show()
{
cout<<"this is b"<<endl;
}
void f()
{
show();
}
};
int main()
{
A* a;
B b;
a=&b;
a->show();
a->f();
}
class B :public A
{
public:
void show()
{
cout<<"this is b"<<endl;
}
void f()
{
show();
}
};
int main()
{
A* a;
B b;
a=&b;
a->show();
a->f();
}
运行结果:
this is b
this is b
this is b
this is b
3.虚析构函数
c++虽然不能声明虚构造函数,但可以声明虚析构函数
对于虚析构,就是在析构函数前加virtual关键字,防止内存泄露.
运用虚析构:
#include<iostream>
using namespace std;
class A
{
public:
virtual ~A()
{
cout<<"A"<<endl;
};
#include<iostream>
using namespace std;
class A
{
public:
virtual ~A()
{
cout<<"A"<<endl;
};
};
class B :public A
{
public:
~B()
{
cout<<"B"<<endl;
};
};
int main()
{
A* a;
B b;
a=new B;
//a=&b;
delete a;
return 0;
}
运行结果:
B //有虚析构函数在析构a之前先调用b的析构函数
A
B //系统自己的析构函数
A
class B :public A
{
public:
~B()
{
cout<<"B"<<endl;
};
};
int main()
{
A* a;
B b;
a=new B;
//a=&b;
delete a;
return 0;
}
运行结果:
B //有虚析构函数在析构a之前先调用b的析构函数
A
B //系统自己的析构函数
A
#include<iostream>
using namespace std;
class A
{
public:
~A()
{
cout<<"A"<<endl;
};
using namespace std;
class A
{
public:
~A()
{
cout<<"A"<<endl;
};
};
class B :public A
{
public:
~B()
{
cout<<"B"<<endl;
};
};
int main()
{
A* a;
B b;
a=new B;
//a=&b;
delete a;
return 0;
}
运行结果:
A //在析构a之前并不调用b的析构函数,析构a
B //系统自己的析构函数,析构b
A
class B :public A
{
public:
~B()
{
cout<<"B"<<endl;
};
};
int main()
{
A* a;
B b;
a=new B;
//a=&b;
delete a;
return 0;
}
运行结果:
A //在析构a之前并不调用b的析构函数,析构a
B //系统自己的析构函数,析构b
A
4.抽象类
带有存虚函数的类就是抽象类
存虚函数的定义格式如下:
Virtual void area()=0;
只说明是统一的接口,而在派生类再给出函数的具体实现.
带有存虚函数的类就是抽象类
存虚函数的定义格式如下:
Virtual void area()=0;
只说明是统一的接口,而在派生类再给出函数的具体实现.