Classes A very brief introduction to the elementary basics of pointers Homework 9. Classes Ján Dugáček November 21, 2018 Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Table of Contents 1 Classes Motivation struct Methods class Exercise Inheritance 2 A very brief introduction to the elementary basics of pointers Motivation std::shared_ptr Naked pointer Empty pointers std::unique_ptr Exercise 3 Homework Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation struct Methods class Exercise Inheritance Motivation It sucks to have a heterogenous table stored in a vector of vectors data [ x ] [ 1 ] = ( data [ x − 1 ] [ 1 ] + data [ x + 1 ] [ 2 ] ) / 2; // . . . data [ x ] . fx = ( data [ x − 1 ] . fx + data [ x + 1 ] . fx ) / 2; Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation struct Methods class Exercise Inheritance std::pair std : : pair a ; a . f i r s t = 3; a . second = 3 . 5 ; std : : vector > v ; v . push_back ( a ) ; v . push_back ( std : : make_pair (3 , 3 . 5 ) ) ; std::pair is a convenient way to create small classes of two elements of preset types, accessed as first and second They shine when set as pairs of values in vectors and other containers std::make_pair is a function that returns a std::pair object with types as its two arguments Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation struct Methods class Exercise Inheritance struct s t r u c t f u n c t i o n P o i n t { bool v a l i d ; f l o a t x ; f l o a t fx ; }; std : : vector func ; f u n c t i o n P o i n t point { f a l s e , 1 , 2}; point . v a l i d = t r u e ; func . push_back ( point ) ; struct creates new type of variable that is composed of other types The variables it contains can be accessed using the dot after variable name The variables inside are called members Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation struct Methods class Exercise Inheritance struct #2 s t r u c t emergency { bool nuclearWar = f a l s e ; bool a l i e n I n v a s i o n = f a l s e ; bool l e a k i n g T o i l e t = f a l s e ; }; emergency s i t u a t i o n ; l e a k i n g T o i l e t = t r u e ; std : : cout << s i t u a t i o n . nuclearWar << std : : endl ; You can set the default values of the variables Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation struct Methods class Exercise Inheritance Methods s t r u c t vec3D { f l o a t x ; f l o a t y ; f l o a t z ; void nor malis e () { f l o a t l ength = s q r t ( x ∗ x + y ∗ y + z ∗ z ) ; x /= length ; z /= len gth ; y /= length ; } }; // . . . vec . n ormal ise ( ) ; Functions defined in structs (called methods) can access and modify its variables They are called in a similar way than members are accessed Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation struct Methods class Exercise Inheritance Methods #2 s t r u c t quaternion { f l o a t r e a l , i , j , k ; quaternion operator +(const quaternion& o ) { quaternion r e s u l t ; r e s u l t . r e a l = r e a l + o . r e a l ; r e s u l t . i = r e a l + o . i ; r e s u l t . j = r e a l + o . j ; r e s u l t . k = r e a l + o . k ; r e t u r n r e s u l t ; } }; // . . . quat3 = quat1 + quat2 ; Same applies to operators, allowing you to get normally working algebraic types Uses one less argument, because the object left from the operator is the method’s object itself Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation struct Methods class Exercise Inheritance Constructor and destructor s t r u c t s u p e r S t r u c t { s u p e r S t r u c t () { std : : cout << " SuperStruct has been created !\ n } ~s u p e r S t r u c t () { std : : cout << " SuperStruct has been destroyed ! } }; // . . . s u p e r S t r u c t super ; Constructor is a method called when the object is created Destructor is a method called when the object is being deallocated Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation struct Methods class Exercise Inheritance Constructor s t r u c t keeper { std : : vector & vec ; const i n t s i z e ; keeper ( std : : vector & vec ) : vec ( vec ) , s i z e ( vec . s i z e ( ) ) { } }; Constructors can have an initialisation section that can set constant variables and references Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation struct Methods class Exercise Inheritance class c l a s s Privacy { i n t s e c r e t ; p u b l i c : void s e t S e c r e t ( i n t newSecret ) { s e c r e t = newSecret ; } p r i v a t e : i n t r e v e a l S e c r e t () { r e t u r n s e c r e t ; } }; class is like struct, but its members are private by default and can be accessed only by methods of that class Members or methods after the public declaration are accessible from everywhere Here, the secret is quite hard to get from the objects struct can also have private members, but they are public by default Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation struct Methods class Exercise Inheritance Exercise 1 Write a function that transforms an inconvenient convenient vector of vectors into a vector of std::pairs 2 Create a class that has a method that consecutively returns strings like 0000, 0001, ... 0042, ..., 0997 etc. 3 Create a radionuclide class that has a chance to change its decomposed member when a certain method is called 4 Create a rock class that represents a rigid body in gravitational field, give it a method that makes its properties develop in time Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation struct Methods class Exercise Inheritance Advanced exercise 1 Create a triplet class that is like std::pair, but it contains three elements 2 Create a class that represents numbers in modular arithmetic and implement some of its operators Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation struct Methods class Exercise Inheritance Inheritance s t r u c t toRead { unsigned i n t index ; }; s t r u c t warning : p u b l i c toRead { std : : s t r i n g t e x t ; }; s t r u c t message : p u b l i c toRead { std : : s t r i n g t e x t ; user author ; }; structs warning and message inherit members and methods of toRead They can be assigned to a variable of type toRead, allowing the same function to access their index member Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation struct Methods class Exercise Inheritance Advanced exercise 1 Create a class that represents arithmetic functions composed of variables, addition, subtraction, multiplication and division (the easiest way to do it is to make a tree structure of classes using inheritance) Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation std::shared_ptr Naked pointer Empty pointers std::unique_ptr Exercise Motivation Normal variable assignment is deep copy, the whole object is copied This is a problem for larger objects or objects we want to access from more locations Reference is a shallow copy, the variable may have a different name but address the same variable References are fine when used as function arguments, but objects often outlive the blocks they are created in Pointers are more powerful references Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation std::shared_ptr Naked pointer Empty pointers std::unique_ptr Exercise std::shared_ptr std : : shared_ptr makeHugeObject ( const std : : s t r i n g& f i l e ) { std : : i f s t r e a m i n ( f i l e ) ; std : : shared_ptr made = std : : make_shared( i n ) ; r e t u r n made ; } // . . . std : : shared_ptr huge = makeHugeObject ( "megadat" ) ; std::shared_ptr is a class that contains a single object that doesn’t copy it if copied All copies of the shared pointer contain the same object The object stops existing when the last shared pointer is deallocated You have to make sure the object will not contain a copy of the shared pointer (or some other circular reference), otherwise it will keep existing until the program exits (it’s called memory leak) Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation std::shared_ptr Naked pointer Empty pointers std::unique_ptr Exercise std::shared_ptr #2 std : : shared_ptr krupa = std : : make_shared("A" ) ; krupa−>push_back ( ’B ’ ) ; std : : s t r i n g betterKrupa = ∗ krupa ; krupa−>append ( "CD" ) ; std : : s t r i n g& l i t e r a t e K r u p a = ∗ krupa ; krupa−>append ( "E" ) ; Accessing members of the object in std::shared_ptr is done through the -> operator Use the left asterisk * to obtain the object inside (it’s not a copy if not assigned to a non-reference variable) std::shared_ptr is much like a reference, but it survives the deletion of the original and can be replaced at the cause of slightly harder usage Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation std::shared_ptr Naked pointer Empty pointers std::unique_ptr Exercise Naked pointer std : : s t r i n g ∗ superKrupa = krupa . get ( ) ; std : : cout << ∗ superKrupa << std : : endl ; superKrupa−>push_back ( ’F ’ ) ; std : : s t r i n g ∗ krupaPtr = &betterKrupa ; Naked pointer allows accessing the variable as other pointer types, but it’s just a number and has no methods It can be obtained from any variable using the left & operator If accesseed after the object was deleted, bad mojo will happen! It can be useful to allow the object inside a std::shared_ptr to keep access to an object that holds the std::shared_ptr It can also be used instead of reference if for some reasons a reference cannot do the trick Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation std::shared_ptr Naked pointer Empty pointers std::unique_ptr Exercise Empty pointers krupa . r e s e t ( ) ; superKrupa = n u l l p t r ; i f ( superKrupa ) std : : s t r i n g << "There i s a superKrupa " << std : : endl ; Unlike references, pointers can be empty An empty pointer contains address 0 (for readability, it’s written as nullptr) Accessing an empty pointer causes the program to reliably crash: ∗(( f l o a t ∗) n u l l p t r ) = 0; An empty pointer is considered false, a non-empty one is considered true Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation std::shared_ptr Naked pointer Empty pointers std::unique_ptr Exercise std::unique_ptr std : : unique_ptr makeHugeObject ( const std : : s t r i n g& f i l e ) { std : : i f s t r e a m i n ( f i l e ) ; std : : unique_ptr made = std : : make_unique( i n ) ; r e t u r n std : : move(made ) ; } // . . . std : : unique_ptr huge = makeHugeObject ( "megadat" ) ; std::unique_ptr is very much like std::shared_ptr, but it cannot be copied It can be moved using std::move, a function that clears the original variable and saves it into the one it’s assigned to If you have to access it from elsewhere, you can use references or naked pointers Like naked pointer, it’s faster than std::shared_ptr Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Motivation std::shared_ptr Naked pointer Empty pointers std::unique_ptr Exercise Exercise 1 Use std::shared_ptr to create a class that keeps the following lines in a tree structure when it parses, writes and allows accessing the following markup Tools Hammers Small hammers Big hammers Screwdrivers Cross Screwdrivers Ján Dugáček 9. Classes Classes A very brief introduction to the elementary basics of pointers Homework Homework Write a function that analyses a line of noisy data (can be a vector) where it finds the point where it starts increasing and the point where it stops increasing and returns the interval where it increases and the amount it increased in a struct You have two weeks to do it Ján Dugáček 9. Classes