Download presentation
Presentation is loading. Please wait.
1
C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading
2
What is polymorphism? Different types of objects respond to the same message and use the appropriate method. Polymorphism Universal Ad-hoc Parametric Subtype Overloading Coercion
3
Polymorphic Objects A function (or operator) is polymorphic if it has an argument that can accept different types. A variable is polymorphic if it can have different types in different contexts. A type is polymorphic if its operations can apply to arguments of different types.
4
Overloading The same name is used to denote different functions. These functions are distinguished by different signatures. Some languages (such as C++) allow the programmer to define their own overloaded functions and operators.
5
Overloading Example int square(int x) { return x*x; } long square(long x) { return x*x; } float square(float x) { return x*x; } double square(double x) { return x*x; }
6
Alternative Method template T square(T x) { return x*x; } This works on all data types for which operator * is defined. int x = square(4); //Calls square(int) double y = square(4.2); //Calls square(double) float z = square(3); //Calls square(int)
7
Implementation How is overloading done? Through name mangling. The compiler modifies the names of each overloaded function. Example void foo(int,int); void foo(double,double); In Assembler, these would be renamed: foo_Fii: foo_Fdd:
8
Is this code legal? #include struct C1 {enum E {red, blue};}; struct C2 {enum E {red, blue};}; extern "C" int printf(const char *,...); void f(C1::E x) {printf("f(C1::E)\n");} void f(C2::E x) {printf("f(C2::E)\n");} int main() { f(C1::red); f(C2::red); return EXIT_SUCCESS; }
9
Yes, this is legal. The nested enums C1::E and C2::E are different types. So the overloaded functions have different signatures.
10
Is this legal? class X { public: int f(); double f(); }; No, you can’t overload only on return type.
11
Is this legal? struct A { static int f(); int f(); }; No, it’s not legal. You can’t overload by static.
12
Is this legal? typedef int I; void f(float, int); void f(float, I); Not legal. A typedef of an int is still an int.
13
Is this legal? f(char*); f(char[10]); Not legal. The arguments are considered the same type (pointer to char).
14
Is this legal? g(char(*)[20]); g(char(*)[40]); Yes, it’s legal. You can distinguish multidimensional arrays by their second (or higher) dimensions.
15
Is this legal? int f(int); int f(const int); Not legal. You can’t overload by constness of argument.
16
Is this legal? void f(int &) { std::cout << “int &\n”; } void f(const int &) { std::cout << “const int &\n”; } main() { f(3); return 0; } Legal. const is used within a type specification. Q: Which function is called? A: f(const int &)
17
Is this legal? void f(int) { std::cout << “int \n”; } void f(int &) { std::cout << “int &\n”; } main() { f(3); return 0; } Legal. The signatures are different. Q: Which function is called? A: f(int)
18
Is this legal? void f(int) { std::cout << “int \n”; } void f(const int &) { std::cout << “const int &\n”; } main() { int x = 2; f(x); f(3); return 0; } Not legal. The calls f(x) and f(3) are ambiguous.
19
Is this legal? void f(int); void f(int i = 10); Not legal. Can’t overload by default arguments.
20
Is this legal? void g(int (float)); void g(int (*)(float)); Not legal. Both functions take the same argument (pointer to function of the same type).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.