2008 March 29th Saturday (三月 二十九日 土曜日)
来源:互联网 发布:跳过广告的软件 编辑:程序博客网 时间:2024/04/30 09:46
#include <iostream>
The "virtual" key in C++ means from a base class, which might has parents, to its dervied classes, the "virtual" functions can be
inherited in interface and implement modes. It means its children can custom a virtual function's behavior as their wishes. Of course,
if a pure virtual function, children have to implement the interface.
using namespace std;
class A{
public:
void say(){
cout<<"A"<<endl;
}
};
class B: public A{
public:
virtual void say(){
cout<<"B"<<endl;
}
};
class C: public B{
public:
virtual void say(){
cout<<"C"<<endl;
}
};
int main(){
A a;
B b;
C c;
A *pa = &b;
pa->say(); // -> A
B *pb = &b;
pb->say(); // -> B
B *pc = &c;
pc->say(); // -> C
return 0;
}
As for non-virtual functions, it means that children inherit those function from parents and have better not modify those behaviors.
That likes the "final" key for a member in Java, but it isn't restricted at compilation time.
After you redefined a non-virtual member from parent class, invoking them, which member is called according to it belong to which class
object of a pointer pointed which class object. There is not dynamic virtual function table.
using namespace std;
class A{
public:
void say(){
cout<<"A"<<endl;
}
};
class B: public A{
public:
void say(){
cout<<"B"<<endl;
}
};
int main(){
A a;
B b;
A *pa = &b;
pa->say(); // -> A
B *pb = &b;
pb->say(); // -> B
return 0;
}
The "virtual" key in C++ means from a base class, which might has parents, to its dervied classes, the "virtual" functions can be
inherited in interface and implement modes. It means its children can custom a virtual function's behavior as their wishes. Of course,
if a pure virtual function, children have to implement the interface.
using namespace std;
class A{
public:
void say(){
cout<<"A"<<endl;
}
};
class B: public A{
public:
virtual void say(){
cout<<"B"<<endl;
}
};
class C: public B{
public:
virtual void say(){
cout<<"C"<<endl;
}
};
int main(){
A a;
B b;
C c;
A *pa = &b;
pa->say(); // -> A
B *pb = &b;
pb->say(); // -> B
B *pc = &c;
pc->say(); // -> C
return 0;
}
As for non-virtual functions, it means that children inherit those function from parents and have better not modify those behaviors.
That likes the "final" key for a member in Java, but it isn't restricted at compilation time.
After you redefined a non-virtual member from parent class, invoking them, which member is called according to it belong to which class
object of a pointer pointed which class object. There is not dynamic virtual function table.
using namespace std;
class A{
public:
void say(){
cout<<"A"<<endl;
}
};
class B: public A{
public:
void say(){
cout<<"B"<<endl;
}
};
int main(){
A a;
B b;
A *pa = &b;
pa->say(); // -> A
B *pb = &b;
pb->say(); // -> B
return 0;
}
- 2008 March 29th Saturday (三月 二十九日 土曜日)
- March 29th Thursday (三月 二十九日 木曜日)
- September 29th Saturday (九月 二十九日 土曜日)
- 2008 March 19th Wednesday (三月 十九日 水曜日)
- December 29th Saturday (十二月 二十九日 土曜日)
- March 19th Monday (三月 十九日 月曜日)
- 2008 March 26th Wednesday (三月 二十六日 水曜日)
- 2008 March 27th Thursday (三月 二十七日 木曜日)
- 2009 March 19th Thursday (三月 十九日 木曜日)
- 2008 March 21th Friday (三月 二十一日 金曜日)
- 2008 March 24th Monday (三月 二十四日 月曜日)
- 2008 March 25th Tuesday (三月 二十五日 火曜日)
- 2008 March 4th Tuesday (三月 四日 火曜日)
- 2008 March 5th Wednesday (三月 五日 水曜日)
- 2008 March 7th Friday (三月 七日 金曜日)
- 2008 March 10th Monday (三月 十日 月曜日)
- 2008 March 11th Tuesday (三月 十一日 火曜日)
- 2008 March 14th Thursday (三月 十四日 木曜日)
- Subvertion+apache2.2.8+winxp
- 2008 March 26th Wednesday (三月 二十六日 水曜日)
- 清除AD过期的帐户和计算机
- 2008 March 27th Thursday (三月 二十七日 木曜日)
- 发布一个用于和GridView或DataGrid表格相关联的查找搜索服务器控件
- 2008 March 29th Saturday (三月 二十九日 土曜日)
- 向父页面提交内容并关闭子页面
- 2008 March 31th Monday (三月 三十一日 月曜日)
- 照相机的基本知识
- [顶]学黑客必去的网站“黑客基地”
- uClinux在S3C44B0X上的移植
- 用C/C++写上传文件的CGI,遇到问题
- 摄影用光
- [转]继承设计技巧