Understanding Object Programming Essentials in C++

slide1 n.w
1 / 70
Embed
Share

Explore object programming essentials in C++, including creating objects, inheritance, data structures, dynamic allocation, memory management, and copy constructors. Learn how to share functionality between objects and maintain data structure consistency.

  • C++
  • Object Programming
  • Inheritance
  • Data Structures
  • Memory Management

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. 1 CHAPTER 5 (PART 2) Object programming essentials

  2. 2 Chapter 5 Objectives Create objects based on objects of other objects of custom classes Understand the concept of inheritance syntax and operation Share functionality between objects using inheritance Implement data structures in C++ Understand the concept of dynamic allocation of C++ objects Prevent memory leaks and deallocate acquired resources Provide derived data about the implemented data structure Keep the data structure consistent at all times Traverse data structures Access data stored in data structures Create copies of data structures Implement and use copy constructors

  3. 3 5.4 STATIC COMPONENTS

  4. 4 5.4.1 The auto keyword (1) The auto keyword came from the ancestor of C++ , the C programming language. All the variables in your code belong to one of two categories. They are: automatic variables, created and destroyed automatically during program execution static variables, existing continuously during the whole program execution The C and C++ programming languages assume that all variables are automatic by default unless they are declared explicitly as static.

  5. 5 5.4.1 The auto keyword (1) It s interesting that the auto keyword is still commonly used in older versions of the C language to explicitly mark automatic variables. #include <iostream> using namespace std; void fun(void) { auto int var = 99; cout << "var = " << ++var << endl; } int main(void) { for(int i = 0; i < 5; i++) fun(); return 0; } Output: var = 100 var = 100 var = 100 var = 100 var = 100

  6. 6 5.4.1 The auto keyword (1) The var variable is declared inside the fun function and is automatic. We ve used the auto keyword, but the program behavior will remain the same if you remove the keyword (try it yourself). The variable is created each time the fun function is invoked and the variable is destroyed each time the function completes its execution. This means that, regardless of the number of invocations, the function will always produce the same output.

  7. 7 5.4.2 The auto keyword (2) Now, replace the auto keyword with static . The code looks nearly identical, but its behavior has been changed radically. #include <iostream> using namespace std; void fun(void) { static int var = 99; cout << "var = " << ++var << endl; } int main(void) { for(int i = 0; i < 5; i++) fun(); return 0; } Output: var = 100 var = 101 var = 102 var = 103 var = 104

  8. 8 5.4.3 Instances of the class The class is like a dummy be brought to life when its incarnation (in this case its object) is created. Every object created from a particular class is named a class s instance. From this point of view, each instance of the class is a separate universe and has nothing to do with any of the remaining instances. All the object s components (fields and functions) are enclosed inside the instance.

  9. 9 5.4.3 Instances of the class In consequence, we mustn t use any of the class components until we ve created an object of that class. The following snippet is wrong and will cause a compilation error. #include <iostream> using namespace std; class Class { public: int val; void print(void) { cout << val << endl; } }; int main(void) { Class::val = 0; //Error Class::print(); //Error return 0; }

  10. 10 5.4.4 Static components of the class All the rules on the previous slide are true if they refer to the non-static components of the class (both fields and functions). The C++ language allows us to define other kinds of components too, they re called static components . A static component exists throughout the whole life of the program. Moreover, there is always only one component regardless of the number of instances of the class. We can say that all the instances share the same static components.

  11. 11 5.4.4 Static components of the class Output: Static = 1, NonStatic = 10 Static = 2, NonStatic = 20

  12. 12 5.4.4 Static components of the class The static variable has to have both an explicitly expressed separate definition a possible initialization, and both must be placed outside the class definition. Another rationale says that the definition has to be separate from the class body because static variables are not actually part of any object. In our program, we do this with a line of the code between the Class body and the main function. Removing this line generates an error.

  13. 13 5.4.5 Static class variables (1) The static class variables usually used as counters of instances of a particular class. The next program implements this idea in a very simple way. We ll increment the static variable Counter inside the Class constructor and decrement it inside the Class destructor Note once again that the Counter field is accessed directly when it s being used inside the class and with the :: operator when it s being used outside the class.

  14. 14 5.4.5 Static class variables (1) Output: 2 instances so far 4 instances Bye, bye!

  15. 15 5.4.6 Static class variables (2) Static variable can be defined private in the class This will obviously prevent direct access to the variable, but it may be something we want if we want to protect the value against any unauthorized modification. Note that any attempts to access the Counter variable expressed like this: Class::Counter = 1; are strictly prohibited.

  16. 16 5.4.6 Static class variables (2) Output: 2 instances 4 instances Bye, bye!

  17. 17 5.4.7 Static class functions It s not only class variables that can be declared as static functions can be declared like this, too. The static function, like a static variable, may also be accessed (or more precisely, invoked) when no instances of the class have been created. Note that the static function may be invoked from inside the class, like this: Class::HowMany(); or by using any of the existing instances, like this: b.HowMany();

  18. 18 5.4.7 Static class functions Output: 0 instances 2 instances 4 instances Bye, bye!

  19. 19 5.4.8 Static vs. non-static components The coexistence of both static and non-static components within a single class causes some additional issues which we need to take into consideration.

  20. 20 5.4.9 Static static interaction The first test program demonstrates a case when a static function tries to invoke another static function. A case like this is always possible, as both functions are available during the entire life of the program. Output: static static

  21. 21 5.4.11 Static non-static interaction The second test program demonstrates a case when a static function tries to invoke a non-static function. A case like this is not possible, as the function being invoked exists when and only when any of the objects which contain this function also exists. The function cannot be successfully accessed without specifying the associated object. Syntax Error Static member can t invoke non-static member

  22. 22 5.4.13 Non-static static interaction The third test case refers to the situation where a non- static function invokes a static function. A case like this is always possible, as the static function is available before any object has been created. Output: static

  23. 23 5.4.15 Static vs. non-static components The last remaining option is simple. We don t need to do any experiment to find out the answer to the following question: Is it possible to invoke a non-static function from within a non- static function? Yes, obviously it is possible. What s more, we ve done it many times before.

  24. 24 Example

  25. 25 5.5 OBJECTS VS. POINTERS AND OBJECTS INSIDE THE OBJECTS

  26. 26 5.5.1 Pointers to objects So far we ve treated objects like ordinary variables and assumed that an object is created in the place where it is declared and destroyed when its declaration scope is exited. Objects may also exist as dynamically created and destroyed entities. In other words, objects may appear on demand when they re needed and vanish in the same way.

  27. 27 5.5.1 Pointers to objects Output: Object constructed! Object destructed!

  28. 28 5.5.1 Pointers to objects We ve created one object of that class using the new keyword. Note that we can omit the empty parentheses after the Class name in either case, the parameter-less constructor will be activated. The object is destroyed using the delete keyword. The process of destroying the object begins with the implicit invocation of its destructor.

  29. 29 5.5.2 Pointers to fields All the variables, including objects, brought to life in the ordinary way live in a separate area of memory called the stack. It s a memory region dedicated to storing all automatic entities. The stack grows when new automatic variables are created and shrinks when the variables are no longer needed. Note that this process is beyond your control. You cannot affect the way in which the stack changes.

  30. 30 5.5.2 Pointers to fields The entities created on demand (by the new keyword) are created in a specific memory region usually called a heap. In contrary to the stack, the heap is fully under your control. You decide how many variables, arrays, objects, etc. will occupy the heap and it s up to you when these entities end their lives. The object being stored in the heap must be accessed in a way that resembles the access to the dynamically allocated structures. You mustn t use the ordinary dotted notation as there s no structure (object) which can play the role of the left argument of the . operator unless you dereference the pointer. You need to use the arrow (->) operator instead.

  31. 31 5.5.2 Pointers to fields The general rule says that: if S is a structure or class and S has a component named C and if p is a pointer to a structure of type S, then the C component may be accessed in the two following ways: (*p).C; // p is explicitly dereferenced in order to access the C component p->C; // p is implicitly dereferenced in order to access the C component

  32. 32 5.5.2 Pointers to fields We ve added one field to the Class. It s declared in the public part of the class so you can access it freely from outside the class. There s one catch you have to use the -> operator. You can use * with . Instead of -> operator. ptr->value = 0; (*ptr).value = 0; Look at the parentheses around the argument of the ++ operator. Do we really need them? What would happen when you removed them?

  33. 33 5.5.2 Pointers to fields Output: Object constructed! 1 Object destructed!

  34. 34 5.5.3. Pointers to functions Member functions invoked for an object accessed through the pointer have to be accessed using the arrow operator, too. Output: Object constructed! Value = 2 Object destructed!

  35. 35 5.5.4 Selecting the constructor If a class has more than one constructor, one of them may be chosen during object creation. This is done by specifying the form of the parameter list associated with the class name. The list should be unambiguously compatible with one of the available class constructors. We ve modified our program again. There are already two constructors. We ve created two new objects inside the main functions. They differ in the constructor used to build each of the objects. In effect, their value fields have different values assigned to them.

  36. 36 5.5.4 Selecting the constructor class Class { public: }; int main(void) { } Class(void) { cout << "Object constructed (#1)" << endl; } Class(int v) { value = v; cout << "Object constructed (#2)" << endl; } ~Class(void) { cout << "Object destructed! val = " << value << endl; } void IncAndPrint(void) { cout << "value = " << ++value << endl; } int value; Output: Object constructed (#1) Object constructed (#2) value = 2 value = 3 Object destructed! val = 3 Object destructed! val = 2 Class *ptr1, *ptr2; ptr1 = new Class; ptr2 = new Class(2); ptr1 -> value = 1; ptr1 -> IncAndPrint(); ptr2 -> IncAndPrint(); delete ptr2; delete ptr1; return 0;

  37. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 37 Classes and Pointers: Some Peculiarities There are three things to take care when the class uses pointer members: 1. Destructor to delete the dynamic memory 2. Overload the assignment operator 3. Override the Copy constructor

  38. 38 5.3.12 Memory leaks Many of the objects are allocated memory that they need for their operation. This memory should be released when the object finishes its activity and the best way to do this is to do the cleaning automatically. Failure to clean the memory will cause a memory leaking , where the unused (but still allocated!) memory grows in size, affecting system performance. Take a look at the example on the following slides. The Class class has only one constructor, which is responsible for allocating memory of the size specified by its parameter value. The object of this class is created as a local variable inside the MakeALeak() functions.

  39. 39 5.3.12 Memory leaks #include <iostream> using namespace std; class Class { public: Class(int val) { value = new int[val]; cout << "Allocation (" << val << ") done." << endl; } int *value; }; void MakeALeak(void) { Class object(1000); } int main(void) { MakeALeak(); return 0; }

  40. 40 5.3.12 Memory leaks the constructor explicitly allocates another part of the memory The object variable is an example of an automatic variable . This means that the variable automatically finishes its life when the execution of the function containing the variable s declaration ends. We can expect that a return from the MakeALeak() functions will cause the following action: the memory allocated to the object itself is freed(this is done implicitly). Unfortunately, the memory explicitly allocated by the constructor remains allocated. To make matters worse, we ve lost the only pointer that held the address of that memory (it was stored by the value field, but the object containing this field doesn t exist anymore). The fairly large portion of memory has leaked.

  41. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 41 Destructor If objectOne goes out of scope, the member variables of objectOne are destroyed The memory space of the dynamic array would stay marked as allocated, even though it cannot be accessed Solution: Put the necessary code in the destructor to ensure that when objectOne goes out of scope, the memory of the array is deallocated

  42. 42 5.3.13 Destructors #include <iostream> using namespace std; class Class { public: Class(int val) { } ~Class(void) { } int *value; }; void MakeALeak(void) { Class object(1000); } int main(void) { MakeALeak(); return 0; } value = new int[val]; cout << "Allocation (" << val << ") done." << endl; delete [ ] value; cout << "Deletion done." << endl;

  43. 43 5.3.11 Copying constructors There is a special kind of constructor intended to copy one object into another. They are referred to as copying constructors and are implicitly invoked when a declaration of an object is followed by an initiator. Constructors of this kind have one parameter referenced to an object of the same class and are used to copy all important data from the source object to the newly created object (or more precisely, to the object currently being created).

  44. 44 5.3.11 Copying constructors Output: 124 123 123

  45. 45 5.3.11 Copying constructors If the copying constructor doesn t exist within a particular class and the initiator is actually used during the declaration of an object, its content will be copied field by field , as if the object had been cloned. The copying constructor will also be used when the context requires a copy of a specific object, e.g. when a particular object is transferred to a function as a value- passed actual parameter.

  46. 46 5.3.11 Copying constructors Output: Hi from the copy constructor! I'm here!

  47. 47 5.3.11 Copying constructors The following example shows two different classes. The former does have a copying constructor, while the latter doesn t. Two objects are created for both of the classes while both of the second objects are created by copying the first ones.

  48. 48 5.3.11 Copying constructors class Class1 { public: }; class Class2 { public: }; int main(void) { } Class1(int val) { this -> value = val; } Class1(Class1 const &source) { value = source.value + 100; } int value; Output: 200 200 Class2(int val) { this -> value = val; } int value; Class1 object11(100), object12 = object11; Class2 object21(200), object22 = object21; cout << object12.value << endl; cout << object22.value << endl; return 0;

  49. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 49 Copy Constructor (continued) Copy constructor automatically executes in three situations: When an object is declared and initialized by using the value of another object When, as a parameter, an object is passed by value When the return value of a function is an object

  50. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 50 Copy Constructor with pointer member This initialization is called the default member-wise initialization Initialization due to the constructor, called the copy constructor (provided by the compiler)

More Related Content