Commit d4e88d176a37ae4d4a38b25a674d754e347ef360

Authored by Grzegorz Jabłoński
1 parent 737eb743

Added some lecture 10 examples

examples10/link.cpp 0 → 100644
  1 +#include <iostream>
  2 +using namespace std;
  3 +
  4 +class Link
  5 +{
  6 +public:
  7 + Link* next;
  8 + virtual void Print() = 0;
  9 +};
  10 +
  11 +class Displayed : public Link
  12 +{
  13 +string name;
  14 +public:
  15 + Displayed(string n): name(n) {};
  16 + void Print() override { cout << name << endl; }
  17 +;
  18 +};
  19 +
  20 +class Task : public Link
  21 +{
  22 +string name;
  23 +public:
  24 + Task(string n): name(n) {};
  25 + void Print() override { cout << name << endl; }
  26 +;
  27 +};
  28 +
  29 +
  30 +class Satellite: public Task, public Displayed
  31 +{
  32 + public:
  33 + Satellite(string n): Task("Task:" + n), Displayed("Displayed:" + n) {};
  34 + void Print() override { Task::Print(); Displayed::Print();};
  35 +};
  36 +
  37 +Link* insert(Link* list, Link* node)
  38 +{
  39 + node->next = list;
  40 + return node;
  41 +}
  42 +
  43 +
  44 +int main()
  45 +{
  46 +Link* displayedList = NULL;
  47 +displayedList = insert(displayedList, new Displayed("a"));
  48 +displayedList = insert(displayedList, new Displayed("b"));
  49 +
  50 +Satellite* s = new Satellite("e");
  51 +Displayed* sd = s;
  52 +
  53 +displayedList = insert(displayedList, sd);
  54 +
  55 +Link* taskList = NULL;
  56 +taskList = insert(taskList, new Task("c"));
  57 +taskList = insert(taskList, new Task("d"));
  58 +
  59 +Task* st = s;
  60 +taskList = insert(taskList, st);
  61 +
  62 +Link* iterator = displayedList;
  63 +
  64 +while(iterator)
  65 +{
  66 + iterator->Print();
  67 + iterator=iterator->next;
  68 +}
  69 +
  70 +iterator = taskList;
  71 +
  72 +while(iterator)
  73 +{
  74 + iterator->Print();
  75 + iterator=iterator->next;
  76 +}
  77 +
  78 +
  79 +}
  80 +
  81 +
  82 +
... ...
examples10/privinh.cpp 0 → 100644
  1 +class A
  2 +{
  3 + public:
  4 + int a;
  5 +};
  6 +
  7 +class B: public A
  8 +{
  9 + public:
  10 + int b;
  11 +};
  12 +
  13 +class C: private A
  14 +{
  15 + public:
  16 + int c;
  17 + void fun()
  18 + {
  19 + a = 15;
  20 + };
  21 +};
  22 +
  23 +int main()
  24 +{
  25 + A a;
  26 + B b;
  27 + C c;
  28 +
  29 + a.a = 17;
  30 + b.a = 17;
  31 + //c.a = 17;
  32 +
  33 + A* aptr = &b;
  34 + A* aptr2 = &c;
  35 + aptr2->a = 17;
  36 +
  37 +}
... ...
examples10/ptrconv.cpp 0 → 100644
  1 +class A {
  2 + int a;
  3 +};
  4 +
  5 +class B {
  6 + int b;
  7 +};
  8 +
  9 +class C : public A, public B
  10 +{
  11 + int c;
  12 +};
  13 +
  14 +int main()
  15 +{
  16 + C c;
  17 +
  18 + A* aptr = &c;
  19 + B* bptr = &c;
  20 +}
  21 +
... ...
examples10/ptrconv2.cpp 0 → 100644
  1 +class A {
  2 +public:
  3 + int a;
  4 +};
  5 +
  6 +class B : virtual public A {
  7 + int b;
  8 +};
  9 +
  10 +class C : virtual public A {
  11 + int c;
  12 +};
  13 +
  14 +class D: public B, public C
  15 +{
  16 + int d;
  17 +};
  18 +
  19 +class E: public C, public B
  20 +{
  21 + int e;
  22 +};
  23 +
  24 +int main()
  25 +{
  26 + D d;
  27 + E e;
  28 +
  29 + C* cptr1 = &d;
  30 + C* cptr2 = &e;
  31 +
  32 + cptr1->a = 1;
  33 + cptr2->a = 2;
  34 +
  35 +}
  36 +
... ...
examples10/storable.cpp 0 → 100644
  1 +#include <iostream>
  2 +
  3 +class Storable {
  4 +public:
  5 +Storable() {};
  6 +virtual void write() {};
  7 +virtual ~Storable() {write() ; } // to be called
  8 +// from overriding destructors
  9 +};
  10 +
  11 +class Data : public Storable
  12 +{
  13 + public:
  14 + int* data;
  15 + Data() { data = new int(32); };
  16 + ~Data() { delete data; };
  17 + void write() override { std::cout << *(this->data) << std::endl;};
  18 +};
  19 +
  20 +int main()
  21 +{
  22 + Data d;
  23 + Storable* ptr = &d;
  24 + ptr->write();
  25 +}
... ...