Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.

Similar presentations


Presentation on theme: "Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein."— Presentation transcript:

1 Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #3 copyrights © Elhanan Borenstein

2 Agenda The Constructor Function Dynamic Allocation of Data Members The Destructor Function The Copy Constructor Function copyrights © Elhanan Borenstein

3 Constructors & Destructors copyrights © Elhanan Borenstein

4 The Constructor Function As we all know, when variables are defined, they may initially contain garbage values. The same is true for class data members. Garbage values can cause severe problems:  Reminder: CPoint class  Using a variable before it was initialized…  Using POINTERS before allocating memory!!! Although Init() or Set() functions can be used, there is no guarantee that they were called before we use the object. Motivation copyrights © Elhanan Borenstein

5 The Constructor Function The Constructor (C’tor) function can solve the problem described in the previous slide. The Constructor function is called automatically when the object is created. Constructor function can:  Initialized variables…  Allocate memory…  Performs any action we wish to take before we begin working with the object. Introduction copyrights © Elhanan Borenstein

6 The Constructor Function The name of the Constructor function is similar to the name of the class. C’tor functions should (obviously) be defined in the public section. The Constructor function cannot return a value (and its prototype should not define a void return value either).  Why?  What if something went wrong?. Definition copyrights © Elhanan Borenstein

7 The Constructor Function Example: CPoint_a  Note: If the C’tor function does not expect any arguments, the object creation does not include parentheses. Example: CPoint_b  Note 1: If the C’tor function does require arguments, the object cannot be created without specifying them.  Note 2: Since the Constructor is called when the object is created, arguments should be passed only when allocating the object (and not when defining a pointer to it). Example: CPoint_both  Just like any other member function, C’tors can be overloaded and have default parameters  Beware of ambiguity (can we define default values for all parameters?). Examples copyrights © Elhanan Borenstein

8 The Constructor Function When the only available C’tor requires arguments, they must be sent when creating the object. When defining an array of objects we cannot pass arguments (why?). Thus, in this case, we will have to create a C’tor that does not requires and arguments.  Note: such a C’tor can still initialized variables…. What happened before we implemented any C’tor?  The default constructor!!! A few notes about constructors copyrights © Elhanan Borenstein

9 The Initialization Line The object data members can be initialized even before entering the constructor function body using an initialization line. An initialization line guarantee that when we enter the C’tor function body, the variables are already initialized. Example: CPoint_init Notes:  Even if after the initialization line there is nothing left for the Constructor function to do, it should still be defined.  The initialization line is considered part of the function and should thus be written where the function is implemented (rather than in the prototype).  The order of parameters in the initialized line is not mandatory. Definition copyrights © Elhanan Borenstein

10 The Initialization Line An initialization line must be used if:  The object includes an object data member that does not have a default constructor…  Const variables…  Reference variable…. Example: CRectangle_init Note:  The C’tor of the object will be called only after all the data members have been created…  The C’tors of the data members will be activated before the C’tor of the object itself Mandatory initialization line… copyrights © Elhanan Borenstein

11 More about Constructors Sending a constructor’s argument with “=“ Initializing data members with “={…,…,…}” Some special case… Temporary objects can be created by an explicit call to the constructor. The object scope will be only the line it was created in. Temp objects… In C++ the first line which will be activated may be that of a constructor of a global object. Global object’s constructor copyrights © Elhanan Borenstein

12 Dynamic Allocation of Data Members as stated above, C’tors are especially useful when the object includes pointers data member that should be dynamically allocated. The C’tor can either allocate the member object or set the pointer to NULL. Example: CPolygon So…what can still go wrong?  How will we free the allocated memory?  What will happen when we copy the object? copyrights © Elhanan Borenstein

13 The Destructor Function The Destructor function (D’tor) is responsible for any actions we wish to do just before the object is terminated:  Free allocated memory…  other actions we wish to perform… The Destructor will be activated in the following scenarios:  Local object termination (when exiting the block where it was defined)…  Deletion of a dynamically allocated object…  Global object deletion at the end of the application… Motivation copyrights © Elhanan Borenstein

14 The Destructor Function The name of the Destructor function is similar to the name of the class with the prefix “~”. D’tor functions should (obviously) be defined in the public section. Example: CPolygon A destructor cannot return a value and doesn’t get any arguments. WHY? Destructors cannot be overloaded. WHY? Definition copyrights © Elhanan Borenstein

15 The Copy Constructor (CC) Function As we saw in the last lecture, when copying one object to the other, all data members are copied as is. If the object includes data members that are pointers, we will get dual pointing:  Changing the data member in one object may change the data member in the copied object…  We may try to free the same allocated memory twice!!! There are case when we wish to modify additional data members when an object is copied. (EXAMPLE?) We can implement a Copy Constructor which will properly copy an object!!! Motivation copyrights © Elhanan Borenstein

16 The Copy Constructor (CC) Function A copy constructor is simply defined as a constructor that gets the same type of object (ByRef) as an argument. Example: CString Notes:  The Default Copy Constructor.  Using private Copy Constructor… The only issue we still have to solve is object assignment… Definition copyrights © Elhanan Borenstein

17 ByVal Objects As we recall from the previous lecture, we will always prefer to pass argument objects ByRef rather than ByVal… However, if we do pass the argument ByVal, the Copy Constructor will be used. Objects as Arguments We may still want to return the object as the function return value ByVal. Why? Once again, the Copy Constructor will be used. What will happen if we will try to pass the object to the CC by value? Return Values copyrights © Elhanan Borenstein

18 Questions? copyrights © Elhanan Borenstein


Download ppt "Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein."

Similar presentations


Ads by Google