Rozdíly

Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.

Odkaz na výstup diff

public:quiz [2018/02/24 19:10] (aktuální)
Řádek 1: Řádek 1:
 +====== Sada quizů ======
  
 +===== Polymorfismus - Quiz 1 =====
 +
 +<code cpp quiz1.cpp>​
 +#include <​iostream>​
 +
 +class base
 +{
 +public:
 +    virtual ~base() {}
 +
 +    virtual void foo() const
 +    {
 +        std::cout << "​A'​s foo!" << std::endl;
 +    }
 +};
 +
 +class derived : public base
 +{
 +public:
 +    virtual void foo() const
 +    {
 +        std::cout << "​B'​s foo!" << std::endl;
 +    }
 +};
 +
 +void call_foo_base(base const* p)
 +{
 +    p->​foo();​
 +}
 +
 +void call_foo_derived(derived const* p)
 +{
 +    p->​foo();​
 +}
 +
 +int main()
 +{
 +    base obj_base;
 +    derived obj_derived;​
 +
 +    obj_base.foo();​
 +    obj_derived.foo();​
 +
 +    call_foo_base(&​obj_base);​
 +    call_foo_base(&​obj_derived);​
 +
 +    call_foo_derived(&​obj_base);​
 +    call_foo_derived(&​obj_derived);​
 +}
 +</​code>​
 +
 +===== Polymorfismus - Quiz 2 =====
 +
 +<code cpp quiz2.cpp>​
 +#include <​iostream>​
 +
 +class base
 +{
 +public:
 +    virtual ~base() {}
 +
 +    void foo() const
 +    {
 +        std::cout << "base foo" << std::endl;
 +    }
 +};
 +
 +class derived : public base
 +{
 +private:
 +    void foo() const
 +    {
 +        std::cout << "​derived foo" << std::endl;
 +    }
 +};
 +
 +void call_foo(base const* p)
 +{
 +    p->​foo();​
 +}
 +
 +int main()
 +{
 +    base obj_base;
 +    derived obj_derived;​
 +
 +    obj_base.foo();​
 +    obj_derived.foo();​
 +
 +    call_foo(&​obj_base);​
 +    call_foo(&​obj_derived);​
 +}
 +</​code>​
 +
 +===== Polymorfismus - Quiz 3 =====
 +
 +<code cpp quiz3.cpp>​
 +#include <​iostream>​
 +
 +int square(int x)
 +{
 +    std::cout << "int square(int x)" << std::endl;
 +    return x * x;
 +}
 +
 +int square(double x)
 +{
 +    std::cout << "int square(double x)" << std::endl;
 +    return x * x;
 +}
 +
 +double square(double x)
 +{
 +    std::cout << "​double square(double x)" << std::endl;
 +    return x * x;
 +}
 +
 +int main()
 +{
 +    int a = square(5);
 +    int b = square(5.0f);​
 +    double c = square(5.0f);​
 +
 +    std::cout << a << " " << b << " " << c << std::endl;
 +}
 +</​code>​
 +
 +===== Polymorfismus - Quiz 4 =====
 +
 +<code cpp quiz4.cpp>​
 +#include <​iostream>​
 +
 +class base
 +{
 +public:
 +    virtual ~base() {}
 +
 +    virtual void functionality() const
 +    {
 +        std::cout << "base functionality"​ << std::endl;
 +    }
 +};
 +
 +class derived : public base
 +{
 +public:
 +    virtual void functionality()
 +    {
 +        std::cout << "​derived functionality"​ << std::endl;
 +    }
 +};
 +
 +void call_functionality(base* obj)
 +{
 +    obj->​functionality();​
 +}
 +
 +int main()
 +{
 +    base obj_base;
 +    derived obj_derived;​
 +
 +    obj_base.functionality();​
 +    obj_derived.functionality();​
 +
 +    call_functionality(&​obj_base);​
 +    call_functionality(&​obj_derived);​
 +}
 +</​code>​
 +
 +===== Polymorfismus - Quiz 5 =====
 +
 +<code cpp quiz5.cpp>​
 +#include <​iostream>​
 +
 +class base
 +{
 +public:
 +    virtual ~base() {}
 +
 +    void functionality_wrapper()
 +    {
 +        functionality();​
 +    }
 +
 +protected:
 +    virtual void functionality() const
 +    {
 +        std::cout << "base functionality"​ << std::endl;
 +    }
 +};
 +
 +class derived : public base
 +{
 +public:
 +    virtual ~derived() {}
 +
 +protected:
 +    virtual void functionality()
 +    {
 +        std::cout << "​derived functionality"​ << std::endl;
 +    }
 +};
 +
 +void call_functionality_wrapper(base* obj)
 +{
 +    obj->​functionality_wrapper();​
 +}
 +
 +int main()
 +{
 +    base obj_base;
 +    derived obj_derived;​
 +
 +    obj_base.functionality_wrapper();​
 +    obj_derived.functionality_wrapper();​
 +
 +    call_functionality_wrapper(&​obj_base);​
 +    call_functionality_wrapper(&​obj_derived);​
 +}
 +</​code>​
QR Code
QR Code public:quiz (generated for current page)