Sada quizů

Polymorfismus - Quiz 1

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);
}

Polymorfismus - Quiz 2

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);
}

Polymorfismus - Quiz 3

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;
}

Polymorfismus - Quiz 4

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);
}

Polymorfismus - Quiz 5

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);
}
QR Code
QR Code public:quiz (generated for current page)