






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A program is a precise sequence of steps to solve a particular problem. This course includes basic programming structure like loops, operator, memory allocation, reference, pointers etc. It teaches how to be a good programmer. This lecture handout is about: Class, Templates, Nontype, Parameters, Static, Members, Functions, Standard, Library, Stack, User, defined, Float, Compiler, Arrays
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Deitel & Deitel - C++ How to Program Chapter. 12, 20
12.4, 12.5, 12.7, 12.8, 20.
As discussed earlier, template functions are utilized while writing functions for generic data type. We take benefit of templates in case of writing the same function repeatedly. In this case, the writing code seems very similar, in fact identical. But the data type is changed for different versions. So we write a function for generic data type whose syntax we have used as under
template
Here T is a generic data type. Now in the function we write T while dealing with a data type. This becomes a generic template of the function. During the process of using this template, this function with a particular data type is called. The compiler automatically detects the type of the data passed (say we passed an int) and generates a new copy of the function with int. Here T is written in the original template. The copy is compiled to the object code while existing in the program. The same thing applies to other data types, used for the same function. Thus, we create a family of functions with a single template. The functionality of these functions is the same but with different data types. For example, if we want to find the square of a number, a template square will be written first. It will be called with int, double or float. Otherwise, we have to write the over loaded versions of the square function for different data types. So template functions are of good use for the programmers. They promote code reuse. The major advantage of their use is that once we write correct code with correct logic for a template function, there will be no need to re-write it.
How can we test a template? We write the template in reverse technique at the moment. When it is known what the template has to do, we take a data type for example int. A complete function for int is written and tested. After the ascertainment of the accuracy of function’s working, the int in the function is replaced with T and declared a template by writing template
Class Templates
Creation of a data type of our own with the same behavior for int , float and double etc, is the case of defining a complete interface and implementation in a generic fashion. To further understand this concept, let’s talk about a data structure called stack. We will now try to understand how does stack work, what its properties are and can we make it generic. You might have seen the plates, kept together in a orderly manner i.e. one on the other. This is a stack. Now if someone wants to add a plate on the pile, he will have to put it on the top of the stack. So, there is only one way to add something on the stack. Similarly, if we want to pick a plate from the pile, it will be taken from the upper- most tier. Thus the property of the stack is that the last placed thing will be picked first. This phenomenon is called ‘Last-in, first out’ or LIFO. In programming, we can understand what thing we want to add, the required thing is added to the top of the stack. When we pick up a thing from it, the last placed item is picked first. Following this rule of stack (last in first out), we can make a stack of integers, floats and doubles etc. Here, the stack is a class with a defined behavior, interface and the data, it holds. Now we say that the data held by the class is variable to help make a stack of integers, floats or doubles. Thus, stack is a good candidate for a template class. It means that when we instantiate the class for creating objects, a stack of integers or floats is required. The behavior of the compiler in template classes is the same as in template functions. If we want to instantiate a template class with a new data type, the compiler will generate a new version of the class with the specific data type at the place of T in the template class. We know that a class is a user-defined data type. With the help of a template class, we make another class of the user defined data type. In other words, things are not restricted to creating copies of class only for native data type. Copies of class of our own data type can also be created. It is a case of a real extensibility. Let’s see the syntax of this generic template class. It is similar to the simple template function in which we write template
template
docsity.com
Compiler does it automatically. We will not see the code of this class written for int. The compiler generates a copy of the class for int and uses it. Similarly, if we write
Number
Here y will be an object with the data member of type double. Again, the entire copy of the class will be created with double everywhere instead of T. The program will compile and run. So it is quiet useful and a big shortcut.
Class Templates and Nontype Parameters
There is a little variation, which is we can also use non-type parameters in templates. What do non-type parameters mean? We have been writing template
Templates and Static Members
Now let’s talk about the implications of the template classes. To understand the behavior of templates with static members, we have to comprehend the concept of static member variables. Static variable members are used to define ordinary classes. The static variable has a single copy for the whole class. So there are not separate copies of the static data variable for each object like ordinary data members. Now let’s see what happens when a static member is a part of a template class. The instantiation of the class has two parts i.e. one is creating an object while the other is the type of the object. For example, from the previous class Number , we write
Number
Here x is an object of generic class Number with a specific type int. We can also use float or double instead of int. We suppose, there is a static variable in the Number class. On instantiating the class with int , there will be a copy of static variable for int set-off objects. If we instantiate the class for float , there is going to be a copy of the static member for float numbers. So the member is static (i.e. there is one copy) for that type of the objects. There will be one static value for different object created with type int while another static value for different objects created for type double. So, this static value is not class wide. It is something of specific nature. In simple words, the compiler reads the code of program (main or other function) and generates a copy of the template class accordingly. It also gives a name of its own to this copy. Thus in
docsity.com
a way, the compiler generates a new unique class, replacing T with int (or any other data type we want). The static member of this unique class behaves exactly like the ordinary class. Similarly the compiler generates a copy for double with a unique name. Here the static member of this copy will affect the objects created for double data type. The static variables are instantiated once for each type whenever we instantiate a class while replacing generic data type with a specific data type.
It is pertinent to note that we can replace the generic data type with our own data type. This is slightly tricky. Suppose, we have written a class, ‘ Person’. There is also a generic class Array, which can be instantiated with int , float or double data type that means it may be an array of integers, floats and doubles respectively. Can we do so with an array of persons? If we have defined a class called Person , there may be an array of Person. Person now behaves like another data type. At the moment, it does not matter whether the data type is user defined or not. We have to be careful that when we are using our own object i.e. our own class in a template, it must support the functions and interfaces, needed for this generic structure of the class. So don’t put in something that cannot be used by this generic structure. We have discussed an example of phoneCall where reverse returns x by converting it to –x. In that example, we had to define the minus (-) operator for phone call. Similarly, in that example, billCode is changed to ‘c’. If number is passed, the negative number will be returned. Its behavior was changed in phoneCall. So we have to take care of these things. Whenever we use a template class and instantiate it for one of our own classes, it is necessary to have compatible function calls in it. It means that member functions behave properly as per requirements.
Templates and Friend Functions
Now we will have a look on another concept i.e. friend functions. We have read in classes that a programmer can declare functions as friend of the class. Let’s first look at the need of friend functions. Suppose we have defined an operator for our class, say
docsity.com
and then write class Stack
In the class definition, An integer variable called size is declared for the size of the array. Then we declare the array and write its data type as T, which is a generic type .It will be replaced by int , float , double or char when we will use this array. We can use the dynamic memory allocation for the array. But we use a fixed size array for the sake of simplicity. To declare an array, we need a constant value. Normally, this constant value is not written in the class definition. It will go to the constructor and be required when the constructor will be called for an object. We can use the constructor to actually define the array for us. We need some utility functions. The function push() is used to push an element on the stack. We use the function pop() to get an element from the stack. The push() and pop() functions put and get things of type T. So pop() should return something of type T. That means it will return int if int is pushed and returns double if double is pushed and so on. So we need push() and pop() which are parameterized with T. After this, there is need of some functions for generic manipulation like if stack is full or if stack is empty. We can write function isempty() that returns a Boolean. If it returns TRUE, the stack will be empty. However, presence of something in the stack will turn it FALSE. Similarly we can write a utility function isfull() to check whether the stack is full. We cannot store elements more than that size in the stack. The isfull() returns TRUE, if the stack is full. The code of the class definition is very simple. We write T wherever we need a generic data type. It can be written as under.
template
In the definition of the functions of the class, we again use
docsity.com
Sample Program
Here is a sample program that demonstrates the use of template class.
/* This program defines a template class and shows its use for different data types. There is also the use of template function. It also overloads the << operator. */ #include<iostream.h>
template
//generic constructor template
template
class Employee { private: int idNum; double salary; public: Employee(int id); friend ostream& operator <<(ostream& out, const Employee &e); };
Employee::Employee(int id=0) { idNum=id; salary=4.9; }
ostream& operator<<(ostream &out, const Employee &emp)
docsity.com
and other design constraints. A common mistake is the angle bracket problem.
Standard Template Library (STL)
Templates are a major code reuse feature. History of C++ language reveals that the template feature was introduced later, relative to other features. But it is a very important feature. We will realize that it makes a lot more sense to keep total code base very small and very concise. It also helps ensure that the same tested code is used everywhere. We had earlier referred to this concept while writing classes. We separated the interface and implementation and sealed the implementation after testing it. Afterwards, we created different objects of the class and every object knew its behavior. Thus there was an abstraction of details. The template functions and template classes go one-step even further. With templates, we can perform different tasks while using one base code. Objects of different types staying with one particular framework can be instantiated. This framework (template) is so important that a couple of researchers actually sat down and started looking at that in programming we often are using the one concept which applies to so many things that we should templatise it. For example, with the help of arrays, we do different manipulations like, ‘next element’, go to the end of the array, add something at the end etc. Now suppose that the size of array is 100. We want to add the 101st element in the array. We can do it by copying the same array in a new big array and adding the element to that array. Thus we have solutions for different problems, but these are the things of very common use. Their every day use is so important that two researchers wrote a whole library of common use functions. This library is a part of the official standard of C++. It is called STL i.e. Standard Template Library. As a library, it is a tested code base. Some one has written, tested and compiled for the ultimate use of programmers. We can use these templates and can implement different concepts for our own data types. Equally is true about the use of the array data type. Our code will become very small with the use of this tested facility. Similarly, there is no bug or error in it. Thus, if we have a tested and tried code base, we should try our best to write programs by using it. STL is a lot of important code, pre-developed for us. It is available as a library. We can write programs by using it. Thus our programs will be small and error free.
docsity.com