c++ training datascope lawrence d’antonio lecture 5 an overview of c++: what is polymorphism? -...

20
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

Post on 22-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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.

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.

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

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)

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:

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;

}

Yes, this is legal.

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

Is this legal?

class X {

public:

int f();

double f();

};

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

Is this legal?

struct A {

static int f();

int f();

};

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

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.

Is this legal?

f(char*);

f(char[10]);

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

Is this legal?

g(char(*)[20]);

g(char(*)[40]);

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

Is this legal?

int f(int);

int f(const int);

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

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

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)

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.

Is this legal?

void f(int);

void f(int i = 10);

Not legal. Can’t overload by default arguments.

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