一、虚函数
1.虚函数的作用
主要是实现了多态的机制,简而言之就是用父类(基类)的指针指向子类(派生类)的对象,然后通过父类(基类)的指针调用不同子类(派生类)的对象的不同函数,这就是一种泛型.
虚函数通过继承,来实现多态(虚函数动态绑定),最后达到封装的效果
一个公有派生类对象可以赋值给基类的对象,但是基类的对象不能赋值给派生类的对象
虚函数一般用于基类成员函数来绑定派生类成员函数一起调用
2.虚函数的使用
//1.在基类用virtual声明成员函数为虚函数,
class Base{virtual void A();
};//在类外定义虚函数无需加virtual
void Base::A(){}//2.在派生类重新定义虚函数,要求函数名/函数类型/函数参数的类型和个数保持一致
class Derive : pubic Base{//虚函数在派生类重新声明时,无需加virtualvoid A();
};//3.定义一个指向基类对象的指针变量,并使用基类对象的指针变量指向不同的派生类对象的函数
void main(){Base* p=new Derive();p->A();
}
class Base {
public:Base() {echo();}virtual void echo() {printf(“Base”);}
};class Derived:public Base {
public:Derived() {echo();}virtual void echo() {printf(“Derived”);}
};int main() {Base* base = new Derived();base->echo(); //输出Base Derived Derivedreturn 0;
}
二.虚析构函数
虚析构函数的作用是delete动态对象时释放资源
//test.h
class A{public:char* strA;A(char* a){strA=new char[12];strncpy(strA,a,strlen(a));}virtual ~A(){ //不加virtual会报错delete strA;}
};class B:public A{public:char* strB;B(char* a):A(a){strB=new char[12];strncpy(strB,a,strlen(a));}~B(){delete strB;}
};//test.cpp
int main(){char input[]="Hello";A* a=new B(input);delete[] a;system("pause");return 0;
}
四.动态绑定
#include <iostream>
using namespace std;
class A
{
public:virtual void func(int val = 1){std::cout " << val << std::endl;}virtual void test(){func();}
};
class B : public A
{
public:void func(int val = 0){std::cout " << val << std::endl;}
};
int main(int argc, char* argv[])
{A*p1 = new A;A*p2 = new B;//B*p3 = new A; //errorB*p3 = reinterpret_cast<B*> (new A);B*p4 = new B;//测试test()p1->test(); //A->1p2->test(); //B->1p3->test(); //A->1p4->test(); //B->1//测试func()p1->func(); //A->1p2->func(); //B->1p3->func(); //A->0p4->func(); //B->0return 0;
}
#include <iostream>
using namespace std;
class A
{
public:void func(int val = 1){std::cout " << val << std::endl;}//这个test()的virtual可有可无virtual void test(){func();}
};
class B : public A
{
public:void func(int val = 0){std::cout " << val << std::endl;}
};
int main(int argc, char* argv[])
{A*p1 = new A;A*p2 = new B;//B*p3 = new A; //errorB*p3 = reinterpret_cast<B*> (new A);B*p4 = new B;//test()p1->test(); //A->1p2->test(); //A->1p3->test(); //A->1p4->test(); //A->1//func()p1->func(); //A->1p2->func(); //A->1p3->func(); //B->0p4->func(); //B->0return 0;
}
