c++ 考点整理
函数重载
定义:函数名称相同,形参类型或者形参数目不同的函数称为函数重载
注:对返回值类型无要求,返回值类型可以相同可以不同,但是形参类型或者数目必须存在不同,能够帮助函数调用识别函数进入的入口。
类继承中的问题
构造函数调用顺序:基类成员变量-->基类构造函数-->派生类成员变量-->派生类构造函数
析构函数顺序:(与构造相反)
动态分配类型转换时,delete结果不会调用派生类的析构函数
class shape{
public:
shape(){
cout<<"shape()"<<endl;
}
~shape(){
cout<<"~shape()"<<endl;
}
};
class Side{
public:
int a;
int b;
Side(){cout<<"Side()"<<endl;}
~Side(){cout<<"~Side()"<<endl;}
};
class Rectangle: public shape{
private:
Side s;
public:
Rectangle(){
cout<<"Rectangle()"<<endl;
}
~Rectangle(){
cout<<"~Rectangle()"<<endl;
}
};
int main(){
Rectangle r1;
shape *p = new Rectangle;
delete p;
return 0;
}
运行结果:
shape()
Side()
Rectangle()
//以上是r1构造函数生成顺序
shape()
Side()
Rectangle()
~shape()
//以上是p分配空间到delete p的过程
~Rectangle()
~Side()
~shape()
//以上是r1析构函数生成顺序

浙公网安备 33010602011771号