64,281
社区成员
发帖
与我相关
我的任务
分享
private:
Father(Father&);
public:
Father() { cout << "default constructor called!"<< endl;};
Father(Father& f) { cout << "copy constructor called!" << endl; };
class Child:public{
void sayHello(){
cout<<"hello Child!!"<<endl;
}
class Child: public father
void sayHello(){
cout<<"hello Child!!"endl;
}
#include <iostream>
using namespace std;
class Father{
public:
virtual void sayHello(){
cout<<"hello Father!!"<<endl;
}
~Father(){
cout<<"~Father()"<<endl;
}
};
class Child:public Father{
public:
void sayHello(){
cout<<"hello Child!!"<<endl;
}
~Child(){
cout<<"~Child()"<<endl;
}
};
void SayHello(Father f,Father& f1,Father* pF){
f.sayHello();//执行Father的sayHello
f1.sayHello();//多态,本题执行Child的sayHello
(*pF).sayHello();//多态,本题执行Child的sayHello
}//析构f,析构函数不是virtual的,执行Father的析构
void main(){
Child c;//构造Child对象c
SayHello(c,c,&c);//调用时,第一个参数按成员copy
Father* f=new Child;//构造Child对象
delete f;//析构f,析构函数不是virtual的,执行Father的sayHello
}//执行c的析构,先执行Child的析构,再执行Father的析构