Toward Data Abstraction: typedef

 < Day Day Up > 



When you write a program you will want to represent the data you wish to manipulate in such a way that makes it clear to you and to others what you are doing. The data also should be modeled in a manner that best suits the problem at hand. And because you are modeling a real-world problem in a computer program you will have to select data elements of the real-world problem and map them into the program you are writing. If you limit yourself to the fundamental data types C++ offers this would be a difficult task. Luckily you’re not limited!

The first facility C++ provides to help with data abstraction is the type definition. The typedef keyword lets you declare an alternate name for an existing data type. This alternate name functions like a type synonym. There are several good reasons for creating type synonyms using typedef.

First, it increases program portability by allowing you to substitute appropriate machine-dependent data types when you compile your program on different computers. A good explanation of using typedef in this manner can be found in [Kernighan]. Second, you can use typedef to tame complicated type declarations. You saw an example of typedef used in this way in chapter 9’s section on function pointers.

Another good reason for using typedef is better program readability. By declaring type synonyms for existing data types you can more closely match their purpose in your program. This leads to a significant improvement in your program’s aesthetics. Let us take a closer look at the use of typedef to accomplish these last two objectives: taming complex data types and improving program aesthetics.

Creating Type Synonyms

The following examples show typedef being used to create a synonym for an existing data type:

typedef float Currency; typedef int Hours; typedef char* String

The first example declares a type synonym for the float data type called Currency. Currency is now just another name for a float. The second example declares a synonym for int named Hours, and the third example declares a synonym for a char pointer named String. These synonyms can now be used in a program.

I have adopted a synonym naming convention here that capitalizes the first letter of the identifier as to make it distinguishable from a variable. You can place typedef declarations in header files. The following example program shows typedef in action starting with the header file that contains the typedef declarations:

Listing 10.1: mydefs.h

start example
 1  #ifndef MY_DEFS_H 2  #define MY_DEFS_H 3 4  typedef float Currency; 5  typedef int Hours; 6 7  #endif
end example

The header file mydefs.h can now be included wherever the type synonyms Currency and Hours could be used to improve the clarity of the code. The following header file uses the declarations in mydefs.h to declare a function named calculatePay():

Listing 10.2: calculatepay.h

start example
 1  #ifndef CALCULATE_PAY_H  2  #define CALCULATE_PAY_H  3  #include "mydefs.h"  4  5  Currency calculatePay(Hours hoursWorked, Currency payPerHour);  6  7  #endif
end example

The calculatePay() function is then defined in the normal way:

Listing 10.3: calculatepay.cpp

start example
 1  #include "mydefs.h"  2  #include "calculatepay.h"  3  4  Currency calculatePay(Hours hoursWorked, Currency payPerHour){  5     return (hoursWorked * payPerHour);  6  }
end example

The following main() function shows calculatePay() in action:

Listing 10.4: main.cpp

start example
 1  #include <iostream>  2  #include "calculatepay.h"  3  #include "mydefs.h"  4  5  using namespace std;    6  7  int main(){  8      Hours employeeWorkHours = 0;  9      Currency employeePayRate = 0.0; 10 11      cout<<"Please enter hours worked: "; 12      cin>>employeeWorkHours; 13      cout<<endl<<"Please enter employee hourly pay rate: "; 14      cin>>employeePayRate; 15 16      cout<<endl<<"The employee's pay is: " 17      <<calculatePay(employeeWorkHours, employeePayRate)<<endl; 18 19      return 0; 20  }
end example



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net