Commit 507b04f8 authored by Šmelko Adam Mgr.'s avatar Šmelko Adam Mgr.
Browse files

cv11

parent 60cd82b9
......@@ -33,6 +33,7 @@ Deadliny:
| 25.11.2021 | Šablony | [CV8](https://gitlab.mff.cuni.cz/teaching/nprg041/Smelko/cpp-cvika/-/blob/master/prezentacie/Cv8.pdf) |
| 02.12.2021 | Gumové pole, 2. DÚ | [CV9](https://gitlab.mff.cuni.cz/teaching/nprg041/Smelko/cpp-cvika/-/blob/master/prezentacie/Cv9.pdf) |
| 09.12.2021 | Výjimky, streamy a manipulátory | [CV10](https://gitlab.mff.cuni.cz/teaching/nprg041/Smelko/cpp-cvika/-/blob/master/prezentacie/Cv10.pdf) |
| 16.12.2021 | Virtuální metody, double dispatch, C++20 | [CV11](https://gitlab.mff.cuni.cz/teaching/nprg041/Smelko/cpp-cvika/-/blob/master/prezentacie/Cv11.pdf) |
## Úkoly:
......
#include <iostream>
struct concrete_val_a;
struct concrete_val_b;
struct abstract_val
{
virtual ~abstract_val() = default;
virtual int compare(abstract_val* b) = 0;
virtual int compare(concrete_val_a* a) = 0;
virtual int compare(concrete_val_b* b) = 0;
};
struct concrete_val_a : abstract_val
{
virtual int compare(abstract_val* b) override
{
return b->compare(this);
}
virtual int compare(concrete_val_a* a) override
{
std::cout << "a compare a" << std::endl;
return 0;
}
virtual int compare(concrete_val_b* b) override
{
std::cout << "a compare b" << std::endl;
return 0;
}
};
struct concrete_val_b : abstract_val
{
virtual int compare(abstract_val* b) override
{
return b->compare(this);
}
virtual int compare(concrete_val_a* a) override
{
std::cout << "b compare a" << std::endl;
return 0;
}
virtual int compare(concrete_val_b* b) override
{
std::cout << "b compare b" << std::endl;
return 0;
}
};
int main()
{
abstract_val* a = new concrete_val_a();
abstract_val* b = new concrete_val_b();
a->compare(b);
}
#include <iostream>
using namespace std;
class A {
public:
virtual int f() { cout << "A::f" << endl; };
int g() { cout << "A::g" << endl; };
int h() { return f(); };
virtual int j() { return g(); };
};
class B : public A {
public:
virtual int f() { cout << "B::f" << endl; };
int g() { cout << "B::g" << endl; };
int h() { return g(); };
virtual int j() { return f(); };
};
int main()
{
A x; B y;
A * p = &x;
A * q = &y;
B * r = &y;
x.f();
x.g();
x.h();
x.j();
y.f();
y.g();
y.h();
y.j();
p->f();
p->g();
p->h();
p->j();
q->f();
q->g();
q->h();
q->j();
r->f();
r->g();
r->h();
r->j();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment