首页 > 其他分享 >final和override

final和override

时间:2023-12-04 17:00:28浏览次数:32  
标签:show void public Person Student override final

文章参考:爱编程的大丙 (subingwen.cn)

1. final

C++11中提供了final关键字。

作用:

  • 写在类名后面,限制类不能被继承。
  • 写在函数后面,限制函数被重写。

1.1 限制函数

final限制的函数无法被重写。

#include <iostream>
#include <cstring>
using namespace std;

class Person{
public:
    Person(){}
    virtual void show();
};

class Student: public Person{
public:
    Student(){}
    void show() final;
};
void Student::show(){
    cout << "Student" << endl;
}

class Pupil: public Student{
public:
    Pupil(){}
    // void show();     因为Student中对该虚函数使用了final关键字,因此此处无法进行重写
};

在上述案例中,Person中的虚函数在派生类中被重写,且加上了final关键字,组织了Student的派生类Pupil继续对该函数进行重写。

1.2 限制类

final限制的类无法被继承。

class Person final{
public:
    Person(){}
    virtual void show();
};
// Student类无法通过编译
// class Student: public Person{};

在上述案例中,Student类无法通过编译,因为Person被final限制,无法被继承。

2. override

override关键字用于确保派生类中生命的重写函数和虚函数拥有相同的签名,同时明确表示将会重写虚函数,从而保证重写虚函数的正确性,提高代码可读性。和final一样,override要写在函数后面。

class Person{
public:
    Person(){}
    virtual void show();
};

class Student: public Person{
public:
    Student(){}
    void show() override;
};
void Student::show(){
    cout << "Student" << endl;
}

标签:show,void,public,Person,Student,override,final
From: https://www.cnblogs.com/beasts777/p/17875385.html

相关文章

  • jfinal2.2在idea用main运行注意事项
    1.用new->projectfromexistssource方式,用maven方式导入2.jfinal2.2使用8.1.8的jettyserver,pom使用compile<dependency><groupId>com.jfinal</groupId><artifactId>jetty-server</artifactId><version>8.1.8</version&g......
  • Apple Final Cut Pro 10.7 - 专业后期制作 (视频编辑)
    AppleFinalCutPro10.7-专业后期制作(视频编辑)FinalCutPro10.7+Compressor4.7+Motion5.7(Universal)请访问原文链接:https://sysin.org/blog/apple-final-cut-pro/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgFinalCutPro强大新功能将Mac与......
  • JFinal启动成功之后,使用localhost访问浏览器界面,显示404(之前是可以滴~)
    问题描述问题解决应该是我没有设置只输入localhost弹出的浏览器的html页面内容;然后我只需要调用到localhost/student/,就显示出来相应的界面啦~~~说白了就是路径问题,,......
  • [转]Java 读取 FinalShell 已保存的密码
    转自:https://blog.csdn.net/Linkaias/article/details/133805438也可以找一些在线解密网站来查看密码。 FinalShell可用来连接到Linux服务器,官网:https://www.hostbuf.com/?install_fspackageorg.example;importjava.io.ByteArrayOutputStream;importjava.io.DataOu......
  • 【Vulnhub 靶场】【DriftingBlues: 9 (final)】【简单】【20210509】
    1、环境介绍靶场介绍:https://www.vulnhub.com/entry/driftingblues-9-final,695/靶场下载:https://download.vulnhub.com/driftingblues/driftingblues9.ova靶场难度:简单发布日期:2021年05月09日文件大小:738MB靶场作者:tasiyanci靶场描述:getflags打靶耗时:2+小时打靶关键:......
  • JFinal框架入门版本
    项目结构具体代码//DemoConfig.javapackagecom.demo.config;importcom.demo.controller.HelloController;importcom.jfinal.config.*;importcom.jfinal.template.Engine;publicclassDemoConfigextendsJFinalConfig{@OverridepublicvoidconfigConst......
  • 求解--JFinal项目启动之后在浏览器显示不了的问题(未解决)
    问题描述问题解决......
  • JFinal具体如何划分?
    包的具体划分JFinal具体分为controller,config,model,Interceptor,service,validator这几类;各部分具体放什么东西(具体职责)controller--与spring的controller相似,放置具体操作;model--放置Student,定义核心代码;interceptor--拦截器,实现权限登录;service--所有的dao对象的定义放进去;v......
  • [HZNUCTF 2023 final]虽然他送了我玫瑰花
    打开界面看到爆红的代码 浏览整个代码没有发现明显的call指令爆红,那就只能慢慢看了发现一个地方出现永真跳转代码  这里的话直接给他nop掉就好了然后在上面选中mian函数p键就好了 另外想讲一下的点是这几个函数都是可以分析的 键入函数 F5变为伪代码 ......
  • try···finally执行
    代码publicstaticvoidmain(String[]args){System.out.println(test());}publicstaticinttest(){inta=1;try{returna;}finally{++a;}}打印结果print输出还是1......