SPL – PS4 C++ Advanced OOP
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
The destructor Similar to the destructor we’ve built in the previous PS
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.
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.
The move assignment operator Similar to the copy assignment operator Like the move constructor, “steals” resources from other object
Smart pointers Included in the standard library Provides some garbage collection facility In this practical session we’ll talk about unique pointer
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
Unique pointer example
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
C++ Inheritance example
C++ Inheritance example(cont)
C++ Inheritance example(cont)
C++ Inheritance types
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
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.
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
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.
Virtual function example
Virtual destructor Without garbage collector, polymorphism can be problematic when attempting to release memory.
Virtual destructor(cont) Can be fixed by declaring destructors virtual
Pure virtual functions and abstract classes Pure virtual functions don’t include implementation in their base class. Therefore, cannot make instantiation of base class.
Namespaces Similar to Java packages.
Namespaces(cont)