虚函数的使用 以及虚函数与重载的关系, 空虚函数的作用,纯虚函数->抽象类,基类虚析构函数使释放对象更彻底

为了访问公有派生类的特定成员,可以通过讲基类指针显示转换为派生类指针。

也可以将基类的非静态成员函数定义为虚函数(在函数前加上virtual

 #include<iostream>
using namespace std; class base{
public:
/*virtual*/ void who(){ //define this function to virtual will be normal
cout << "this is the class of base !" << endl;
}
}; class derive1:public base{
public:
void who(){
cout << "this is the class of derive1 !" << endl;
}
};
class derive2 :public base{
public:
void who(){
cout << "this is the class of derive2 !" << endl;
}
};
int main(){
base beseObject, *p;
derive1 obj1;
derive2 obj2;
p = &beseObject;
p->who();
cout << "------------------------------" << endl;
p = &obj1;
p->who();
((derive1*)p)->who();
cout << "------------------------------" << endl;
p = &obj2;
p->who();
((derive2*)p)->who();
cout << "------------------------------" << endl;
obj1.who();
obj2.who();
int i;
cin >> i;
return ;
}

两次结果比较: 没加virtual:虚函数的使用 以及虚函数与重载的关系, 空虚函数的作用,纯虚函数->抽象类,基类虚析构函数使释放对象更彻底;加virtual后:虚函数的使用 以及虚函数与重载的关系, 空虚函数的作用,纯虚函数->抽象类,基类虚析构函数使释放对象更彻底

与重载的关系:

 #include<iostream>
using namespace std; class base{
public:
virtual void f1(){ //virtual function
cout << "f1 function of base " << endl;
}
virtual void f2(){ //virtual function
cout << "f2 function of base " << endl;
}
virtual void f3(){ //virtual function
cout << "f3 function of base " << endl;
}
void f4(){
cout << "f4 function of base " << endl;
}
}; class derive:public base{
public:
void f1(){ //virtual function
cout << "f1 function of derive " << endl;
}
virtual void f2(int x){ //lose virtual characteristic
cout << "f2 function of derive " << endl;
}
//f3(){ //wrong, not the same return type
// cout << "f3 function of base " << endl;
//}
void f4(){ //normal overload
cout << "f4 function of derive " << endl;
}
};
int main(){
base obj1, *ptr;
derive obj2;
ptr = &obj1;
ptr->f1();
ptr->f2();
ptr->f3();
ptr = &obj2;
ptr->f1();
ptr->f2();
ptr->f4();
int i;
cin >> i;
return ;
}

结果:

虚函数的使用 以及虚函数与重载的关系, 空虚函数的作用,纯虚函数->抽象类,基类虚析构函数使释放对象更彻底

空虚函数在中间类里必须声明,以保证其后的派生类能使用该虚函数。建立一条从虚函数到派生类的虚函数路径。

 #include<iostream>
using namespace std; class base{
public:
virtual void print(){
cout << "calss base!" << endl;
}
};
class son:public base{
public:
virtual void print(){ //empty virtual class
}
};
class grandson :public son{
public:
void print(){
cout << "calss grandson!" << endl;
}
};
void show(base* b){
b->print();
}
int main(){
base *pbase = new base;
son *pson = new son;
grandson *pgrandson = new grandson;
show(pbase);
show(pson);
show(pgrandson);
int i;
cin >> i;
return ;
}

结果:虚函数的使用 以及虚函数与重载的关系, 空虚函数的作用,纯虚函数->抽象类,基类虚析构函数使释放对象更彻底

存虚函数与抽象类

 #include<iostream>
using namespace std; class shape{ //抽象类里必须有一个纯虚函数
public:
virtual float area() = ;
};
class triangle :public shape{
protected:
float h, w;
public:
triangle(float hh, float ww){
h = hh; w = ww;
}
float area(){
return h*w*0.5;
}
};
class rectangle :public shape{
protected:
float h, w;
public:
rectangle(float hh, float ww){
h = hh; w = ww;
}
float area(){
return h*w;
}
};
float total(shape* s[], int n){
float sum = ;
for (int i = ; i < n - ; i++)
sum += s[i]->area();
return sum;
}
int main(){
shape* s[];
s[] = new triangle(, );
s[] = new rectangle(, );
float sum = total(s, );
cout <<"total area is: "<< sum << endl;;
int i;
cin >> i;
return ;
}

结果:算出总面积 为 6

上一篇:C++的抽象类、虚函数、虚基类和java的抽象类和接口


下一篇:python全栈开发中级班全程笔记(第二模块、第三章)(员工信息增删改查作业讲解)