diff --git a/lectures/figures/encapsulation.png b/lectures/figures/encapsulation.png new file mode 100644 index 0000000..0fc8af6 Binary files /dev/null and b/lectures/figures/encapsulation.png differ diff --git a/lectures/figures/typing.png b/lectures/figures/typing.png new file mode 100644 index 0000000..feb552a Binary files /dev/null and b/lectures/figures/typing.png differ diff --git a/lectures/week6.html b/lectures/week6.html new file mode 100644 index 0000000..7142411 --- /dev/null +++ b/lectures/week6.html @@ -0,0 +1,3409 @@ + + + + + + + + + + + +talk slides + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ +
+

git pull upstream master

+
+
+
+
+ +
+

Answer questions from
the previous session

+ +
+
+
+
+ +
+

Concept of interfaces (C++)

    +
  • An interface is usually written in .hh files
  • +
  • It describes how to use a function
  • +
  • To be opposed with the body of a function which describes how it works
  • +
+


+

Why is it important to have this concept ?

+
+
+ +
+
    +
  • To allow collaborative work
  • +
  • To normalize the knowledge needed to call a function
  • +
  • To limit modification in cascade
  • +
+ +
+
+
+
+ +
+

C++ Class/Object

+
+
+
+
+ +
+
    +
  • void
  • +
  • char
  • +
  • short
  • +
  • int
  • +
  • long int
  • +
  • float
  • +
  • double
  • +
  • long double
  • +
  • pointers
  • +
+ +
+
+ +
+
    +
  • unsigned
  • +
  • const
  • +
+ +
+
+
+
+ +
+

How to define new types in C++ ?

+
+
+
+
+ +
+

Typedefs or type aliases


+ +
+
+ +
+
    +
  • old C++
  • +
+ +
+
+ +
+ +
+ +
+ +
typedef type name;
+
+// example
+typedef unsigned int UInt;
+typedef double Real;
+
+ + +
+ +
+ +
+
+ +
+
+ +
+
+ +
+
    +
  • modern C++
  • +
+ +
+
+ +
+ +
+ +
+ +
using name = type;
+
+// example
+using UInt = unsigned int;
+using Real = double;
+
+ + +
+ +
+ +
+
+
+
+ +
+

What is the difference with #define ?

+ +
+
+ +
+ +
+ +
+ +
#define UInt unsigned int
+
+ + +
+ +
+ +
+
+
+
+ +
+
+
+
+
+
+ +
+

enum or enumeration list

    +
  • Makes a type from an enumerated list of possibilities
  • +
+ +
+
+ +
+ +
+ +
+ +
enum ParticleType {
+  _pt_planet,
+  _pt_star,
+  _pt_atom,
+  _pt_ball)
+};
+
+ + +
+ +
+ +
+
+ +
+
    +
  • Internal representation $\Longrightarrow$ integers (it is a code)
  • +
  • Used in switch-case sequences
  • +
  • Other usage ?
  • +
+ +
+
+
+
+ +
+

Structures: struct

    +
  • Agglomeration of other types
  • +
+ +
+
+ +
+ +
+ +
+ +
struct Complex {
+  double real;
+  double imaginary;
+};
+
+ + +
+ +
+ +
+
+
+
+ +
+

Encapsulation

    +
  • Fundamental mechanism allowing to conceal, and what is accessible in a class
  • +
  • This is not existing in Python
  • +
+
+
+
+ +
+ + +
+
+
+
+ +
+

C++ classes

    +
  • Declarations are usually in a .hh file
  • +
  • declares the members and methods
  • +
+ +
+
+ +
+ +
+ +
+ +
class MyClass {
+public:
+  // Constructor
+  MyClass();
+  // Destructor
+  virtual ~MyClass();
+
+public:
+  ret method1(/* arguments */);
+
+private:
+  type member;
+};
+
+ + +
+ +
+ +
+
+
+
+ +
+

C++ Methods

    +
  • Methods(functions) can be overloaded (same name, different arguments and return types).
  • +
  • Default parameters are accepted.
  • +
+ +
+
+ +
+ +
+ +
+ +
class MyClass {
+
+public:
+  int foo(int a);
+  int foo(double a = 1e3);
+
+  int member1;
+};
+
+ + +
+ +
+ +
+
+ +
+
    +
  • Implementation of the methods (in a .cc file)
  • +
+ +
+
+ +
+ +
+ +
+ +
int MyClass::foo(int a) {
+  // ...
+}
+
+ + +
+ +
+ +
+
+
+
+ +
+

this points to the current object

+
+
+ +
+ +
+ +
+ +
int MyClass::foo(int value) {
+  // access through the pointer
+  this->member1 = value;
+}
+
+ + +
+ +
+ +
+
+ +
+
    +
  • helpful to distinguish object members from parameters
  • +
+ +
+
+ +
+ +
+ +
+ +
ret1 MyClass::foo(type1 member1) {
+  // member1 = member1; ???
+  this->member1 = member1;
+}
+
+ + +
+ +
+ +
+
+
+
+ +
+

Object instanciation and method call

+
+
+ +
+ +
+ +
+ +
ClassName object;
+ret1 a = object.method1();
+
+ + +
+ +
+ +
+
+
+
+ +
+

Construction with arguments

+
+
+ +
+ +
+ +
+ +
class MyClass {
+public:
+  MyClass(int a);
+};
+
+MyClass::MyClass(int a) {
+  // some code
+}
+
+int main() {
+  // object instanciation
+  MyClass object(1);
+}
+
+ + +
+ +
+ +
+
+
+
+ +
+

Dynamic allocation and method call

    +
  • old style C++
  • +
+ +
+
+ +
+ +
+ +
+ +
ClassName *ptr = new ClassName();
+ret1 a = ptr->method1();
+delete ptr;
+
+ + +
+ +
+ +
+
+ +
+
    +
  • in modern C++
  • +
+ +
+
+ +
+ +
+ +
+ +
#include <memory>
+
+auto unique_ptr = std::make_unique<ClassName>();
+ret1 a = unique_ptr->method1();
+
+auto shared_ptr = std::make_shared<ClassName>();
+ret1 b = shared_ptr->method1();
+
+ + +
+ +
+ +
+
+
+
+ +
+

Inheritance

    +
  • When a Class is based on another Class
  • +
  • Re-use/Factor implementation/code
  • +
+
+
+
+
+
+ +
+ +
+ +
+ +
class DaughterClass : public MyClass {
+public:
+  DaughterClass();          // Constructor
+  virtual ~DaughterClass(); // Destructor
+};
+
+ + +
+ +
+ +
+
+ +
+
    +
  • If you need to call non default constructor of the mother class
  • +
+ +
+
+ +
+ +
+ +
+ +
class DaughterClass : public MyClass {
+public:
+  DaughterClass(int a);
+};
+
+DaughterClass::DaughterClass(int a) : MyClass(a) {
+  // some code
+}
+
+ + +
+ +
+ +
+
+
+
+ +
+

Polymorphism

Set of mechanisms allowing program methods/functions in several ways

+
    +
  • Overloading (you know it!)
  • +
  • Arguments passed as of mother class type
  • +
+ +
+
+
+
+ +
+

Let us consider the following code

+ +
+
+ +
+ +
+ +
+ +
class A {
+public:
+  double value;
+};
+
+class B : public A {};
+
+double foo(A &a) { return a.value; }
+
+int main() {
+  B b;
+  // C++ is strongly typed
+  // can I do that ?
+  foo(b);
+}
+
+ + +
+ +
+ +
+
+
+
+ +
+

Virtual Member

    +
  • extends polymorphism to methods
  • +
+ +
+
+ +
+ +
+ +
+ +
class A {
+public:
+  virtual double getValue() { return 0.; };
+};
+
+class B : public A {
+
+public:
+  double getValue() override { return value; };
+
+private:
+  double value;
+};
+
+double foo(A &a) { return a.getValue(); }
+
+ + +
+ +
+ +
+
+
+
+ +
+

Pure virtual: abstract classes

+
+
+ +
+ +
+ +
+ +
class A {
+public:
+  virtual double getValue() = 0;
+};
+
+class B : public A {
+
+public:
+  virtual double getValue() override { return value; };
+
+private:
+  double value;
+};
+
+double foo(A &a) { return a.getValue(); }
+
+ + +
+ +
+ +
+
+
+
+ +
+

Calling a parent class method

+
+
+ +
+ +
+ +
+ +
class A {
+
+public:
+  virtual double getValue() { return 3.; };
+};
+
+class B : public A {
+
+public:
+  double getValue() override { return A::getValue() + 2.; };
+};
+
+ + +
+ +
+ +
+
+
+
+ +
+

Dynamic cast

    +
  • Allows to determine if an object is really of a given type during program execution
  • +
+ +
+
+ +
+ +
+ +
+ +
ClassName *ptr = dynamic_cast<DaughterClass *>(&object);
+
+if (ptr == NULL)
+  std::cout << "Houston we have a problem";
+
+ + +
+ +
+ +
+
+ +
+
    +
  • same by reference type (generates an exception if failing to cast the object)
  • +
+ +
+
+ +
+ +
+ +
+ +
DaughterClass &ref = dynamic_cast<DaughterClass &>(object);
+
+ + +
+ +
+ +
+
+
+
+ +
+

Static cast

    +
  • Allows to force the cast a compilation time (can be dangerous)
  • +
+ +
+
+ +
+ +
+ +
+ +
ClassName &ref = static_cast<DaughterClass &>(object);
+
+ + +
+ +
+ +
+
+
+
+ +
+

Operators

    +
  • It is possible to use operators with user defined classes
  • +
+ +
+
+ +
+ +
+ +
+ +
// inside the class
+class ClassName {
+  ret1 operator+(ClassName &b);
+};
+
+ret1 ClassName::operator+(ClassName &b) {
+  ///...
+}
+
+ + +
+ +
+ +
+
+ +
+ +
+ +
+ +
// or outside of the class
+ret1 operator+(ClassName &ref1, ClassName &ref2){
+    ///...
+};
+
+ + +
+ +
+ +
+
+
+
+ +
+

Input/Output to std::stream

+
+
+ +
+ +
+ +
+ +
class MyClass {
+
+public:
+  int my_member;
+};
+
+/// standard output stream operator
+inline std::ostream &operator<<(std::ostream &stream, MyClass &_this) {
+  stream << _this.my_member;
+  return stream;
+}
+
+ + +
+ +
+ +
+
+
+
+ +
+

Exceptions

    +
  • Can catch an exception
  • +
+ +
+
+ +
+ +
+ +
+ +
  std::vector<int> myvector(10);
+  try {
+    // vector::at throws an out-of-range
+    myvector.at(20) = 100;
+  } catch (const std::out_of_range &oor) {
+    std::cerr << "Out of Range error: ";
+    std::cerr << oor.what() << std::endl;
+  }
+
+ + +
+ +
+ +
+
+
+
+ +
+ +
+ +
+ +
#include <stdexcept>
+
+class MyError : public std::exception {
+public:
+  MyError(int value) {
+    // set the internal error code
+    this->parameter = value;
+  }
+
+  const char *what() const noexcept override {
+    // construct the string message
+    std::stringstream sstr;
+    sstr << "Error Code: " << parameter;
+    return sstr.str().c_str();
+  }
+  int parameter;
+};
+
+ + +
+ +
+ +
+
+
+
+ +
+ +
+ +
+ +
  // usage in context
+  try {
+    throw MyError(-1);
+  } catch (MyError &e) {
+    std::cerr << e.what() << std::endl;
+  }
+
+ + +
+ +
+ +
+
+
+
+ +
+

Interactive session on making first class usage in C++

+
+
+ +
+ +
+
+
+
+
+ + + + + + +