Polymorphism

Fundamentals on Computing for Robotics, Graphics and Computer Vision

Darío Suárez - Adolfo Muñoz

Polymorphism

The ability to have a function/method to do different thinkgs depending on the involved data types

Polymorphism

  • Overloading: several functions with same name.
  • Generics: leaving data type as parameter
  • Inheritance: a class representing its subclasses.

Advantages of polymorphism

  • Reusability of code
  • Reduction of coupling among functionalities
  • Further abstraction

Overloading

Two or more functions with the same name but with different types and number of arguments

Overloading

Generics

Programming feature through which algorithms and data types are written in terms of types (parameters) to be specified later

Generics

Two stages:

  • Specification: define the algorithm/data type leaving one or more types as parameters
  • Instantiation: choose the types for those parameters upon usage

Generic specification

Generic specification

In C++, it needs to be completely on a header file.

Generic Instantiation

Generic algorithms

  • Algorithms can also be generic.
  • If possible, types are automatically deduced.

Generic algorithms

Generics

There are many generic data types in C++:

  • std::vector<T>
  • std::list<T>
  • std::unique_ptr<T>
  • std::shared_ptr<T>
  • ...

Inheritance

Relationship between two classes that enables one of them (superclass) to represent another (subclass)

Inheritance


                    class SuperC {
                        int superattribute;
                        void supermethod();
                        SuperC(int a) {
                            superattribute = a;
                        } 
                    }; 
                    

                    class SubC : public SuperC {
                        int subattribute;
                        void submethod();
                        SubC(int a, int b) :
                            SuperC(a) {
                            subattribute=b;
                        } 
                    }; 
                    

SubC inherits superattribute and supermethod()

Visibility

  • private: Cannot access except from within the class
  • public: Can access from everywhere

Visibility

  • private: Cannot access except from within the class
  • protected: Can access from within the class and its subclasses
  • public: Can access from everywhere

Inheritance polymorphism

A pointer / reference to a superclass can represent any of its subclasses


            void functionc(SuperC* c) {
                ...
            }
            int main() {
                SuperC superc;
                SubC subc;
                functionc(&superc); 
                functionc(&subc); //This is also legal
            } 
            

functionc works with both types

Inheritance polymorphism


                    class SuperC {
                     virtual void polymethod();
                    }; 
                    

                    class SubC : public SuperC {
                     void polymethod() override;
                    }; 
                    

            void functionc(SuperC* c) {
                c->polymethod();
            } 
        

functionc calls either SuperC::polymethod or SubC::polymethod

Inheritance polymorphism


                    class SuperC {
                     virtual void polymethod() = 0;
                    }; 
                    

                    void functionc(SuperC* c) {
                     c->polymethod();
                    } 
                    

                    class SubC1 : public SuperC {
                     void polymethod() override;
                    }; 
                    

                    class SubC2 : public SuperC {
                     void polymethod() override;
                    }; 
                    

SuperC::polymethod is not implemented.
functionc calls either SubC1::polymethod or SubC2::polymethod

Thinking with inheritance

  • Superclasses represent generalizations of their subclasses
  • Subclasses represent particular cases of superclasses
  • Without polymorphism there is no need for inheritance

Example

shape.h
fence.h
fence.cpp

Example

circle.h
circle.cpp
rectangle.h
rectangle.cpp

Example

main.cpp