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 @@ + + + +
+ + + + + + + +typedef type name;
+
+// example
+typedef unsigned int UInt;
+typedef double Real;
+
using name = type;
+
+// example
+using UInt = unsigned int;
+using Real = double;
+
What is the difference with #define ?
+ +#define UInt unsigned int
+
enum ParticleType {
+ _pt_planet,
+ _pt_star,
+ _pt_atom,
+ _pt_ball)
+};
+
class MyClass {
+public:
+ // Constructor
+ MyClass();
+ // Destructor
+ virtual ~MyClass();
+
+public:
+ ret method1(/* arguments */);
+
+private:
+ type member;
+};
+
class MyClass {
+
+public:
+ int foo(int a);
+ int foo(double a = 1e3);
+
+ int member1;
+};
+
int MyClass::foo(int a) {
+ // ...
+}
+
int MyClass::foo(int value) {
+ // access through the pointer
+ this->member1 = value;
+}
+
ret1 MyClass::foo(type1 member1) {
+ // member1 = member1; ???
+ this->member1 = member1;
+}
+
ClassName object;
+ret1 a = object.method1();
+
class MyClass {
+public:
+ MyClass(int a);
+};
+
+MyClass::MyClass(int a) {
+ // some code
+}
+
+int main() {
+ // object instanciation
+ MyClass object(1);
+}
+
ClassName *ptr = new ClassName();
+ret1 a = ptr->method1();
+delete ptr;
+
#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();
+
class DaughterClass : public MyClass {
+public:
+ DaughterClass(); // Constructor
+ virtual ~DaughterClass(); // Destructor
+};
+
class DaughterClass : public MyClass {
+public:
+ DaughterClass(int a);
+};
+
+DaughterClass::DaughterClass(int a) : MyClass(a) {
+ // some code
+}
+
Set of mechanisms allowing program methods/functions in several ways
+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);
+}
+
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(); }
+
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(); }
+
class A {
+
+public:
+ virtual double getValue() { return 3.; };
+};
+
+class B : public A {
+
+public:
+ double getValue() override { return A::getValue() + 2.; };
+};
+
ClassName *ptr = dynamic_cast<DaughterClass *>(&object);
+
+if (ptr == NULL)
+ std::cout << "Houston we have a problem";
+
DaughterClass &ref = dynamic_cast<DaughterClass &>(object);
+
ClassName &ref = static_cast<DaughterClass &>(object);
+
// 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){
+ ///...
+};
+
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;
+}
+
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;
+ }
+