Topic outline
- General
- Topic 1: Polymorphism This topic
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.
In the attached files you will find all the notes required and detailed explanations about the topics presented in this session. Please do go through them carefully and we can discuss any questions during the interactive session
Exercises:
There will be three types of exercises below.
Group 1 exercises will aim at theory revision and should be assigned to all so we can discuss the answers over the seminars
Group 2 exercises should be assigned to small groups and we will expect a short presentation over the seminar (not more than 5 minutes) explaining or solving the problem.
Group 3 exercises are programming exercises. You are expected to work in small groups to solve the programming problems.
Theory Revision - These exercises are aiming to revise the theory presented
Presentation Exercises - You should in teams create a small presentation about one of the questions in this group
Programming Exercises - These exercises are used to apply the concepts learned in this session. Please do attempt them in small groups if possible.
- Topic 2: Template coding
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.
In the attached files you will find all the notes required and detailed explanations about the topics presented in this session. Please do go through them carefully and we can discuss any questions during the interactive session
Exercises:
There will be three types of exercises below.
Group 1 exercises will aim at theory revision and should be assigned to all so we can discuss the answers over the seminars
Group 2 exercises should be assigned to small groups and we will expect a short presentation over the seminar (not more than 5 minutes) explaining or solving the problem.
Group 3 exercises are programming exercises. You are expected to work in small groups to solve the programming problems.
Theory Revision - These exercises are aiming to revise the theory presented
Presentation Exercises - You should in teams create a small presentation about one of the questions in this group
Programming Exercises - These exercises are used to apply the concepts learned in this session. Please do attempt them in small groups if possible.
- C++ requires variables, functions, classes etc with specific data types. However, many algorithms) have almost the same code but with different data types.
- Topic 3: Standard Template Library (STL)
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
In the attached files you will find all the notes required and detailed explanations about the topics presented in this session. Please do go through them carefully and we can discuss any questions during the interactive session
Exercises:
There will be three types of exercises below.
Group 1 exercises will aim at theory revision and should be assigned to all so we can discuss the answers over the seminars
Group 2 exercises should be assigned to small groups and we will expect a short presentation over the seminar (not more than 5 minutes) explaining or solving the problem.
Group 3 exercises are programming exercises. You are expected to work in small groups to solve the programming problems.
Presentation Exercises - You should in teams create a small presentation about one of the questions in this group
Programming Exercises - These exercises are used to apply the concepts learned in this session. Please do attempt them in small groups if possible.
- Algorithms