博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++函数重载(5) - 函数重载在类继承中的行为
阅读量:4071 次
发布时间:2019-05-25

本文共 1879 字,大约阅读时间需要 6 分钟。

目录


本篇做了一些非常有趣的验证,在不同语言版本之间,探索一下函数重载,在类继承中的行为表现。

1.C++版本

如果子类中的函数名称与基类中的某个成员函数相同,则子类对象能否调用这个基类的函数?

#include 
using namespace std;class Base{public: int f(int i) { cout << "f(int): "; return i+3; }};class Derived : public Base{public: double f(double d) { cout << "f(double): "; return d+3.3; }};int main(){ Derived* dp = new Derived; cout << dp->f(3) << '\n'; cout << dp->f(3.3) << '\n'; delete dp; return 0;}

输出结果:

f(double): 6.3
f(double): 6.6

而不是预期的结果:

f(int)  : 6
f(double): 6.6

结论:C++中,重载对于子类是无效的。

因为基类与子类之间没有重载的关系。对于这个程序,编译器只会在子类范围中查找,会找出函数“double f(double)”并调用它,绝对不会查找到基类的范围中。
C++中没有跨范围的重载,这个例子并不是重载规则中的某种特殊情况。

2.Java版本

同时,我们试一下Java版本的重载看看。 just for funny coding  :-) 

class Base{    public int f(int i)    {        System.out.print("f (int): ");        return i+3;    }}class Derived extends Base{    public double f(double i)    {        System.out.print("f (double) : ");        return i + 3.3;    }}class myprogram3{    public static void main(String args[])    {        Derived obj = new Derived();        System.out.println(obj.f(3));        System.out.println(obj.f(3.3));    }}

输出结果:

f (int): 6
f (double): 6.6 

结论:Java的重载跨越了类的继承范围。Java编译器在编译期间,调用了对应的重载函数,这是基于参数类型实现的。

3.C#版本

  那就也试试C#版本的吧。 :-)

using System;                    class Base{    public int f(int i)    {        Console.Write("f (int): ");        return i + 3;    }}class Derived : Base{    public double f(double i)    {        Console.Write("f (double) : ");        return i+3.3;    }}class MyProgram{      static void Main(string[] args)    {        Derived obj = new Derived();        Console.WriteLine(obj.f(3));        Console.WriteLine(obj.f(3.3));        Console.ReadKey(); // visual studio使用,用于halt console,类似C/C++的getch.    }}

输出结果:

f(double) : 6.3
f(double) : 6.6 

结论:C#与C++类似,在基类与子类之间没有重载功能。在C#中,没有跨类范围的重载,所以它并不是重载规则的一个特殊情况。

 

转载地址:http://rmeji.baihongyu.com/

你可能感兴趣的文章
hd cdnServer 51cdn / ChinaCache / ATS / Apache Traffic Server
查看>>
project web architecture
查看>>
OS + Unix HP-UX
查看>>
OS + Unix Solaris / openSolaris
查看>>
db sql montior
查看>>
Unix + SCO UnixWare
查看>>
db db2 books
查看>>
read humor_campus
查看>>
IBM WebSphere Commerce Analyzer
查看>>
Unix + OS IBM Aix System Director
查看>>
Unix + OS IBM Aix FTP / wu-ftp / proftp
查看>>
my read work
查看>>
db db2 base / instance database tablespace container
查看>>
hd disk / disk raid / disk io / iops / iostat / iowait / iotop / iometer
查看>>
project ASP.NET
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
OS + Unix Aix telnet
查看>>
IBM Lotus
查看>>
Linux +Win LAMPP Tools XAMPP 1.7.3 / 5.6.3
查看>>
my read_university
查看>>