Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3."— Presentation transcript:

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

2 OOP Spring 2006 – Recitation 32 Static Members The Way to Avoid Duplications

3 OOP Spring 2006 – Recitation 33 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)?

4 OOP Spring 2006 – Recitation 34 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.

5 OOP Spring 2006 – Recitation 35 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; };

6 OOP Spring 2006 – Recitation 36 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.

7 OOP Spring 2006 – Recitation 37 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).

8 OOP Spring 2006 – Recitation 38 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)

9 OOP Spring 2006 – Recitation 39 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.

10 OOP Spring 2006 – Recitation 310 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.

11 OOP Spring 2006 – Recitation 311 Accessing Static Methods The access is similar to static members: –Through. and -> member access operators: cout << a1.interest(); –Through :: scope operator: cout << Account::interest();

12 OOP Spring 2006 – Recitation 412 The Implicit this

13 OOP Spring 2006 – Recitation 413 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”.

14 OOP Spring 2006 – Recitation 414 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 }

15 OOP Spring 2006 – Recitation 415 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 *.

16 OOP Spring 2006 – Recitation 416 Constants

17 OOP Spring 2006 – Recitation 417 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.

18 OOP Spring 2006 – Recitation 418 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.

19 OOP Spring 2006 – Recitation 419 const Pointers char greeting[] = "Hello"; char *p = greeting;// non-const pointer, // non-const data const char *p = greeting;// non-const pointer, // const data char * const p = greeting;// const pointer, // non-const data const char* const p= greeting;// const pointer, // const data

20 OOP Spring 2006 – Recitation 420 const References int i= 10; constint& 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

21 OOP Spring 2006 – Recitation 421 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.

22 OOP Spring 2006 – Recitation 422 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) {}

23 OOP Spring 2006 – Recitation 423 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.

24 OOP Spring 2006 – Recitation 424 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.

25 OOP Spring 2006 – Recitation 425 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).

26 OOP Spring 2006 – Recitation 526 The Big Three Compiler-generated Methods

27 OOP Spring 2006 – Recitation 527 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.

28 OOP Spring 2006 – Recitation 528 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); };

29 OOP Spring 2006 – Recitation 529 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 generated useEmty(e1);// Copy constructor e2 = e1;// Assignment operator }// Destructor

30 OOP Spring 2006 – Recitation 530 Default Constructor and Destructor The default constructor and destructor are empty. Default constructor is generated only if no other constructor has been defined.

31 OOP Spring 2006 – Recitation 531 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.

32 OOP Spring 2006 – Recitation 532 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.

33 OOP Spring 2006 – Recitation 533 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.

34 OOP Spring 2006 – Recitation 534 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.


Download ppt "OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3."

Similar presentations


Ads by Google