Top Banner
C++ Training C++ Training Datascope Datascope Lawrence D’Antonio Lawrence D’Antonio Lecture 5 Lecture 5 An Overview of C++: An Overview of C++: What is Polymorphism? - What is Polymorphism? - Overloading Overloading
20

C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

C++ TrainingC++ TrainingDatascopeDatascope

Lawrence D’AntonioLawrence D’AntonioLecture 5Lecture 5

An Overview of C++:An Overview of C++:

What is Polymorphism? - OverloadingWhat is Polymorphism? - Overloading

Page 2: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

What is polymorphism?What is polymorphism?

Different types of objects respond to the Different types of objects respond to the same message and use the appropriate same message and use the appropriate method.method.

Polymorphism

Universal

Ad-hoc

Parametric

Subtype

Overloading

Coercion

Page 3: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

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.

Page 4: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

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.

Page 5: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

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; }

Page 6: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

Alternative Method

template<typename T>

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)

Page 7: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

Implementation

How is overloading done? Through name mangling. The compiler modifies

the names of each overloaded function. Examplevoid foo(int,int);void foo(double,double);

In Assembler, these would be renamed:foo_Fii:

foo_Fdd:

Page 8: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

Is this code legal?#include <stdlib.h>

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;

}

Page 9: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

Yes, this is legal.

The nested enums C1::E and C2::E are different types. So the overloaded functions have different signatures.

Page 10: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

Is this legal?

class X {

public:

int f();

double f();

};

No, you can’t overload only on return type.

Page 11: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

Is this legal?

struct A {

static int f();

int f();

};

No, it’s not legal. You can’t overload by static.

Page 12: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

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.

Page 13: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

Is this legal?

f(char*);

f(char[10]);

Not legal. The arguments are considered the same type (pointer to char).

Page 14: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

Is this legal?

g(char(*)[20]);

g(char(*)[40]);

Yes, it’s legal. You can distinguish multidimensional arrays by their second (or higher) dimensions.

Page 15: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

Is this legal?

int f(int);

int f(const int);

Not legal. You can’t overload by constness of argument.

Page 16: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

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 &)

Page 17: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

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)

Page 18: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

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.

Page 19: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

Is this legal?

void f(int);

void f(int i = 10);

Not legal. Can’t overload by default arguments.

Page 20: C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.

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).