welcome to introduction to object-oriented programming (oop) · vs. returning a temporary a.k.a....

82
QUIZ Explain the meaning of each variable declared: Data d, *dp = &d; int Data::*ptr=&Data::a; int (Data::*fp2) (float); int (Foo::*fptr) (string); int (Foo::*fptr) (string) = &Foo::f;

Upload: others

Post on 10-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

QUIZ

Explain the meaning of each variable declared:

• Data d, *dp = &d;

• int Data::*ptr=&Data::a;

• int (Data::*fp2) (float);

• int (Foo::*fptr) (string);

• int (Foo::*fptr) (string) = &Foo::f;

Page 2: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

QUIZ

Explain from context the meaning of foo, bar, baz and qux in the following code:

• (foo.*bar)(42, 43);

• float f = (baz->*qux)();

• foo.*bar = 42;

• baz->*qux = 43.5;

Page 3: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

QUIZ The following class exists in a program:

class A{

public:

int m;

int n;

};

Declare and initialize a pointer to each of its members.

Page 4: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

QUIZ The following class exists in a program:

class A{

public:

int m;

int n;

};

int A::*pm_m = &A::m;

int A::*pm_n = &A::n;

Page 5: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

QUIZ

Instantiate two objects of class A, x and y. Use the pointers to set the following values for their members: • x: 1, 2 • y: 3, 4

class A{

public:

int m;

int n;

};

int A::*pm_m = &A::m;

int A::*pm_n = &A::n;

Page 6: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

QUIZ class A{

public:

int m;

int n;

};

int A::*pm_m = &A::m;

int A::*pm_n = &A::n;

A x, y; x.*pm_m = 1; x.*pm_n = 2; y.*pm_m = 3; y.*pm_n = 4;

Page 7: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

QUIZ class A{

public:

int m;

int n;

};

Declare a public member function add() that returns the sum of the two data members.

Declare and initialize a pointer to add().

A x;

Page 8: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

QUIZ class A{

public:

int m;

int n;

int add(){return m+n;}

};

Use the pointer to display the sum of the two members of x.

A x; int (A::*fp)(); fp = &A::add;

Page 9: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

QUIZ class A{

public:

int m;

int n;

int add(){return m+n;}

};

A x; int (A::*fp)(); fp = &A::add; cout <<(x.*fp)();

Page 10: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Ch. 12: Operator Overloading

Page 11: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Operator overloading is just “syntactic sugar,” i.e. another way to make a function call:

shift_left(42, 3); 42 << 3; The difference is that the arguments for this function don’t appear inside parentheses, but instead they surround or are next to the operator’s character(s).

example

Page 12: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Let’s first understand what we’re trying to do!

Just a “wrapper” for the int type!

Page 13: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location
Page 14: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Wait a second! Isn’t + a binary operator?

Page 15: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Yes, but, when defined as member function, the LEFT operand is always the object, so only the RIGHT operand needs to be passed.

Page 16: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Why is the return value const?

Page 17: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

In order to prevent “crazy” uses like (x + y) = z;

Page 18: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Write the member function to overload the division / operator.

QUIZ

Page 19: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Must #include <cassert>

Note: The author of the text has his own, customized version of assert, called require:

…..

Page 20: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Now let’s overload the compound assignment operator +=

Page 21: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

. . .

Why non-const and why reference?

Page 22: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

. . .

Non-const to prevent uses like (x += y) += z;

Reference to prevent the creation of a copy.

Page 23: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

. . .

If this is a pointer to the current object, *this is …

Page 24: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location
Page 25: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Overloadable operators Although you can overload almost all the operators available in C, the use of operator overloading is fairly restrictive:

• cannot combine operators that currently have no meaning in C (such as ** to represent exponentiation)

• cannot change the evaluation precedence of operators

• cannot change the number of arguments required by an operator.

Page 26: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Unary operators

. . .

declarations

Page 27: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

. . .

definitions

Page 28: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Examples with member functions

. . .

Page 29: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Thoroughly read and understand the entire subsection Unary operators, including Increment and Decrement.

We stop before the subsection

Binary operators

Page 30: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Individual work for next time:

End-of-chapter exercises 1, 2, 3

EOL 30

Page 31: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Based on their “signatures” (i.e. function headers), explain what the compiler does when it encounters each operator.

QUIZ

What is this operator called?

Page 32: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

When the compiler sees ++a (a pre-increment), it generates a call to operator++(a); but when it sees a++, it generates a call to operator++(a, int). That is, it differentiates between the two forms by making calls to different overloaded functions.

QUIZ

Page 33: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Why does Integer have to “befriend” all these functions?

QUIZ

. . . .

Page 34: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

B/c they’re global functions, and the member is private.

QUIZ

. . . .

Page 35: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Binary operators Assignment can only be implemented as member function (not global!), that’s why we have it only in the Byte class:

?

More in the separate section Overloading assignment

Page 36: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Binary operators

More of the same …

Read this section!

Page 37: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

1.If you only need to read from the argument and not change it, default to passing it as a const reference.

• Ordinary arithmetic operations (like + and –, etc.) and Booleans will not change their arguments, so pass by const reference is predominantly what you’ll use.

• When the function is a class member, this translates to making it a const member function.

Arguments & return values - GUIDELINES -

Page 38: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

1.If you only need to read from the argument and not change it, default to passing it as a const reference.

• Only with the operator-assignments (like +=) and the operator=, which change the left-hand argument, is the left argument not a constant, but it’s still passed in as an address because it will be changed.

Arguments & return values - GUIDELINES -

Page 39: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

2. The type of return value you should select depends on the expected meaning of the operator.

• If the effect of the operator is to produce a new value, you will need to generate a new object as the return value. For example, Integer::operator+ must produce an Integer object that is the sum of the operands. This object is returned by value as a const, so the result cannot be modified as an lvalue.

Arguments & return values - GUIDELINES -

Page 40: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Arguments & return values

3. All the assignment operators modify the lvalue. To allow the result of the assignment to be used in chained expressions, like a=b=c, it’s expected that you will return a reference to that same lvalue that was just modified.

• But should this reference be a const or nonconst? Although you read a=b=c from left to right, the compiler parses it from right to left, so you’re not forced to return a nonconst to support assignment chaining.

- GUIDELINES -

Assignment is right-associative

Page 41: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

What is the output?

QUIZ

Page 42: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

It’s the same as if parenthesized

thus:

QUIZ

Page 43: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

3. All the assignment operators modify the lvalue. To allow the result of the assignment to be used in chained expressions, like a=b=c, it’s expected that you will return a reference to that same lvalue that was just modified.

• However, people do sometimes expect to be able to perform an operation on the thing that was just assigned to, such as (a=b).func( ); to call func( ) on a after assigning b to it. Thus, the return value for all of the assignment operators should be a nonconst reference to the lvalue.

Arguments & return values - GUIDELINES -

Page 44: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

vs.

Returning a temporary a.k.a. the return value optimization

The compiler builds the object directly into the location of the outside return value. Only constructor is called. No copy-constructor is called! No destructor is called (no object in this function’s scope – leave it to the caller’s scope!)

Constructor, copy-constructor, and destructor are called.

Page 45: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Read over lightly:

operator[]

new

delete

operator,

Page 46: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Operator->

Generally used when you want to make an object appear to be a pointer.

Since such an object has more “smarts” built into it than exist for a typical pointer, an object like this is often called a smart pointer.

Page 47: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Operator->

Especially useful if you want to:

• “wrap” a class around a pointer to make it safe

• create an iterator (object that moves through a collection /container of other objects and selects them one at a time, without providing direct access to the implementation of the container)

Page 48: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Operator->

A pointer dereference operator must:

• be a member function

• return an object (or reference to an object) that also has a pointer dereference operator, or

• return a pointer that can be used to select what the pointer dereference operator arrow is pointing at.

example

Page 49: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location
Page 50: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location
Page 51: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

It’s more common to see a “smart pointer” or “iterator” class nested within

the class that it services – see the next example NestedSmartPointer.cpp

Page 52: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

. . . . . Member function of

the vector class

nullptr

Page 53: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location
Page 54: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Abort at the end of container

Let’s understand this! sp is an object of class SmartPointer …

(next slide)

Page 55: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location
Page 56: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Shouldn’t it be sp -> -> f() instead?

Page 57: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Shouldn’t it be sp -> -> f() instead?

A: Yes, technically it should, but the compiler is built to automatically do the work of the second -> operator.

Page 58: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

We stop before the subsection

Operators you can’t overload

EOL 31

Page 59: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Operators you can’t overload

• The member selection operator.

• The pointer to member dereference operator.*

• There’s no exponentiation operator. The most popular choice for this was operator** from Fortran, but this raised difficult parsing questions. Also, C has no exponentiation operator, so C++ didn’t seem to need one either because you can always perform a function call.

• There are no user-defined operators.

• You can’t change the precedence rules.

Page 60: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Non-member operators

If it doesn’t make any difference whether we overload the operators with member or global functions, it is recommended to choose members; this emphasizes the association between the operator and its class.

Page 61: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Non-member operators

However, sometimes you want the left-hand operand to be an object of some other class.

A common application is when the operators << and >> are overloaded for iostreams; we want to be able to write:

MyClass myObject;

cout <<myObject;

cin >>myObject;

Page 62: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location
Page 63: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

These are global functions, not members!

Page 64: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Remember stringstream from Lab 7!

Page 65: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Overloading assignment

In which of these commands is the copy-constructor called?

Hint: (Only) when a new object is created from an existing object!

Page 66: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

The right-hand side does not even need to be a user-defined object:

Assignment syntax

Constructor syntax (recommened!)

Page 67: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Conclusion: Any time you’re initializing an object using an = instead of the ordinary function-call form of the constructor, the compiler will look for a constructor that accepts whatever is on the right-hand side.

Page 68: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location
Page 69: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

One simple rule: copy all of the necessary information from the right-hand object into the current object (that is, the object that operator= is being called for)

Page 70: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Common mistake: you should always check first for self-assignment!

In some cases, such as this one, it’s harmless if you perform the assignment operations anyway, but if changes are made to the implementation of the class, it can make a difference, and if you don’t do it as a matter of habit, you may forget and cause hard-to-find bugs.

Page 71: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Pointers in classes

Problem: Simply copying a pointer means that you’ll end up with two objects pointing to the same storage location.

Solution: You need to do bookkeeping of your own.

example

Page 72: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location
Page 73: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location
Page 74: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Simply copy whatever the pointer refers to. (Also works for copy-

construction.)

Page 75: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Problem with the copy technique

If the object requires a lot of memory or time for initialization, copying is not efficient.

Solution: next slide

Page 76: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Reference counting

You give intelligence to the object that’s being pointed to so it knows how many objects are pointing to it.

Both copy-construction and assignment mean: attaching another pointer to an existing object and incrementing the reference count.

Destruction means: decrementing the reference count. If the reference count goes to zero, destroy the object!

Page 77: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

The program

C12:ReferenceCounting.cpp

is not required for the exam.

Read FYI.

Page 78: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Automatic operator= creation

Because assigning an object to another object of the same type is an activity most people expect to be possible, the compiler will automatically create a type::operator=(type) if you don’t make one.

The behavior of this operator mimics that of the automatically created copy-constructor; if the class contains objects (or is inherited from another class), the operator= for those objects is called recursively, a.k.a. memberwise assignment.

Page 79: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Remember from ch.11: “Deleted” functions were introduced in

C++11

Page 80: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location
Page 81: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

We stop before the section

Automatic type conversion

SKIP the remainder of ch.12

Page 82: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location

Homework for ch. 12

Provided as separate handout (also available on our webpage --> agapie.net)

Due Friday, Nov. 20, at the beginning of class.

Please hand in a hard-copy, do not email!

EOL 32