OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

34
OOP Spring 2006 – Recitation 3 1 Object Oriented Programming Spring 2006 Recitation 3
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    218
  • download

    0

Transcript of OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

Page 1: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 3 1

Object Oriented Programming

Spring 2006Recitation 3

Page 2: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 3 2

Static Members

The Way to Avoid Duplications

Page 3: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 3 3

Motivation• We’ve been hired to write a bank account

managing application. Each account has its owner and balance.

• All accounts bear the same interest rate.• How do we “say” in OO language that all

objects of class Account bear the same interest (Remembering that global variables are a disadvantage)?

Page 4: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 3 4

Solution• We can declare the _interest member of

Account as static.• This means that all objects of class

Account share one _interest variable, while each object has its own copy of non-static variables.

• Similar to static function variable, where all function invocations share the same variable.

Page 5: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 3 5

Account Classclass Account {public: Account(string owner, double init_balance); string owner() { return _owner; } double balance() { return _balance; } double interest() { return _interest; }private: string _owner; double _balance; static double _interest;};

Page 6: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 3 6

Definition• The _interest member above is declared,

but not defined.• Just as we need to define a function, we

need to define a static member of a class.• It must be defined only once in the

program, thus the definition should not be in the header.

double Account::_interest;

outside of the class is the way to define.

Page 7: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 3 7

Initialization• We can specify an initial value for a static

variable:double Account::_interest = 1.12;

• If the value is not specified, the value is zero (more precisely, the memory belonging to the variable is zeroed out).

Page 8: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 3 8

Accessing Static Members

• Member functions access static member just as any other member:

double Account::interest() { return _interest; }

• Two ways to access from outside:– Using . and -> member access operators:

cout << a1._interest;

– Using :: scope operator:cout << Account::_interest;

– (had _interest been declared public and a1 was an Account object)

Page 9: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 3 9

Static Methods• If a method doesn’t use non-static

members, why should each object carry a copy of it? The behavior is the same across all objects of that class.

• Such method can be declared static too.• interest() above is a good candidate.

Page 10: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 3 10

Declaration and Definition

• To make a method static, add keyword static before its declaration in the class.

• Definition is the same as for non-static methods (do not add static in definition).

• A static method cannot access non-static members and methods.

Page 11: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 3 11

Accessing Static Methods

• The access is similar to static members:– Through . and -> member access operators:

cout << a1.interest();

– Through :: scope operator:cout << Account::interest();

Page 12: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 12

The Implicit this

Page 13: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 13

The Implicit this• Sometimes we need to address the

“whole object” inside a member function (for example, in a copy() method to check against self-assignment).

• Member functions actually have an implicit parameter (of the class’s type) – the this pointer.

• this points to the actual object “called”.

Page 14: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 14

A copy() Methodclass Car {public:

// other methods…void copy(const Car& rhs);

private:// other data members…int _color;

};

void Car::copy(const Car& rhs) {if (this == &rhs) {

return;}_color = rhs._color; // _color <=> this->_color

}

Page 15: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 15

this Come From…• The compiler “rewrites” method fingerprints

(and calls) to accept another parameter:– A method fingerprint will “look” like:

// void Car::copy(Car *this, const Car& rhs);

– A method call will “look” like:// Car c1, c2;// Car::copy(&c1, c2);

• The type of this in previous example is Car *.• If the variable is const, its type is const Car *.

Page 16: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 16

Constants

Page 17: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 17

Constant Values• Sometimes we need to use values that

cannot be changed, i.e. constant values:– Arrays sizes (int buffer[MAX_LEN])– Numeric constants (double pi = 3.141592)

• C++ provides the const qualifier to declare that a variable’s value cannot be changed.

• The compiler then checks to see that it is really never changed.

Page 18: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 18

A const Variable• const variables must always be initialized.• A const variable is used the same way a

normal variable is, but it cannot be assigned to:

const int i = 5;i = 10; // error, i is const

• const variable can also be used as an array size (instead of C’s #define).

• Pointers and references can be const too.

Page 19: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 19

const Pointerschar greeting[] = "Hello";

char *p = greeting; // non-const pointer,

// non-const data

const char *p = greeting; // non-const pointer,

// const datachar * const p = greeting; // const

pointer,// non-const

dataconst char* const p = greeting; // const

pointer,// const data

Page 20: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 20

const Referencesint i = 10;

const int& j = i;int& k = i;

i = 5; // legal, changes i, j and k

j = 0; // error, j is reference to const

k = -5; // legal, changes i, j and k

Page 21: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 21

const Members• Since any variable can be const, class’s

members can too be const.• For example, each Car can have a Serial

Number, but it should not be changed once the Car is made, hence it’s const.

• It must be initialized in all constructors.

Page 22: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 22

A Car with Serial Number

class Car {public: Car(int serial, int color); int color() { return _color; }private: const int _serial; int _color;};

// Initialization using initialization listCar::Car(int serial, int color) : _serial(serial),

_color(color) {}

Page 23: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 23

Initialization List• The part after : in Car’s constructor is

called initialization list. We can give initial values to data members there instead of the constructor’s body.

• const and reference data members can only be initialized there.

• NOTE: List members in the initialization list in the order in which they are declared.

• static const members are initialized as regular static members.

Page 24: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 24

const Methods• Just as any other variable, an object can

be const.const Car PM1(001); // Prime-minister's car

• The method Car::copy() declared earlier would change this object, thus it should not be allowed to execute.

• The method Car::color() would not change PM1, thus is should be allowed to execute.

• We need a way to tell which method will change the object, and which will not.

Page 25: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 4 25

const Method Declaration

• A const method is one which will not change its object, thus it can be applied to const objects.

• To make a method const, add const after its name (both in declaration and definition):

int Car::color() const;

• Now Car::color() can be called for PM1.• A const method can be applied to non-

const objects too (but not the other way).

Page 26: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 5 26

The Big Three

Compiler-generated Methods

Page 27: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 5 27

Who Are They?• Actually, just like the three musketeers

that were four – there are four compiler-generated methods.

• These are– default constructor,– copy constructor,– destructor and– assignment operator.

Page 28: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 5 28

Class is Never Empty• If we write

class Empty {};

it is as if we’d writtenclass Empty {public:

Empty();Empty(const Empty&);~Empty();Empty& operator=(const Empty& rhs);

};

Page 29: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 5 29

When They Come to Life?

• They are generated only when needed:void useEmty(Empty e);

void func() {Empty e1, e2; // Causes default constructor

// to be generateduseEmty(e1); // Copy constructore2 = e1; // Assignment operator

} // Destructor

Page 30: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 5 30

Default Constructor and

Destructor• The default constructor and destructor

are empty.• Default constructor is generated only if no

other constructor has been defined.

Page 31: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 5 31

Copy Constructor• The default copy constructor initializes

each data member with corresponding data member in the given object using corresponding copy constructor.

• The copy constructor for built-in types (int, double, void*) is bitwise copy.

Page 32: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 5 32

Assignment Operator• The default assignment operator

initializes each data member with corresponding data member in the given object using corresponding assignment operator.

• The assignment operator for built-in types (int, double, void*) is bitwise copy.

Page 33: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 5 33

Works, But Not Always• The default behavior is not always good.

– If the class has a pointer member, the memory where points should be copied in copy constructor and assignment operator, and not the pointer itself.

– The memory needs to be deleted in destructor.

• When the default behavior is not enough, we need to write our own version.

• We can’t use the default version in our version.

Page 34: OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.

OOP Spring 2006 – Recitation 5 34

The Rule of Big Three• The rule states that

If a class needs any of the Big Three (copy constructor, assignment operator, destructor), it needs them all.

• If we see that we need to do some non-trivial task in any of the Big Three, chances are that we will need to implement all of them.