oop class lawrence d’antonio lecture 4 an overview of c++, part 2

90
OOP Class OOP Class Lawrence D’Antonio Lawrence D’Antonio Lecture 4 Lecture 4 An Overview of C++, Part An Overview of C++, Part 2 2

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

OOP ClassOOP Class

Lawrence D’AntonioLawrence D’Antonio

Lecture 4Lecture 4

An Overview of C++, Part 2An Overview of C++, Part 2

Page 2: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is a class?What is a class?

A class is a set of objects sharing common A class is a set of objects sharing common features.features.

A class defines an object’s attributes and A class defines an object’s attributes and behavior. Methods are provided to act on behavior. Methods are provided to act on an object and to pass messages between an object and to pass messages between objects.objects.

A class is the basic unit of abstraction.A class is the basic unit of abstraction. A class is the basic unit of modularity.A class is the basic unit of modularity. A class can be concrete or abstract.A class can be concrete or abstract.

Page 3: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Class Design as Type Design

Scott Meyers, Item #19 How should objects of your new type be

created and destroyed? How should object initialization differ from

object assignment? What does it mean for objects of your new

type to be passed by value?

Page 4: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Class Design as Type Design 2

What are the restrictions on legal values for your new type?

Does your new type fit into an inheritance graph?

What kind of type conversions are allowed for your new type?

What operators and functions make sense for the new type?

Page 5: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Class Design as Type Design 3

What standard functions should be disallowed?

Who should have access to members of your new type?

What is the “undeclared interface” of your new type?

How general is your new type? Is a new type really what you need?

Page 6: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is an object?What is an object?

An object is an instance of a class.An object is an instance of a class. An object has state, behavior, identity.An object has state, behavior, identity.

Page 7: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is an object?

Coad-Yourdon

An abstraction of something in a problem domain, reflecting the capabilities of the system to keep information about it, interact with it, or both; an encapsulation of attribute values and their exclusive services.

Page 8: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is an object?

OMG

An object is a thing. It is created as the instance of an object type. Each object has a unique identity that is distinct from and independent of any of its characteristics. Each object offers one or more operations.

Page 9: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is an object?

Firesmith

An object is defined as a software abstraction that models all relevant aspects of a single tangible or conceptual entity or thing from the application domain or solution space. An object is one of the primary entities in an object-oriented application, typically corresponds to a software module, and consists of a set of related attribute types, messages, exceptions, operations, and optional component objects.

Page 10: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is an object?

Booch

From the perspective of human cognition, an object is any of the following:

A tangible and/or visible thing. Something that may be apprehended

intellectually. Something toward which thought or action is

directed.

Page 11: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is an object?

Booch continued.

An object has state, behavior, and identity; the structure and behavior of similar objects are defined in their common class; the terms instance and object are interchangeable.

Page 12: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is an object?

Shlaer-Mellor

An object is an abstraction of a set of real-world things such that:

All the things in the set have the same characteristic.

All instances are subject to and conform to the same set of rules and policies.

Page 13: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is an object?

Jacobson

An object is characterized by a number of operations and a state which remembers the effect of these operations.

Page 14: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is encapsulation?What is encapsulation?

Internal details of objects are concealed Internal details of objects are concealed from the objects users (information hiding).from the objects users (information hiding).

Both data and implementation may be Both data and implementation may be hidden. The object is a black box.hidden. The object is a black box.

Access to members is controlled through Access to members is controlled through the class definition.the class definition.

The accessible part of a class is called its The accessible part of a class is called its interface.interface.

Page 15: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Data encapsulation exampleclass Clock { private:

int hours; // 1-12 private int minutes; // 0-59

public:Clock(int h, int m) {

if (h < 1 || h > 12) { throw(”Hours must be between 1 and

12″); } if (m < 0 || m > 59) {

throw(”Minutes must be between 0 and 59″);

} h = hours; m = minutes;

}//...

};

Page 16: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Class Invariants

The above is an example of “Programming by Contract.”

The class guarantees that

These are called class invariants

1 hours 12, 0 minutes 59

Page 17: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Data Members

Data members can be declared as: const – a declaration that an object is read

only. The object may be stored in a CPU register.

volatile – a declaration that an object’s value may be changed asynchronously. The object may not be stored in a CPU register.

Page 18: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Data Members 2

static – A data member shared by all objects of a class. There is only one copy of a static member, it is not part of the object memory layout.

mutable – A data member that is allowed to be modified, even if it a member of a const object.

Page 19: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is the following code legal?

struct X {

static int a = 2;

};

main()

{

X my_x;

X::a = 4;

my_x::a = 5;

}

Page 20: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Not legal!

Cannot initialize static data member a within class.

static variables are similar to extern variables.

Page 21: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is the following code legal?

struct X {

static int a;

};

main()

{

X my_x;

X::a = 4;

my_x::a = 5;

}

Page 22: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Not legal!

This is a linker error.

X::a was used but never defined.

Page 23: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is the following code legal?

struct X {

static int a;

};

int X::a;

main()

{

X my_x;

X::a = 4;

my_x::a = 5;

}

Page 24: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Legal.

X::a was defined before being used.

Note: it is okay that X::a was not initialized.

Page 25: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is the following code legal?

struct X {static int a;static const int b = 3;

};

int X::a = 2;

main(){

X my_x;X::a = 4;my_x::a = X::b;

}

Page 26: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Legal.

static const members can be declared and defined at the same time.

Page 27: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is the following code legal?

void func(volatile std::list<int> li)

{

int n = li.front();

}

Not legal!

Only volatile member functions can be called on a volatile object.

list::front() not a volatile function

Page 28: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is a method?What is a method?

A method is a member function that acts A method is a member function that acts upon an object. A method is generally upon an object. A method is generally called for one object (exception: static called for one object (exception: static members).members).

Commonly found methods are Commonly found methods are constructors, destructors, assignment, constructors, destructors, assignment, mutators, accessors.mutators, accessors.

Page 29: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Static Members#include <iostream> using namespace std;

class X { public:

int a; void f(int b) { cout << “X::f()\n”;}

};

int main() { int X::*ptiptr = &X::a; //pointer to data member

void (X::* ptfptr) (int) = &X::f; //pointer to member function

X xobject; xobject.*ptiptr = 10; (xobject.*ptfptr) (20); }

Page 30: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is message passing?What is message passing?

Messages are transfers of data or Messages are transfers of data or requests for another object to take an requests for another object to take an action.action.

Page 31: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Message passing example

Adapter pattern

Page 32: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

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 33: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

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 34: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

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 35: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

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 36: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

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 37: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

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 38: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

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 39: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Yes, this is legal.

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

Page 40: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?

class X {

public:

int f();

double f();

};

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

Page 41: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?

struct A {

static int f();

int f();

};

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

Page 42: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

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 43: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?

f(char*);

f(char[10]);

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

Page 44: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

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 45: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?

int f(int);

int f(const int);

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

Page 46: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

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 47: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

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 48: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?

void f(double int) { std::cout << “double \n”; }void f(const int &) { std::cout << “const int &\n”; }

main() {f(3);return 0;

}

Legal. The signatures are different.

Q: Which function is called?A: f(const int &)

Page 49: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?

void f(int);

void f(int i = 10);

Not legal. Can’t overload by default arguments.

Page 50: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

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

Page 51: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Coercion

A coercion is an implicit type conversion. This allows the programmer to omit a type cast.

There are three types of coercions: Compiler defined (such as promotions,

derived base) Constructor User defined

Page 52: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Compiler defined coercions

Simple example

double x;

x = 2; //2 promoted to double

Page 53: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Function call coercions

void foo(double x) { //... }

//foo() can be called with any type that can be

//converted to double

foo((short) 4);

foo(‘a’);

foo(3L);

foo(2.3F);

Page 54: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?#include <iostream>

void f(char a, int b){ std::cout << a << b << '\n'; }

void f(int a, char b){ std::cout << a << b << '\n'; }

main(){ f('a','b'); return 0;}

Page 55: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Not legal, it’s ambiguous.

Page 56: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Coercion vs. Overloading example

3 + 4;

3 + 4.0;

3.0 + 4;

3.0 + 4.0;

How does this work?

Page 57: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Explanations of example

Four overloaded versions of operator + Two overloaded versions of operator +,

one for integers, the other for doubles. The middle two calls in the example would use coercion.

One version of operator +, for doubles. The first three calls in the example would use coercion.

Page 58: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Derived to Base Conversion

C++ will implicitly convert a pointer or reference to a derived class to a pointer or reference to the base class. This is because the derived class has a copy of the base class inside it.

A pointer to the base class cannot be converted to a pointer to the derived class.

Page 59: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?class A {};

class B: public A {};

class C: protected A {};

class D: private A {};

main() { A *pa = new B; pa = new C; pa = new D; return 0;}

Page 60: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

pa = new B; //Legal, B is an A

pa = new C; //Illegal, C is not an A

pa = new D; //Illegal, D is not an A

In the last two cases, the base class is inaccessible.

Page 61: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?

class A {};

class C: protected A {public: void foo() { A *pa = this; }}; class D: private A {public: void foo() { A *pa = this; }}; main() { C c; c.foo();

D d; d.foo(); return 0;}

Page 62: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Yes, it’s legal. Protected and private inheritance exemplify a has-a relationship, rather than an is-a relationship. Member functions are allowed to convert pointer to derived into pointer to base.

Page 63: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Pointer to Member Conversions

What is the relationship between a pointer to member of a base class and a derived class?

Page 64: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?struct A { int a;};

struct B: public A { int a;};

main(){ A a; B b; int A::*pa = &B::a; int B::*pb = &A::a;

return 0;}

Page 65: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Complicated.

int A::*pa = &B::a;

is illegal. Can’t convert pointer to derived member to a pointer to base member.

int B::*pb = &A::a;

Is legal. Can convert pointer to base member to a pointer to derived member.

Page 66: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Conversion Constructors

Constructors that take a single argument can be thought of as a type conversion.

struct X { X(int); };

So that any code that expects an object of class X can be passed an int that will then be converted to an X.

Page 67: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?

struct X { X(int){} };

void f(const X &) {}void g(int) {}void g(X) {}

main() {f(3);g(4);g(X(5));

return 0;}

Page 68: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

f(3); //Legal, you can create a temp //object X(3), which is passed to //f()

g(4); //Legal, calls g(int)

g(X(5)); //Legal, calls g(X)

Page 69: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?struct X { X(int){} };

void f(X &) {}void g(int) {}void g(X) {}

main() {int a = 2;

f(3);f(a);g(4);g(X(5));

return 0;}

Page 70: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

f(3); //Not legal, can’t use temp X object to initialize an X &

f(a); //Not legal, can’t use temp X object to initialize an X &

g(4); //Legal, as before

g(X(5)); //Legal, as before

Page 71: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?

struct X { X(int){} };

struct Y { Y(X) {} };

void f(Y) {}

main() {

f(3);

return 0;

}

Page 72: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Not legal.

Basically, only one level conversions are allowed. Not allowed to convert int to X to Y.

Page 73: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?struct X { X(int){} };

void f(X) {}void g(int) {}void g(X) {}

main() {int a = 2;

f(3);f(a);g(4);g(X(5));

return 0;}

Page 74: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

f(3); //Legal, temp X is passed by value

f(a); //Legal, temp X is passed by value

g(4); //Legal, as before

g(X(5)); //Legal, as before

Page 75: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Parametric Polymorphism

Parametric polymorphism parametrizes Parametric polymorphism parametrizes the object type (e.g., a list class, where the the object type (e.g., a list class, where the type of object stored is parametrized).type of object stored is parametrized).

Page 76: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Template Functions

template<class T>

T max(T a, T b) {

return a > b ? a : b;

}

Page 77: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Is this legal?

int a,b = 6;

const int c = 3;

double x,y = 3.2;

a = max(b,4);

a = max(b,c);

x = max(y,3.3);

x = max(b,y);

Page 78: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

a = max(b,4); //Legal, max<int,int>a = max(b,c); //Legal, max<int,int>x = max(y,3.3); //Legal, max<double,double>x = max(b,y); //Illegal, no max<int,double>

A template function is called only when there is an exact match for type parameters (only trivial conversions, such as const int to int are allowed).

Page 79: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Better max?template<class S, class T>T max(S a, T b) { return a > b ? a : b;}

main() { int a, b = 3; double x, y = 3.2;

a = max(b,5); x = max(y,5.4); x = max(b,y); x = max(y,b); return 0;}

Page 80: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

a = max(b,5); //Legal, returns 5

x = max(y,5.4); //Legal, returns 5.4

x = max(b,y); //Legal, 3.2

x = max(y,b); //Legal, but //returns 3.0!

Page 81: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Best max?template<class R, class S, class T>R max(S a, T b) { return a > b ? a : b;}

main() { int a, b = 3; double x, y = 3.2;

a = max(b,5); x = max(y,5.4); x = max(b,y); x = max(y,b); return 0;}

Page 82: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Doesn’t compile. The function max() is supposed to have 3 template parameters. But each call only uses 2 parameters.

Page 83: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Try this maxtemplate<class R, class S, class T>R max(S a, T b) { return a > b ? a : b;}

main() { int a, b = 3; double x, y = 3.2;

a = max<int>(b,5); x = max<double>(y,5.4); x = max<double>(b,y); x = max<double>(y,b);

return 0;}

Page 84: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Subtype polymorphism

Subtype (or inclusion) polymorphism Subtype (or inclusion) polymorphism allows objects of a given type to be allows objects of a given type to be substituted for by objects of a subtype.substituted for by objects of a subtype.

Page 85: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is inheritance?What is inheritance?

One class (derived/child) relies on the definition of another class (base/parent).

Single vs. multiple inheritance A method of sharing code or sharing

interface among classes. Language may define a class tree (with

single root): Java, Smalltalk Language may define a class forest: C++

Page 86: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is typing?What is typing?

Static typing: Data type determined at compile-time. Type must be declared.

Dynamic typing: Data type may be determined at run-time. Type need not be declared.

Strong typing: Variables are bound to a specific type.

Weak typing: A variable’s type may change.

Page 87: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Varieties of typing

Static and strong typing: Java, Pascal, OCaml, Haskell

Static and weak typing: C/C++ Dynamic and strong typing: Python Dynamic and weak typing: PHP

Page 88: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Dynamic typing example# Python example

class Cat: def speak(self): print "meow!"

class Dog: def speak(self): print "woof!"

class Bob: def speak(self): print "hello world!"

def command(pet): pet.speak()

pets = [ Cat(), Dog(), Bob() ]

for pet in pets: command(pet)

Page 89: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

Weak typing example

var x := 5;

var y := "37";

Print(x + y);

In Visual Basic this prints: 42

In JavaScript this prints: 537

Page 90: OOP Class Lawrence D’Antonio Lecture 4 An Overview of C++, Part 2

What is exception handling?

The mechanism used to report and recover from abnormal states.

When an error is detected, execution is passed to a handler.