【无标题】

复制代码
#include <iostream>
using namespace std;

class Father
{
  public:
    Father() { A(); }
     void virtual A() { cout<<"Father::A()"<<endl; }
     void B() { cout<<"Father::B()"<<endl; A();}
};
class Son : public Father
{
  public:
    void A() { cout<<"Son::A()"<<endl; }
    void B() { cout<<"Son::B()"<<endl; }
};
int main()
{
  Father f;//father::A
  Son s;//father::a!!!
  Father *pf=&s;
  pf->A();
 
  pf->B();
  s.A();//son::a
  s.B();//son::b
}

关注到如果虚函数是在Father()的构造函数中,是不体现虚特性的,而在其它地方是体现的,比如关注void B()中的A()

相关推荐
初夏睡觉12 分钟前
从0开始c++,但是重置版,第1篇(c++基本框架)
开发语言·c++
前端小白在前进16 分钟前
⭐力扣刷题:螺旋矩阵
算法·leetcode·矩阵
老赵聊算法、大模型备案5 小时前
北京市生成式人工智能服务已备案信息公告(2025年12月11日)
人工智能·算法·安全·aigc
workflower5 小时前
时序数据获取事件
开发语言·人工智能·python·深度学习·机器学习·结对编程
CoderYanger6 小时前
C.滑动窗口-求子数组个数-越长越合法——2799. 统计完全子数组的数目
java·c语言·开发语言·数据结构·算法·leetcode·职场和发展
C++业余爱好者6 小时前
Java 提供了8种基本数据类型及封装类型介绍
java·开发语言·python
厕所博士6 小时前
红黑树原理前置理解—— 2-3 树
算法·2-3树·红黑树原理理解前置
林杜雨都6 小时前
Action和Func
开发语言·c#
皮卡龙6 小时前
Java常用的JSON
java·开发语言·spring boot·json
火山灿火山7 小时前
Qt常用控件(三)
开发语言·qt