Download presentation
Presentation is loading. Please wait.
1
SPL – PS4 C++ Advanced OOP
2
The rule of 5 The rule of 3 has some problems.
Starting from C++11, two additional functions were added to the rule of 3. Move constructor Move assignment operator Like the rule of 3, implies only for classes that manage resources
5
The destructor Similar to the destructor we’ve built in the previous PS
6
The move constructor Called when an object is initialized from an rvalue reference. Initialization std::string a = “str” Function argument passing v.push_back(“str”) Function return by value “Steals” resources from other object.
7
The move constructor (cont)
A new charArray object will be created with the given string The move constructor will create a new object inside the vector, and will steal the string The pointer in the first created object will become nullptr The first object will be destroyed, since it’s pointer became null, the object on the vector won’t be affected.
8
The move assignment operator
Similar to the copy assignment operator Like the move constructor, “steals” resources from other object
9
Smart pointers Included in the standard library
Provides some garbage collection facility In this practical session we’ll talk about unique pointer
10
Unique pointer Provides limited garbage-collection facility
Automatically deletes the pointer they manage, when they are destroyed or assigned another value. Can also delete the pointer they manage by calling unique_ptr::reset explicitly Owns their pointer uniquely, no other pointer should point to managed object
11
Unique pointer example
12
C++ Inheritance A relationship among classes, in which one class shares the structure or functionality of another class. In C++ there are three levels of inheritance visibility
13
C++ Inheritance example
14
C++ Inheritance example(cont)
15
C++ Inheritance example(cont)
16
C++ Inheritance types
17
Public Inheritance example
isEmpty() is a part of the base class intArray Insert(int,int) is a part of the base class intArray but his functionality is extended in the derived class. biggest() exists only in the derived class
18
Private inheritance Makes all public and protected methods in base class private in derived class. Defines a “has-a” and not a “is-a” relation between base and derived class.
19
Constructor of derived class
The C++ compiler generates a call to the default constructor of the base class, unless explicitly instructed otherwise. A better solution is to call directly to the CTOR of the base class, using the following syntax
20
Virtual functions Virtual functions are functions whose behavior can be overridden by derived class. Unlike non-virtual function, overridden behavior is preserved even if no compile-time data is available on the type of the class. If derived virtual function has a different access level then base function, their access level might not be respected.
21
Virtual function example
22
Virtual destructor Without garbage collector, polymorphism can be problematic when attempting to release memory.
23
Virtual destructor(cont)
Can be fixed by declaring destructors virtual
24
Pure virtual functions and abstract classes
Pure virtual functions don’t include implementation in their base class. Therefore, cannot make instantiation of base class.
25
Namespaces Similar to Java packages.
26
Namespaces(cont)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.