主题目录

  • 本主题

    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)

    The architecture of STL is largely the creation of Alexander Stepanov. In 1979 he began working out his initial ideas of generic programming and exploring their potential for revolutionizing software development. Although David Musser had developed and advocated some aspects of generic programming already by 1971, it was limited to a rather specialized area of software development (computer algebra).

    Stepanov recognized the full potential for generic programming and persuaded his then-colleagues at General Electric Research and Development (including, primarily, David Musser and Deepak Kapur) that generic programming should be pursued as a comprehensive basis for software development. At the time there was no real support in any programming language for generic programming.

    The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms and iterators. It is a generalized library and so, its components are parameterized. A working knowledge of template classes is a prerequisite  for working with STL.

    STL has four main components

    • Algorithms
    • Containers
    • Functions
    • Iterators