Presentation is loading. Please wait.

Presentation is loading. Please wait.

SPL – PS3 C++ Classes.

Similar presentations


Presentation on theme: "SPL – PS3 C++ Classes."— Presentation transcript:

1 SPL – PS3 C++ Classes

2 Overview Object oriented programming in C++ Classes with resources
Basic syntax Differences from Java

3 Parameter passing Parameters can be passed to a function in different ways. Correct parameter passing can reduce number of bugs and create a more efficient code. Parameters can be passed to a function by value, by reference or by pointer*. The same principles that apply to parameter passing also apply to function return values.

4 Parameter passing – Passing by value
The value is being copied to the argument list of the called function. Usually (But not always) done for primitive values. When an object is passed by value, it’s copy constructor will be implicitly invoked. (Could be slow for large objects)

5 Parameter passing – Passing by pointer
A pointer to the object is being copied to the function argument list. Can be used when we want the function to change the object. Can also pass null pointers. Actually passing by value.

6 Parameter passing – Passing by reference
Allows us to send a link to the original object without using pointers. Usually safer than passing by pointer. Must be initialized at start, so cannot pass a NULL value.

7 C++ classes – header example

8 C++ classes – implementation example

9 Syntax differences between C++ and Java
Separation of declaration and implementation - Done in cases when you want to share the class with another class. Semicolon at the end of the class declaration. Private and public sections, unlike Java where each method/variable visibility is declared separately. Const methods

10 Const methods const functions do not change the state of the object.
Can only return const references and const pointers of object’s members. Can only access const methods. Once declared const, you can only use const methods on this variable.

11 Member initialization list
Used to initialize class members. Executed before the body of the function. Not initializing in member initialization list – means implicit call to default constructor. Const members can only be initialized in member initialization list. It is a convention to keep the order of the list as the order of the declaration.

12 Reminder – Header files
Contain declarations of variables, functions, and classes. Usually no implementation in header files. (Not allowed in this course) Use preprocessor variable definition to avoid including same header twice.

13 C++ objects In C++ objects hold values, not references.
If no construction parameters supplied, default constructor implicitly invoked. When one object is assigned to another, an actual copy of the object is made.

14 C++ objects (cont) When modifying an object in a function, pass object by reference. Two objects cannot jointly access same object, use references for that effect. Object can only hold value of a particular type, use pointers if you want to hold objects from different sub-classes. If you want variable to be either null or an actual object, use pointers.

15 The this pointer The this pointer holds the address of the object instance. Not accessible from static member functions. The this pointer is not modifiable.

16 Operator . and operator ->

17 The rule of 3 A class that hold resources must define and implement the following functions. A destructor – tasked with freeing the resources held by the class. A copy constructor – tasked with creating a copy of the class. Copy assignment operator – copies other object data to override it’s own.

18

19

20

21

22

23

24

25 Destructor Called when the object is about to “go away”.
When the object goes out of scope. When dynamically allocated object is freed. Frees dynamically allocated storage used by this object. Up to the programmer to make sure no other pointers exist for freed memory.

26 Copy constructor Called implicitly when an object initialized to be a copy of an existing object. When passed by value. When an object returned by value. When an object is initialized from an existing object of the same class.

27 Copy assignment operator
If no copy assignment operator is overloaded, default copy assignment operator shallow copies class’s fields. Which can cause trouble if the class has pointer members. Returns a reference to the object. Copy assignment operator Copy constructor Object already initialized, must free memory to avoid memory leaks Initializes and creates the object. Self assignment is possible, and need to be checked for by the programmer Self assignment not possible Must return a reference to the object after assignment. Supports chained assignment. A constructor and hence, doesn’t return a value.

28 Exception safety in copy assignment operator
If copying the other object threw an exception, that can lead to trouble.


Download ppt "SPL – PS3 C++ Classes."

Similar presentations


Ads by Google