Overload and override in c++
2017-05-27 15:52
459 查看
Overload
C++ allow you to specify more than one definitions for a function name or an operator in the same scope,which is called function overloading and operator overloading respectively.
Consider the following example:
example one:
There are three elements in function overload:
in the same scope
the same function name
different arguments
example two
contents about operator overloading will be fullfilled int he future.
Override
C++ allow you to override a virtual method of the base class, which has a key word “virtual” in front.
Consider the following example:
C++ allow you to specify more than one definitions for a function name or an operator in the same scope,which is called function overloading and operator overloading respectively.
Consider the following example:
example one:
#include<iostream> using namespace std; class A{ int x; public: void print(){std::cout<<"print:1"<<endl;} void print(int a){std::cout<<"print int:"<<a<<endl;} void print(double a){std::cout<<"print double:"<<a<<endl;} //conflict with void print(double) //double print(double a){return a;} }; int main(int argc, char *argv[]){ A* a = new A; a->print(); a->print(2); a->print(300.00); /* A a; a.print(); a.print(2); a.print(300.243); */ return 0; }
There are three elements in function overload:
in the same scope
the same function name
different arguments
example two
contents about operator overloading will be fullfilled int he future.
Override
C++ allow you to override a virtual method of the base class, which has a key word “virtual” in front.
Consider the following example:
#include<iostream> using namespace std; class A{ int x; public: virtual void func(){} }; class B:A{ int y; public: void func() {cout<<"overriding"<<endl;} }; int main(int argc, char*argv[]){ B* b = new B(); b->func(); return 0; }
相关文章推荐
- Override and Overload in Java
- overload and overwrite in C++
- Developing a GUI in C++ and DirectX
- Where Are ClassWizard and WizardBar in Visual C++ .NET?[msdn]
- c++ overload override 和 hide
- Create a C++ DLL and Use It in C# (P/Invoke)
- Aggregrate:Initialization of structures and arrays in C++
- Always override Object.equals() and Object.hashCode() in the same time
- Memory as a Programming Concept in C and C++
- Mixing Native and Managed Types in C++
- Cross-Platform Development in C++: Building Mac OS X, Linux, and, Windows Applications (Paperback) Dec.2007.eBook-BBL
- Unicode database support in Tiburon for Delphi and C++
- How to call c++ exported method and classes in c#
- C++ 和 Delphi 的函数覆盖(Override)与重载(overload)
- 【C++专题】重载(overload)、覆盖(override)、隐藏(hide) 辨析
- new, newlc and new in C++
- 关于C#和C++的重载(Overload)、隐藏(Hide)、覆盖(Override)几个区别比较和得出的疑问
- RAII, Dynamic Objects, and Factories in C++
- (轉貼) Evolving a language in and for the real world C++ 1991-2006 (中文翻譯版) (By Bjarne Stroustrup) (C/C++)
- 想翻译Memory Management Algorithms and Implementation in c/c++