C++ All-in-One For Dummies
Book image
Explore Book Buy On Amazon

Structure templates have many interesting uses, such as creating a data repository that doesn’t depend on a particular type. The StructureTemplate example here shows one such use.

#include 
using namespace std;
template
struct Volume
{
    T height;
    T width;
    T length;
    Volume()
    {
        height = 0;
        width = 0;
        length = 0;
    }
    T getvolume()
    {
        return height * width * length;
    }
    T getvolume(T H, T W, T L)
    {
        height = H;
        width = W;
        length = L;
        return height * width * length;
    }
};
int main()
{
    Volume first;
    cout << "First volume: " << first.getvolume() << endl;
    first.height = 2;
    first.width = 3;
    first.length = 4;
    cout << "First volume: " << first.getvolume() << endl;
    Volume second;
    cout << "Second volume: " << second.getvolume(2.1, 3.2, 4.3) << endl;
    cout << "Height: " << second.height << endl;
    cout << "Width: " << second.width << endl;
    cout << "Length: " << second.length << endl;
    return 0;
}

In this case, the structure contains height, width, and length data values that the code can use to determine volume. The structure includes a constructor to initialize the values, so even if someone calls getvolume() without initializing the structure, nothing bad will happen.

The structure allows independent access of each of the data values. You can set or get them as needed.

The getvolume() function is overloaded. You can call it with or without input values. The code in main() tests the structure thoroughly. Here’s what you see as output from this example:

First volume: 0
First volume: 24
Second volume: 28.896
Height: 2.1
Width: 3.2
Length: 4.3

You can use structures for another interesting purpose. The C++ standard says you can’t create a typedef template. For example, the following code produces an error when you try to compile it:

template
typedef map MyDef;

When you try to compile this code in Code::Blocks, you see the following error:

error: template declaration of 'typedef'

However, you can define a typedef within a structure template. The StructureTemplate2 example code here shows how.

#include 
#include 
using namespace std;
template
struct MyDef
{
    typedef map Type;
};
int main()
{
    MyDef::Type marriages;
    marriages["Tom"] = "Suzy";
    marriages["Harry"] = "Harriet";
    cout << marriages["Tom"] << endl;
    cout << marriages["Harry"] << endl;
    return 0;
}

This example overcomes the C++ limitations by placing the typedef within the struct, MyDef. The same structure can hold any number of typedef entries.

Using a typedef in this manner makes it easier to work with map. All you need to worry about is the value type — the key type is already defined as string.

About This Article

This article is from the book:

About the book author:

John Mueller has published more than 100 books on technology, data, and programming. John has a website and blog where he writes articles on technology and offers assistance alongside his published books.

Luca Massaron is a data scientist specializing in insurance and finance. A Google Developer Expert in machine learning, he has been involved in quantitative analysis and algorithms since 2000.

This article can be found in the category: