C++覆盖、重载、多态区别 ; 重载、重写(覆盖)、重定义(隐藏)三者区别

详解C++中重载(Overload)、重写(Override,也称覆盖)和重定义(Redefine,也称隐藏)的区别 本文系统解析了C++中重载(Overload)、重写(Override)和重定义(Redefine)的核心区别。重载发生在同一作用域内,要求函数名相同但签名不同;重写是派生类对基类虚函数的重新实现,实现运行时多态;重定义则是派生类定义与基类同名函数导致基类版本被隐藏。三者在作用域、函数签名要求、多态类型和调用规则等方面存在显著差异,理解这些差异对编写高效的面向对象程序至关重要。 阅读详情
覆盖:
在基类中定义了一个非虚拟函数,然后在派生类中又定义了一个同名同参数同返回类型的函数,这就是覆盖了。
在派生类对象上直接调用这个函数名,只会调用派生类中的那个。

//coverage.cpp

 

#include <iostream>

using namespace std;

 

class A

{

public:

  void ShowMessage();

};

class B:public A

{

public:

  void ShowMessage();

};

 

void A::ShowMessage()

{

  cout<<"Hello,This is A./n";

  return;

}

void B::ShowMessage()

{

  cout<<"Hello,This is B./n";

  return;

}

 

int main()

{

  A* p;

  B memb;

  *p = memb;

  p->ShowMessage();

  memb.ShowMessage();

  return 0;

}

 

输出为:
Hello,This is A.
Hello,This is B.
 
重载:

有两个或多个函数名相同的函数,但是函数的形参列表不同。在调用相同函数名的函数时,根据形参列表确定到底该调用哪一个函数。

//reload

 

#include <iostream>

using namespace std;

 

class A

{

public:

  void ShowMessage();

  void ShowMessage(string str);

};

 

void A::ShowMessage()

{

cout<<"Hi,This is A./n";

  return;

}

void A::ShowMessage(string str)

{

  cout<<str<<endl;

  return;

}

 

int main()

{

  A mem;

  mem.ShowMessage();

  mem.ShowMessage("Hello.How are you?/n");

  return 0;

}

输出为:
Hi,This is A.
Hello.How are you?
 
多态:
在基类中定义了一个虚拟函数,然后在派生类中又定义一个同名,同参数表的函数,这就是多态。多态是这3种情况中唯一采用动态绑定技术的一种情况。也就是说,通过一个基类指针来操作对象,如果对象是基类对象,就会调用基类中的那个函数,如果对象实际是派生类对象,就会调用派声雷中的那个函数,调用哪个函数并不由函数的参数表决定,而是由函数的实际类型决定。

//poly.cpp

 

#include <iostream>

using namespace std;

 

class A

{

public:

   virtual void ShowMessage();

};

class B:public A

{

public:

  void ShowMessage();

};

 

void A::ShowMessage()

{

  cout<<"This is A./n";

  return;

}

void B::ShowMessage()

{

  cout<<"This is B./n";

  return;

}

 

int main()

{

  A* p;

  p=new A();

  p->ShowMessage();

  p=new B();

  p->ShowMessage();

  return 0;

}

输出为:
This is A.
This is B.

----------------------------------------------------------------------------------------------------------------------------

1、重载

首先说重载,它最简单,C++中的重载和java中的差不多,都是在一个类中,方法名相同而参数不同的几个方法,但是不能靠返回类型来判断。例如:

class AA
{
public:
    void print()
    {
        cout << "父类" << endl;
    }
    void print(int x )
    {
        cout << "父类:" << x  << endl;
    }
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

上面的两个方法就属于重载。

2、重定义

重定义 (redefining)也叫做隐藏:

子类重新定义父类中有相同名称的非虚函数 ( 参数列表可以不同 ) 。


class AA
{
public:
    void print()
    {
        cout << "父类" << endl;
    }
};
class BB : public AA
{
public:
    void print(int x)//这叫重定义,此时A类中的print()被隐藏
    {
        cout << "子类:" << x  << endl;
    }
};

void main()
{
    int x = 1;
    BB b; //子类
    AA a; //父类
    a.print();//访问父类的print()
    b.print(x);//访问子类的print()
    b.AA::print();//访问父类的print()
    //b.print();error:函数参数太少
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

这种情况下print()和print(int x)叫作重定义,在重定义时,父类的print()方法被隐藏了,要想使用父类的方法必须通过::。

3、重写(也叫覆盖)

函数特征相同。但是具体实现不同,主要是在继承关系中出现的 。当我们对别人提供好的类的方法感觉不是太满意时,我们就可以通过继承这个类然后重写其方法改成我们需要的逻辑。

1、最重要的一点,重写是子类与父类之间的。

2、被重写的函数不能是 static 的。

3、函数三要素(函数名、函数参数、函数返回类型)完全一样

4、如果父类中有virtual关键字,这种父子之间的关系叫做虚函数重写,这也就是C++中的多态机制,和java自动转换不同的是,C++需要我们手动设置。

例如:

class AA
{
public:
    virtual void print(int x )
    {
        cout << "父类:" << x  << endl;
    }
};

class BB : public AA
{
public:
    virtual void print(int x)
    {
        cout << "子类:" << x  << endl;
    }
};

int main()
{
    AA *p = NULL;
    BB b; 
    p = &b;
    p->print(1);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

这个输出结果取决于AA类中的void print(int x )函数前面是否加入virtual关键字,如果加了则调用子类的,否则父类。


重载、覆盖(重写)、重定义(同名隐藏)的区别 (超详解) 重载(Overloading)、覆盖(Overriding)、重定义(Hiding)是面向对象编程中常见的概念,它们分别用于描述不同情况下函数或方法的行为。 阅读详情

相关推荐

C++中重载(overload)、重写(override,也叫做“覆盖”)和重定义(redefine)的区别?

1)重载(overload): 指函数名相同,但是它的参数表列个数或顺序,类型不同。但是不能靠返回类型来判断。    a 相同的范围(在同一个类中)    b 函数名字相同、 参数不同    c virtual关键字可有可无    d 返回值可以不同; 2) 重写(覆盖override)是指派生类函数覆盖基类函数,特征是:    a 不同的范围,分别位于基类和派生类中    b 函

happywq2009的专栏 1998

【C++】多态 ⑤ ( 重载 | 重写 | 重定义 )

一、重载、重写、重定义概念 1、重载 Overloading 2、重写 Overriding 3、重定义 Redefinition

让 学习 成为一种 习惯 ( 韩曙亮 の 技术博客 ) 1895

ModuleHotReload:C ++模块类热重装,可按需重新编译

ModuleHotReload 具有按需重新编译的C ++模块/类热重载。

C++三大特性- - -多态(虚函数、重载、重写和重定义)

文章目录多态1. 多态的定义及实现1.1 多态构成条件1.2 虚函数1.3 虚函数的重写1.4 c++11中的 override 和 final1.5 重载、覆盖(重写)、隐藏(重定义)的区别2、抽象类2.1 纯虚函数2.2 接口继承和实现继承3、多态的原理3.1 虚函数表 多态 多态:通俗说就是多种形态,具体点就是去完成某个行为,当不同的对象去完成时会产生出不同的状态。 1. 多态的定义及实现 1.1 多态构成条件 多态是在不同继承关系的类对象,去调用同一函数,产生了不同的行为。比如Student继承了P

qq_42433334的博客 1563

SPEI计算避坑指南:gma中Distribution和FitMethod参数怎么选?

本文深入探讨了在SPEI计算中如何科学选择gma库中的Distribution和FitMethod参数,帮助研究者避开常见陷阱。详细分析了LogLogistic、Gamma和Pearson3三种分布的适用场景,以及LMoment与MLE拟合方法的优缺点,为气象干旱研究提供实用指南。

weixin_30308101的博客 250

体验灵雀云-创建基于Docker的云服务

今天体验了一下灵雀云,创建了第一个基于Docker的云服务。 1.登录灵雀云 2.开始使用 登录之后,从"控制台"进入操作界面,开始创建服务。 首先需要选择一个镜像。灵雀云已经为您提供了一些常用的镜像列表。请在“常用镜像”列表中,选择“其它”,然后找到“alauda/hello-world”镜像,点击它右侧的“选择”按钮,进入服务参数配置页面。 选择完要创建的

tomcat的专栏 3234

学懂C++ (十一):详解C++ 函数覆盖与重载的区别

详解C++ 函数覆盖与重载的区别

编程技术探索者,分享C/C++、C#、Java、数据库等开发经验,聚焦实战技巧与AI兴趣,助力编程爱好者成长。 1671

变压器耦合放大器(低频应用+高频应用)

变压器耦合放大器(低频应用+高频应用)

m0_54943120的博客 2275

C++ 覆盖方法与重载方法

覆盖方法(overriding) 什么是函数的覆盖方法 覆盖方法用于同一函数的重写 —— 派生类函数覆盖基类同名函数 覆盖方法重写(覆盖)了一个方法,以实现不同的功能。一般是用于子类在继承父类时,重写(重新实现)父类中的方法 覆盖方法的特征: 范围不同(分别位于 基类 和 派生类中) 函数名字相同 函数参数相同 基类函数必须有virtual(虚函数)关键字 为什么需要覆盖方法: 在 C++ 里,当我们需要在 基类里提供一个通用的函数,但是在它的某个子类里,需要修改这个方法(通用函数)的实现,就要用到覆盖方

Chroniccandy的博客 2154

RS-485调试踩坑实录:MAX13487数据回环问题排查与修复(附电路图)

本文深入解析了RS-485通信中MAX13487芯片的数据回环问题,提供了详细的排查过程和解决方案。通过分析芯片特性和自动换向机制,揭示了RO引脚配置不当是导致问题的关键原因,并给出硬件(添加上拉电阻)和软件(调整MCU配置)两种修复方案,附有优化设计建议和实际调试技巧。

play7的博客 1299

药物代谢仿真软件:PKSim_(6).案例研究与实践应用

通过本章的案例研究和实践应用,我们展示了如何利用PKSim软件构建药物动力学模型、进行参数估计、仿真预测和结果分析。这些方法不仅有助于优化药物代谢研究过程,还可以在药物研发中提供重要的数据支持。通过模型的构建和验证,我们可以更好地理解药物在体内的动态变化过程,为临床试验和药物治疗提供科学依据。

kkchenjj的博客 655

【C++入门笔记】覆盖(override)方法和重载(overload)方法

前言 通过前面的学习,我们已经知道了如何通过创建一个新的子类来重用现有的代码(继承)。 但是如果我们需要在基类里提供一个通用的函数,但在它的某个子类里需要修改这个方法的实现,在C++里,覆盖(overriding)就可以做到。 覆盖方法 C++可以让我们很容易实现这种既有共同特征又需要在不同的类里面有不同实现的方法。 我们需要做的就是在类里重新声明这个方法,然后再改写一下它的实现代码(就...

Jeloys的博客 865

知识图谱(Knowledge Graph)原理与代码实战案例讲解

知识图谱(Knowledge Graph)原理与代码实战案例讲解 作者:禅与计算机程序设计艺术 / Zen and the Art of Computer Programming 1. 背景介绍 1.1 问题的由来

AI天才研究院 1078

菜鸟学C++ (分清楚C++中重载、重写和重定义)

1、背景 在C++里有必要弄清楚他们三者的不同。 1.1 参考资料 https://www.cnblogs.com/weizhixiang/articles/5760286.html 2、三者的不同详细 重载(overload):是函数名相同,参数列表不同,override只是在类的内部存在。 重写(override),也叫覆盖。子类重新定义父类中有相同名称和参数的虚函数(virtua...

zhejfl的博客 2万+

Arduino与震动马达模块握手

震动马达模块采用采用高品质手机震动马达,适用于震动感互动产品的制作,可穿戴智能设备震动提醒,适合作为非可听指示器的小型振动马达。当输入为高电平时电动机会震动,就像你的静音模式的手机一样。

vor234的博客 5847

云原生监控:Prometheus+Grafana监控Nginx实战

在云原生环境中,应用的高可用性和性能优化至关重要。Nginx作为一款广泛使用的高性能Web服务器、反向代理服务器及电子邮件(IMAP/POP3)代理服务器,对其进行有效的监控能够及时发现潜在问题,保障服务的稳定运行。本文章的目的是详细介绍如何使用Prometheus和Grafana构建一套完整的监控系统来监控Nginx,范围涵盖从环境搭建、数据采集、存储到可视化展示的整个流程。

AI云原生与云计算技术学院 1574

c++三大概念要分清--重载,隐藏(重定义),覆盖(重写)

重载,隐藏(重定义),覆盖(重写)—这几个名词看着好像很像,不过其实一样都不一样!!综述:一、重载:(1) 概念:在同一个作用域内;函数名相同,参数列表不同(参数个数不同,或者参数类型不同,或者参数个数和参数类型都不同),返回值类型可相同也可不同;这种情况叫做c++的重载!注意:c语言没有函数重载的机制;详见:http://blog.youkuaiyun.com/gogogo_sky/article/deta

1万+

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

C++ -- 重载、重定义、重写的区别

一.重载1.在同一个作用域下,函数名相同,函数的参数不同(参数不同指参数的类型或参数的个数不相同)2.不能根据返回值判断两个函数是否构成重载。3.当函数构成重载后,调用该函数时,编译器会根据函数的参数选择合适的函数进行调用。4.构成重载的例子:#include&lt;iostream&gt;using namespace std;int Add(int a, int b){      return...

我的博客 1万+

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C++的重写(覆盖)、重载、重定义(隐藏)、多态

“重写(覆盖)、重载、重定义、多态” 绝对称得上是C++里一个比较经典的问题了,下面我们来层层剖析它。 重写(覆盖)override 重写又名覆盖,常见于子类继承父类的时候,子类重写父类的某些方法。 有如下要求: 父类中被重写的方法必须是virtual的 子类中进行重写操作的那个方法,访问修饰符和父类的可以不同(即根据自身需要选择public、protected、private), 但是返回值...

吟游诗人——吟唱生命的不朽 1318

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

【C++】重载、重写、重定义的辨析

重载、重写、重定义对比 一、重载(overload) 二、重写 / 覆盖(override) 三、重定义 / 隐藏(redefining) 为什么在虚函数中不能使用 static 关键字? 重写函数的访问修饰符可以不同吗?

m0_74195626的博客 2001

C++重写(覆盖)、重载、重定义、多态

1 重写(覆盖)override override是重写(覆盖)了一个方法,以实现不同的功能。一般用于子类在继承父类时,重写(覆盖)父类中的方法。函数特征相同,但是具体实现不同。 重写需要注意: 被重写的函数不能是static的,必须是virtual的 重写函数必须有相同的类型,名称和参数列表 重写函数的访问修饰符可以不同。尽管virtual是private...

yihuanyihuan的博客 1875

C++多态与多态原理以及重载和重写(覆盖)的区别

C++多态与多态原理以及虚函数表多态多态原理重载和重写(覆盖)的区别 多态 多态分为两类:静态多态(编译时多态)和动态多态(运行时多态)   静态多态就是我们所熟悉的函数重载和运算符重载,而派生类和虚函数实现动态多态。   静态多态和动态多态的区别就是函数地址是早绑定(静态联编)还是晚绑定(动态联编)。如果函数的调用,在编译阶段就可以确定函数的调用地址,并产生代码,就是静态多态(编译时多态),就是说地址是早绑定的。而如果函数的调用地址不能编译不能在编译期间确定,而需要在运行时才能决定,这这就属于晚绑定(动态

kuanyew的博客 3984

C++中的覆盖(重写)、重载、隐藏(重定义)、多态!

C++中的覆盖(重写)、重载、隐藏、多态!

魏波 2387

C++重载重写重定义的区别及多态的概念

1.重载和重写的区别: 重载overload:在同一个类中,函数名相同,参数列表不同,编译器会根据这些函数的不同参数列表,将同名的函数名称做修饰,从而生成一些不同名称的预处理函数,未体现多态。   重写override:也叫覆盖,子类重新定义父类中有相同名称相同参数的虚函数,主要是在继承关系中出现的,被重写的函数必须是virtual的,重写函数的访问修饰符可以不同,尽管virtual是pri

u012043391的专栏 3848

函数的重载、重写和重定义简述(c/c++知识篇)

1.函数重载(Function Overloading) 1、什么是函数重载 在同一个类中(同一个作用域中/在类的内部),存在一组函数名相同,函数的参数列表不同(参数的个数、类型、顺序),函数有无 virtual 关键字都可以,我们把这组函数称为函数重载。 2、为什么使用函数重载(函数重载的好处 由于函数重载可以在同一个作用域内,使用同一个函数名 命名一组功能相似的函数,这样做减少了函数名的数量,避免了程序员因给函数名命名所带来的烦恼,从而提高程序的开发的效率。 3、函数重载的条件 1 必须在同一

栋哥修炼日记 1万+
上一篇: 深入理解C++中public、protected及private用法
下一篇: #ifdef _DEBUG用法小结
SYP35
博客等级 码龄12年 26粉丝 88原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值