<< Chapter < Page Chapter >> Page >
An explanation and example creating versatile code using typedef within the C++ programming language.

Overview

Everyone seeks of ways to be more efficient in what they do. A farmer uses a tractor instead of a horse. A construction worker uses an air powered nail gun instead of a hammer. Programmers are no different than others, in that they are constantly improving their ability to produce correctly working programs. Some aspect of this is the use of modular/structured programming, proper documentation and following industry rules for a specific programming language. One example of efficient coding is letting the computer count the number of elements in an array. If we define an array:

int ages[] = {33,32,10,3};

We can use the following expression to calculate the number of members in the array:

sizeof ages / sizeof ages[0]

This type of flexible coding allows us to change the members of the array by adding or subtracting a values, like this:

int ages[] = {57,33,32,3,1};

Thus, we don't have to modify our code that uses the expression that calculates the number of member in the array.

One use of the typedef is to allow us to write code that can be quickly changed to handle different data types. There are several integer and floating-point data types that all store number values with different domains. If we write our code using some typedef statement, then our code becomes versatile . By changing only our typedef commands, our code can be used to process data of a different data type. This is demonstrated within the demo file provided, thus you need to study this material in conjunction with the demo program.

Demonstration program in c++

Creating a folder or sub-folder for source code files

Depending on your compiler/IDE, you should decide where to download and store source code files for processing. Prudence dictates that you create these folders as needed prior to downloading source code files. A suggested sub-folder for the Bloodshed Dev-C++ 5 compiler/IDE might be named:

  • Demo_Programs

If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.

Download the demo program

Download and store the following file(s) to your storage device in the appropriate folder(s). Following the methods of your compiler/IDE, compile and run the program(s). Study the source code file(s) in conjunction with other learning materials. You may need to right click on the link and select "Save Target As" in order to download the file.

Download from Connexions: Demo_Versatile_Array_Functions.cpp

Download from Connexions: Demo_Farm_Acres_Input.txt

Download from Connexions: Demo_Deposit_Checks_Input.txt

Definitions

versatile
Easily modifying code to handle another data type.
flexible coding
Using the sizeof operator to calculate the number of members in an array.
typedef
Allows the programmer to create an alias, or synonym, for an existing data type.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals - a modular structured approach using c++. OpenStax CNX. Jan 10, 2013 Download for free at http://cnx.org/content/col10621/1.22
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?

Ask