Broad Network


Template and Header in C++

Writing a C++ Application – Part 5

Forward: In this part of the series, I explain how to incorporate a template, precisely a container template, into an application.

By: Chrysanthus Date Published: 7 Feb 2013

Introduction

This is part 5 of my series, Writing a C++ Application. In this part of the series, I explain how to incorporate a template, precisely a container template, into an application. We continue and add a bit more to the application of the previous parts of this series. You should have read the previous parts of the series, as this is a continuation.

A Sample Template Container
In one of the previous series of this volume titled, Writing a C++ Container, I explained how to write a C++ container. You can reach the series from the link, C++ Course, below. In that series, I created a C++ sequence list container. The container consists of three template classes. I use the same container to explain how you incorporate a template into an application, here. A link to the template container zip file (plus the files of this tutorial) is given below.

All Template code in One file
All template code: the declarations, the synopses and the implementation (bodies of member functions) go into one file. Of course you can have different files, each with a particular template unit. We have only one template unit here, which is the sequence list container.

In the sequence list container, you have (typed) the declarations first before the synopses. Below the synopses you have the implementations. The declarations are necessary here and should come first because a synopsis in the file does not use only its own declaration for its coding; it uses the declarations of other synopses (classes).

Including the Template File
I talk here about a large application. For each file that needs the template, you just include the template file at the top of the file using the #include preprocessing directive.

In this project, all the template code is in a file called, list.cpp, in the c:\dir1 directory. In the sampleEntities.cpp file of the c:\dir1 directory and the mainFile.cpp file of the working directory, I placed the following directive at the top:

    #include "c:\dir1\list.cpp"

The templates (actually three templates in this project) are not used in the mainFile.cpp file. I have just included the file there just to show that you can include the complete template in all parallel files.

Using the Template
With the inclusion of the template file, you use the template below, in the file of interest, in the normal way. The templates in this project will be used in the sampleEntities.cpp file. So modify the fn() function in the file as follows:

    void alpha::fn()
        {
            cout << pi << "\n";
            cout << myConstStr << "\n";
            arr[0] = 25;
            cout << arr[0] << "\n";
            cout << theDay << "\n";
            cout << myObject.add() << "\n";
            using namespace premier;
            cout << e << "\n";
            cout << ident <<  "\n";  
            cout << "\n";

            //code for template sequence container list
            TempList<float> lst;
            TempIterator<float> iter;

            iter = lst.begin();

            lst.push_back(1.1);
            lst.push_back(2.2);
            lst.push_back(3.3);
            lst.push_back(4.4);


            for (TempIterator<float> p = lst.begin(); p; ++p)
                {
                    cout << *p << '\n';
                }            
        }

The first half of this function body is what has been there in the precious parts of the series. The second half is new and is for the templates. This function is actually called in the mainFile.cpp file.

Testing
I compiled the application with the following command:

    g++ c:\dir1\sampleEntities.cpp mainFile.cpp -o mainFile.exe

There was no error message. I executed the application with the following command:

    mainFile.exe

The fn() function displayed the output for its top and bottom function body portions, as expected.

Download
All the files are in one zip file called, dir1.zip. When you unzip the file, place the file mainFile.cpp in the working directory. Test (compile and execute) the application as illustrated above. The above instruction lines are for my compiler; yours should be similar. The link is:

http://www.broad-network.com/ChrysanthusForcha/dir1.zip

End of Tutorial
This is the end of this tutorial. I hope you can now incorporate a template into your C++ application.

End of Series
I hope you have appreciated this series. The aim is to enable you write your own C++ application. If you have been reading all the series in this volume (see link of “C++ Course” below) in the order given, then you should be in the position to write a very good C++ application.

I assume you have completed this series. It means you already had the professional skills in C++. Now, you are in an advanced course of C++. You may already be working. If you ever have too much work, you can subcontract (outsource) some or all to someone in the site below.

Chrys

Programming Solutions

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem

Comments

Become the Writer's Fan
Send the Writer a Message