Introduction into Object-Oriented Programming with C++
A prerequesite for this tutorial is the knowledge of the Object Related Programming. This tutorial should introduce virtual functions, pure virtual functions and derived classes. We create an interface for C++ STL containers. As this tutorial should introduce the object oriented paradigm we will not implement the member functions for the STL containers. Only placeholders are used in order to keep the focus on the paradigm and not on the algorithm implementations.
Ingredients
In this lesson the following data are required:
![]() | source code demo file: main.cpp
|
![]() | source code class demo header file: container.hh
|
![]() | source code class demo file: container.cpp |
Tasks
- Open the three demofiles and read them. Keep focus on the container.hh file especially on the class declaractions. Compile and run the program to see how it works.
- Implement a public virtual function virtual void pushFront() within the base class and declare a normal public function void pushFront() within the derived class.
- What happens if you do not declare the pushFront() function, within the base class, virtual ? Why ?
- For the next part we need a new structuring of the class hierarchy. So comment out the class declaractions within your container.hh and declare the following classes:
class container
class vec cont : public container
class list cont : public container
- Implement the same public functions for both derived classes:
void pushFront();
void printContainer();
- Implement two pure virtual functions within the base class by the following:
virtual void pushFront() = 0;
virtual void printContainer() = 0;
- Access both functions of the derived classes by dynamic binding.
Hints
- A derived class provides accessability to the own members and the members of the base class. This derivative is being called inheritance. Furthermore we can add functions or variables to the derived class. Due to this fact we can describe a derviced class as an extension to the base class. We will structure the classes in that way if we should implement two ob jects where both ob jects offer a core of functions, which are the same for both classes, but furthermore they provide different functions in relation to the other objects.
- Polymorphism is briefly described as one interface, many implementations. Due to the derived classes we are able to group ob jects into families which all share a core of properties. As polymorphism offers an interface for such a family we can work with all ob jects by just interacting with the interface. This type of polymorphism is being called subtyping polymorphism.
- keyword - virtual: This keyword converts the declared function virtual. The functionality of a virtual function within the base class can be overwritten by the derived class. e.g. two equally namned functions where each functionbody is different to the others. In own words: Two equally named funtions which actually do different things. This can only be realized by dynamic binding.
- Dynamic binding refers to the mechanism that resolves a virtual function call at runtime. Therefore we declare a pointer on the base class. We are able to instantiate an ob ject any time by the new operator. The -> operator allows us to dynamically access the memberfunctions. Dynamically means that the program chooses on runtime for which class the called memberfunction belongs to.
container *dynCont; // pointer of type base class container
dynCont = new vec_cont(); // memory is being allocated
dynCont->pushBack(); // the program
