Lecture 6
Lecture 6
2
Class Glossary
Class Definition.
Class Implementation.
Class data members.
Class Member functions.
Class Constructors.
Class Destructor.
Class setters.
Class getters.
Class operators.
Class static members.
Class Inheritance.
3
Classes syntax
Definition starts with the keyword class
Classes have three access modifiers:
private, protected and public
By default everything is private
Classes can contain data and functions
Access members with a "."
Have two types of special functions:
Constructors: called upon creation of an
instance of the class
Destructor: called upon destruction of an
instance of the class
GOOGLE-STYLE Use CamelCase for class name
0
https://google.github.io/styleguide/cppguide.html#Type_Names
4
What about structs?
0
https://google.github.io/styleguide/cppguide.html#Access_Control
0
https://google.github.io/styleguide/cppguide.html#Variable_Names
7
Constructors and Destructor
8
Many ways to create instances
1 class SomeClass {
2 public :
3 SomeClass (); // Default constructor .
4 SomeClass (int a); // Custom constructor .
5 SomeClass (int a, float b); // Custom constructor .
6 ~ SomeClass (); // Destructor .
7 };
8 // How to use them?
9 int main () {
10 SomeClass var_1; // Default constructor
11 SomeClass var_2 (10); // Custom constructor
12 // Type is checked when using {} braces . Use them!
13 SomeClass var_3 {10}; // Custom constructor
14 SomeClass var_4 = {10}; // Same as var_3
15 SomeClass var_5 {10, 10.0}; // Custom constructor
16 SomeClass var_6 = {10, 10.0}; // Same as var_5
17 return 0;
18 }
9
Setting and getting data
10
Declaration and definition
Data members belong to declaration
Class methods can be defined elsewhere
Class name becomes part of function name
1 // Declare class .
2 class SomeClass {
3 public :
4 SomeClass ();
5 int var () const ;
6 private :
7 void DoSmth ();
8 int var_ = 0;
9 };
10 // Define all methods .
11 SomeClass :: SomeClass () {} // This is a constructor
12 int SomeClass :: var () const { return var_; }
13 void SomeClass :: DoSmth () {}
11
Always initialize members for
classes
C++ 11 allows to initialize variables in-place
Do not initialize them in the constructor
No need for an explicit default constructor
1 class Student {
2 public :
3 // No need for default constructor .
4 // Getters and functions omitted .
5 private :
6 int earned_points_ = 0;
7 float happiness_ = 1.0f;
8 };
13
Const correctness
14
Typical const error
1 # include <iostream >
2 # include <string >
3 using namespace std;
4 class Student {
5 public :
6 Student ( string name) : name_{name} {}
7 // This function * might * change the object
8 const string & name () { return name_;
9 private :
10 string name_;
11 };
12 void Print( const Student & student ) {
13 cout << " Student : " << student .name () << endl;
14 }
1 error : passing "const Student " as "this" argument
discards qualifiers [-fpermissive ]
2 cout << " Student : " << student .name () << endl;
3 ^
15
Intuition lvalues, rvalues
Every expression is an lvalue or an rvalue
lvalues can be written on the left of
assignment operator (=)
rvalues are all the other expressions
Explicit rvalue defined using &&
Use std::move(…) to explicitly convert an
lvalue to an rvalue
1 int a; // "a" is an lvalue
2 int& a_ref = a; // "a" is an lvalue
3 // " a_ref " is a reference to an lvalue
4 a = 2 + 2; // "a" is an lvalue ,
5 // "2 + 2" is an rvalue
6 int b = a + 2; // "b" is an lvalue ,
7 // "a + 2" is an rvalue
8 int && c = std :: move(a); // "c" is an rvalue
16
std::move
0
https://en.cppreference.com/w/cpp/utility/move
17
Important std::move
18
Hands on example
1 # include <iostream >
2 # include <string >
3 using namespace std; // Save space on slides .
4 void Print( const string & str) {
5 cout << " lvalue : " << str << endl;
6 }
7 void Print( string && str) {
8 cout << " rvalue : " << str << endl;
9 }
10 int main () {
11 string hello = "hi";
12 Print (hello);
13 Print (" world ");
14 Print (std :: move(hello));
15 // DO NOT access " hello " after move!
16 return 0;
17 }
19
Never access values after move
The value after move is undefined
1 string str = " Hello ";
2 vector <string > v;
3
4 // uses the push_back (const T&) overload , which means
5 // we'll incur the cost of copying str
6 v. push_back (str);
7 cout << " After copy , str is " << str << endl;
8
9 // uses the rvalue reference push_back (T&&) overload ,
10 // which means no strings will be copied ; instead ,
11 // the contents of str will be moved into the vector .
12 // This is less expensive , but also means str might
13 // now be empty .
14 v. push_back (move(str));
15 cout << " After move , str is " << str << endl;
20
std::move performance
1 // MyClass has a private member that contains 200 strings
2 struct MyClass {
3 int id_ = 0;
4 std::vector <std::string > names_{
5 "name", "name", "name", "name", "name", "name", "name", "name", "name",
6 "name", "name", "name", "name", "name", "name", "name", "name", "name",
7 "name", "name", "name", "name", "name", "name", "name", "name", "name",
8 "name", "name", "name", "name", "name", "name", "name", "name", "name",
9 "name", "name", "name", "name", "name", "name", "name", "name", "name",
10 "name", "name", "name", "name", "name", "name", "name", "name", "name",
11 "name", "name", "name", "name", "name", "name", "name", "name", "name",
12 "name", "name", "name", "name", "name", "name", "name", "name", "name",
13 "name", "names", "name", "name", "name", "name", "name", "name", "name",
14 "name", "name", "name", "name", "name", "name", "name", "name", "name",
15 "name", "name", "name", "name", "name", "name", "name", "name", "name",
16 "name", "name", "name", "name", "name", "name", "name", "name", "name",
17 "name", "name", "name", "name", "name", "name", "name", "name", "name",
18 "name", "name", "name", "name", "name", "name", "name", "name", "name",
19 "name", "name", "name", "name", "name", "name", "name", "name", "name",
20 "name", "name", "name", "name", "name", "name", "name", "name", "name",
21 "name", "name", "name", "name", "name", "name", "name", "name", "name",
22 "name", "name", "name", "name", "name", "name", "name", "name", "name",
23 "name", "name", "name", "name", "name", "name", "name", "name", "name",
24 "name", "name", "name", "name", "name", "name", "name", "name", "name",
25 "name", "name", "name", "name", "name", "name", "name", "name", "name",
26 "name", "name", "name", "name", "name", "name", "name", "name", "name"};
27 };
21
std::move performance
22
std::move performance
23
How to think about std::move
24
Custom operators for a class
25
Example operator <
1 # include <algorithm >
2 # include <vector >
3 class Human {
4 public :
5 Human (int kindness ) : kindness_ { kindness } {}
6 bool operator <( const Human& other) const {
7 return kindness_ < other. kindness_ ;
8 }
9
10 private :
11 int kindness_ = 100;
12 };
13 int main () {
14 std :: vector <Human > humans = {Human {0}, Human {10}};
15 std :: sort( humans .begin (), humans .end ());
16 return 0;
17 }
26
Example operator <<
1 #include <iostream >
2 #include <vector >
3 class Human {
4 public:
5 int kindness(void) const { return kindness_; }
6 private:
7 int kindness_ = 100;
8 };
9
10 std:: ostream& operator <<(std:: ostream& os , const Human& human) {
11 os << "This human is this kind: " << human.kindness ();
12 return os;
13 }
14
15 int main () {
16 std::vector <Human > humans = {Human {0}, Human {10}};
17 for (auto && human : humans) {
18 std:: cout << human << std:: endl;
19 }
20 return 0;
21 }
27
Copy constructor
28
Copy assignment operator
Copy assignment operator is called
automatically when the object is
assigned a new value from an Lvalue
For class MyClass has a signature:
MyClass& operator=(const MyClass& other)
Returns a reference to the changed
object
Use *this from within a function of a class
to get a reference to the current object
1 MyClass a; // Calling default constructor .
2 MyClass b(a); // Calling copy constructor .
3 MyClass c = a; // Calling copy constructor .
4 a = b; // Calling copy assignment operator .
29
Move constructor
30
Move assignment operator
31
1 class MyClass {
2 public :
3 MyClass () { cout << " default " << endl; }
4 // Copy (&) and Move (&&) constructors
5 MyClass ( const MyClass & other ) {
6 cout << "copy" << endl;
7 }
8 MyClass ( MyClass && other) {
9 cout << "move" << endl;
10 }
11 // Copy (&) and Move (&&) operators
12 MyClass & operator =( const MyClass & other) {
13 cout << "copy operator " << endl;
14 }
15 MyClass & operator =( MyClass && other) {
16 cout << "move operator " << endl;
17 }
18 };
19
20 int main () {
21 MyClass a; // Calls DEFAULT constructor
22 MyClass b = a; // Calls COPY constructor
23 a = b; // Calls COPY assignment operator
24 MyClass c = std :: move(a); // Calls MOVE constructor
25 c = std :: move(b); // Calls MOVE assignment operator
26 } 32
33
Do I need to define all of them?
The constructors and operators will be
generated automatically
Under some conditions…
Six special functions for class MyClass:
MyClass()
MyClass(const MyClass& other)
MyClass& operator=(const MyClass& other)
MyClass(MyClass&& other)
MyClass& operator=(MyClass&& other)
~MyClass()
None of them defined: all auto-generated
Any of them defined: none auto-generated
34
Rule of all or nothing
0
Arne Mertz: https://arne-mertz.de/2015/02/the-rule-of-zero-revisited-the-rule-of-all-or-nothing/
0
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cdefop-default-operations
35
Deleted functions
Any function can be set as deleted
1 void SomeFunc (...) = delete ;
39
1 # include <cmath >
2
3 class Point {
4 public :
5 Point (int x, int y) : x_(x), y_(y) {}
6
7 static float Dist( const Point& a, const Point& b) {
8 int diff_x = a.x_ - b.x_;
9 int diff_y = a.y_ - b.y_;
10 return sqrt( diff_x * diff_x + diff_y * diff_y );
11 }
12
13 float Dist( const Point& other) {
14 int diff_x = x_ - other.x_;
15 int diff_y = y_ - other.y_;
16 return sqrt( diff_x * diff_x + diff_y * diff_y );
17 }
18
19 private :
20 int x_ = 0;
21 int y_ = 0;
22 }; 40
Static member functions
Allow us to define method that does not
require an object too call them, but are
somehow related to the Class/Type
1 # include <iostream >
2 using std :: cout;
3 using std :: endl;
4
5 int main () {
6 Point p1(2, 2);
7 Point p2(1, 1);
8 // Call the static method of the class Point
9 cout << "Dist is " << Point :: Dist(p1 , p2) << endl;
10
11 // Call the class - method of the Point object p1
12 cout << "Dist is " << p1.Dist(p2) << endl;
13 }
41
Using for type aliasing
42
Using for type aliasing
1 # include <array >
2 # include <memory >
3 template <class T, int SIZE >
4 struct Image {
5 // Can be used in classes .
6 using Ptr = std :: unique_ptr <Image <T, SIZE >>;
7 std :: array <T, SIZE > data;
8 };
9 // Can be combined with " template ".
10 template <int SIZE >
11 using Imagef = Image <float , SIZE >;
12 int main () {
13 // Can be used in a function for type aliasing .
14 using Image3f = Imagef <3>;
15 auto image_ptr = Image3f :: Ptr(new Image3f );
16 return 0;
17 }
43
Enumeration classes
Store an enumeration of options
Usually derived from int type
Options are assigned consequent numbers
Mostly used to pick path in switch
1 enum class EnumType { OPTION_1 , OPTION_2 , OPTION_3 };
46
Suggested Video
C++ Classes
https://youtu.be/2BP8NhxjrO0
48
Suggested Video
Unit Tests
https://youtu.be/nbFXI9SDfbk
49
References 1
Classes
https://en.cppreference.com/w/cpp/classes
Data Members
https://en.cppreference.com/w/cpp/language/data_members
Member Functions
https://en.cppreference.com/w/cpp/language/member_functions
Static
https://en.cppreference.com/w/cpp/language/static
Operators
https://en.cppreference.com/w/cpp/language/operators
50
References 2
Constructors
https://en.cppreference.com/w/cpp/language/constructor
Destructor
https://en.cppreference.com/w/cpp/language/destructor
Copy Constructor
https://en.cpprefeDestructorrence.com/w/cpp/language/copy_destructor
Move Constructor
https://en.cppreference.com/w/cpp/language/move_constructor
Copy Assignment
https://en.cppreference.com/w/cpp/language/copy_assignment
Move Assignment
https://en.cppreference.com/w/cpp/language/move_assignment
51