Object Oriented Programming Spring 2006 Recitation 3 OOP
Object Oriented Programming Spring 2006 Recitation 3 OOP Spring 2006 – Recitation 3 1
Static Members The Way to Avoid Duplications OOP Spring 2006 – Recitation 3 2
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)? OOP Spring 2006 – Recitation 3 3
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. OOP Spring 2006 – Recitation 3 4
Account Class class 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; }; OOP Spring 2006 – Recitation 3 5
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. OOP Spring 2006 – Recitation 3 6
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). OOP Spring 2006 – Recitation 3 7
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 << a 1. _interest; – Using : : scope operator: cout << Account: : _interest; – (had _interest been declared public and a 1 was an Account object) OOP Spring 2006 – Recitation 3 8
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. OOP Spring 2006 – Recitation 3 9
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. OOP Spring 2006 – Recitation 3 10
Accessing Static Methods • The access is similar to static members: – Through. and -> member access operators: cout << a 1. interest(); – Through : : scope operator: cout << Account: : interest(); OOP Spring 2006 – Recitation 3 11
The Implicit this OOP Spring 2006 – Recitation 4 12
The Implicit this • • • Sometimes we need to address the “whole object” inside a member function (for example, in a copy() method to check against selfassignment). Member functions actually have an implicit parameter (of the class’s type) – the this pointer. this points to the actual object “called”. OOP Spring 2006 – Recitation 4 13
A copy() Method class 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 } OOP Spring 2006 – Recitation 4 14
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 c 1, c 2; // Car: : copy(&c 1, c 2); • • The type of this in previous example is Car *. If the variable is const, its type is const Car *. OOP Spring 2006 – Recitation 4 15
Constants OOP Spring 2006 – Recitation 4 16
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. OOP Spring 2006 – Recitation 4 17
A const Variable • • 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 • • variable can also be used as an array size (instead of C’s #define). Pointers and references can be const too. const OOP Spring 2006 – Recitation 4 18
const Pointers char greeting[] = "Hello"; char *p = greeting; // // const char *p = greeting; // // char * const p = greeting; // // const char* const p = greeting; // // OOP Spring 2006 – Recitation 4 non-const pointer, non-const data non-const pointer, const data const pointer, non-const data const pointer, const data 19
const References int i = 10; const int& j = i; int& k = i; i = 5; j = 0; k = -5; // legal, changes i, j and k // error, j is reference to const // legal, changes i, j and k OOP Spring 2006 – Recitation 4 20
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. OOP Spring 2006 – Recitation 4 21
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 list Car: : Car(int serial, int color) : _serial(serial), _color(color) {} OOP Spring 2006 – Recitation 4 22
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. OOP Spring 2006 – Recitation 4 23
const Methods • Just as any other variable, an object can be const Car PM 1(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 PM 1, thus is should be allowed to execute. We need a way to tell which method will change the object, and which will not. OOP Spring 2006 – Recitation 4 24
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 PM 1. A const method can be applied to non- const objects too (but not the other way). OOP Spring 2006 – Recitation 4 25
The Big Three Compiler-generated Methods OOP Spring 2006 – Recitation 5 26
Who Are They? • • Actually, just like three musketeers that were four – there are four compiler-generated methods. These are – – default constructor, copy constructor, destructor and assignment operator. OOP Spring 2006 – Recitation 5 27
Class is Never Empty • If we write class Empty {}; it is as if we’d written class Empty { public: Empty(); Empty(const Empty&); ~Empty(); Empty& operator=(const Empty& rhs); }; OOP Spring 2006 – Recitation 5 28
When They Come to Life? • They are generated only when needed: void use. Emty(Empty e); void func() { Empty e 1, e 2; // // use. Emty(e 1); // e 2 = e 1; // } // Causes default constructor to be generated Copy constructor Assignment operator Destructor OOP Spring 2006 – Recitation 5 29
Default Constructor and Destructor • • The default constructor and destructor are empty. Default constructor is generated only if no other constructor has been defined. OOP Spring 2006 – Recitation 5 30
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. OOP Spring 2006 – Recitation 5 31
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. OOP Spring 2006 – Recitation 5 32
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. OOP Spring 2006 – Recitation 5 33
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. OOP Spring 2006 – Recitation 5 34
- Slides: 34