Chapter 10.1.2 Generic Programming in C++ | Introduction to Programming Using Java

Chapter 10.1.2  Generic Programming in C++ | Introduction to Programming Using Java

 

10.1.2 Generic Programming in C++

 

Unlike Smalltalk, C++ is a very strongly typed language, even more so than Java. Every variable has a type, and can only hold data values of that type. This means that the kind of generic programming that is used in Smalltalk is impossible in C++. Furthermore, C++ does not have anything corresponding to Java’s Object class. That is, there is no class that is a superclass of all other classes. This means that C++ can’t use Java’s style of generic programming with non-parameterized generic types either.

 

Chapter 10.1.2 Generic Programming in C++ | Introduction to Programming Using Java

 

Nevertheless, C++ has a powerful and flexible system of generic programming. It is made possible by a language feature known as templates. In C++, instead of writing a different sorting subroutine for each type of data, you can write a single subroutine template. The template is not a subroutine; it’s more like a factory for making subroutines. We can look at an example, since the syntax of C++ is very similar to Java’s:

Chapter 10.1.2 Generic Programming in C++ | Introduction to Programming Using Java

This piece of code defines a subroutine template. If you remove the first line, “template<class ItemType>”, and substitute the word “int” for the word “ItemType” in the rest of the template, you get a subroutine for sorting arrays of ints. (Even though it says “class ItemType”, you can actually substitute any type for ItemType, including the primitive types.) If you substitute “string” for “ItemType”, you get a subroutine for sorting arrays of strings. This is pretty much what the compiler does with the template.

 

Chapter 10.1.2 Generic Programming in C++ | Introduction to Programming Using Java

 

If your program says “sort(list,10)” where list is an array of ints, the compiler uses the template to generate a subroutine for sorting arrays of ints. If you say “sort(cards,10)” where cards is an array of objects of type Card, then the compiler generates a subroutine for sorting arrays of Cards. At least, it tries to. The template uses the “>” operator to compare values.

If this operator is defined for values of type Card, then the compiler will successfully use the template to generate a subroutine for sorting cards. If “>” is not defined for Cards, then the compiler will fail—but this will happen at compile time, not, as in Smalltalk, at run time where it would make the program crash.

 

Chapter 10.1.2 Generic Programming in C++ | Introduction to Programming Using Java

 

In addition to subroutine templates, C++ also has templates for making classes. If you write a template for a binary tree class, you can use it to generate classes for binary trees of ints, binary trees of strings, binary trees of dates, and so on—all from one template.

The most recent version of C++ comes with a large number of pre-written templates called the Standard Template Library or STL. The STL is quite complex. Many people would say that its much too complex. But it is also one of the most interesting features of C++.

 

SEE MORE:

17 thoughts on “Chapter 10.1.2 Generic Programming in C++ | Introduction to Programming Using Java”

Leave a Comment