Error message

  • Deprecated function: Return type of DatabaseStatementBase::execute($args = [], $options = []) should either be compatible with PDOStatement::execute(?array $params = null): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2244 of /home2/psicolog/public_html/feliponcho/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /home2/psicolog/public_html/feliponcho/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /home2/psicolog/public_html/feliponcho/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /home2/psicolog/public_html/feliponcho/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /home2/psicolog/public_html/feliponcho/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /home2/psicolog/public_html/feliponcho/includes/database/database.inc).
  • Deprecated function: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in drupal_random_bytes() (line 2268 of /home2/psicolog/public_html/feliponcho/includes/bootstrap.inc).
  • Deprecated function: rtrim(): Passing null to parameter #1 ($string) of type string is deprecated in url() (line 2349 of /home2/psicolog/public_html/feliponcho/includes/common.inc).
  • Deprecated function: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in url_is_external() (line 2393 of /home2/psicolog/public_html/feliponcho/includes/common.inc).
  • Deprecated function: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in url_is_external() (line 2395 of /home2/psicolog/public_html/feliponcho/includes/common.inc).
  • Deprecated function: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in url() (line 2311 of /home2/psicolog/public_html/feliponcho/includes/common.inc).

Polymorphism and Virtualization

Index



Multipath Inheritance Ambiguity

  • Let's suppose the case of the class 'Child' that inherits directly from two different classes 'Parent1' and 'Parent2'. And 'Parent'1 and 'Parent2' in turn inherit from the same class 'Grandparent'. It is said that 'Grandparent' class is indirect base class of 'Child'. This pose an ambiguity problem, because all the private and public members of 'Grandparent' are inherited to 'Child' twice!.
  • Ambiguity is avoided by declaring 'Grandparent' (the common 'ancestor' class) as a virtual class.




    Run Time vs. Compile Time Polymorphism

  • Compile Time Polymorphism is achieved by overloading functions and operators.
  • Run Time Polymorphism is achieved by using inheritance and virtual functions.
  • A virtual function is a member function of a class defined with the keyword virtual and its functionality can be over-rided in that class derived classes.
  • Virtual functions are used to give different meanings to a function, and this is part of the concept of polymorphism (different meanings = many (poly) forms (morfism) )
  • If a function is designated virtual in the base class then the derived class's function would be called (if it exists). If it is not virtual, the base class's function would be called.






    Dynamic Binding vs. Static Binding

  • Virtual Functions are resolved during run-time or dynamic binding. The main difference between non-virtual and virtual is in the way they are resolved:
    • Non-virtual are resolved during compile time = static binding
    • Virtual are resolved during run-time = dynamic binding = late binding.




    Why do we need Virtual Functions?

  • It's because sometimes derived classes would do something not exactly in the same way their parents did.
  • For example, if we have the virtual function make in the base class Vehicle, derived classes FourWheeler and Sedan would make a car in different ways: FourWheeler will use all-terrain wheels and special 4x4 transmission and Sedan will use automatic transmission and normal tires.
  • If we create a dynamic list of generic Vehicles we may add to this list both FourWheeler and Sedan vehicles to that list, and when we want to make all the items on that dynamic list, we should only need to walk it calling the member make for each object in the list, and "automatically" each Vehicle in the list will execute its own make version, so Fourwheelers will have 4x4 transmissions and Sedans will have automatic transmissions.



    Code Example

    #include<iostream>
    using namespace std;
    
    // Base class for C++ virtual function example
    class Window
    {
    public:
        virtual void Create() // virtual function for C++ virtual function example
        {
            cout << "Base class Window" << endl;
        }
    };
    
    class CommandButton : public Window
    {
    public:
        void Create()
        {
            cout << "Derived class Command Button - Overridden C++ virtual function" << endl;
        }
    };
    
    void main()
    {
        Window *x, *y;
        x = new Window();
        x->Create();
    
        y = new CommandButton();
        y->Create();
    }
    
  • The output will be:

    Base class Window
    Derived class Command Button - Overridden C++ virtual function