主题目录

  • 本主题

    Topic 1: Polymorphism

    Polymorphism

    ...“program in the general” rather than “program in the specific.”...

    The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function. 

    We write programs that process objects of classes that are part of the same class hierarchy as if they were all objects of the hierarchy’s base class. 

    Polymorphism is relying on each object to know how to “do the right thing” in response to the same function call is the key concept of polymorphism.

    The same message sent to a variety of objects has “many forms” of results—hence the term polymorphism.

    Virtual Function

    A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.

    With virtual functions, the type of the object - not the type of the handle used to invoke the object's member function - determines which version of a virtual function to invoke.

    Operator Overloading

    C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively. Operator overloading is not unique to C++.  How C++ does it is fairly unique and extremely powerful. There are two distinct ways to overload operators in C++. One is with member functions as part of a class definition and the other is a separate nonmember functions. 


  • Topic 2: Template coding

    Templates

    "Templates should be used to generate a collection of classes when the type of the objects does not affect the behaviour of the class’s functions. … Inheritance should be used with a collection of classes when the type of the objects does affect the behaviour of the class’s functions."

    Meyers (Effective C++, Item 42, p157).

    C++ templates are marvelously simple and powerful. This is the feature that supports generic programming. Generic programming refers to code that works independent of type. While C++ is a strongly typed language, there is still great benefit in being able to write functions and classes that are "type-agnostic." That is, they operate on objects without concern for the type of those objects. Because C++ supports defining your own types through classes and operator overloading, it's possible to do a great deal of generic programming in templates while leaving implementation details to the classes and operators.


    Why use Templates?

    • C++ requires variables, functions, classes etc with specific data types. However, many algorithms) have almost the same code but with different data types.
    • Re-implementing causes code duplication and can introduce errors.
    • Writing general code bypasses all type-checking.
    • Preprocessors replace text indiscriminately and can introduce errors by compiling code the programmer never even sees.

  • Topic 3: Standard Template Library (STL)

    Generic programming and the Standard Template Library (STL)