In C++ and in Delphi, you can create either a new data type or an alias for an existing data type. To create an alias in Delphi, you have to use the reserved word type like this:
type TMyString = string;
To create an alias in C++, you need to use the reserved word typedef:
typedef existing_type_name new_type_name;
For instance, the following listing shows how to create integer and ShortString types in C++.
Listing 7-3: Using typedef to create data type aliases
#include <iostream.h> #include <conio.h> #pragma hdrstop typedef long Integer; typedef char ShortString[256]; #pragma argsused int main(int argc, char* argv[]) { ShortString s; cout << "User name: "; cin >> s; cout << "Hello, " << s << endl; getch(); return 0; }
To create an enumerated type in C++, you have to use the reserved word enum:
enum enumerated_type_name {value_1, value_2, value_n}; enum figure {pawn, rook, knight, bishop, queen, king};
By default, in both Delphi and C++, the value of the enumeration's first element is 0. To change the value of an element and others that follow it, use the following syntax:
enum enumeration_name {element_1 = value_1, element_n = value_n};
For instance, in the following enumeration, the value of pawn is 1, the value of rook is 2, and the value of the king element is 11:
enum figure {pawn = 1, rook, knight, bishop, queen = 10, king};
Structures are the C++ equivalent of Delphi's records. To declare a new structure, you need to use the reserved word struct followed by a block that contains a list of fields. The structure's block must be terminated with a semicolon:
struct name_of_the_structure { field_1; field_n; };
The following listing shows how to declare a TBook structure that contains both simple fields and a string array field. It also shows how to declare a variable of type TBook and how to access structure members (the same way record fields are accessed in Delphi).
Listing 7-4: Using structures
#include <iostream.h> #include <conio.h> #include <string.h> #pragma hdrstop struct TBook { char Title[50]; int PageCount; char Authors[4][40]; // array of four 40-character strings }; #pragma argsused int main(int argc, char* argv[]) { TBook book; strcpy(book.Title, "Delphi Language Guide"); strcpy(book.Authors[0], "Borland"); book.PageCount = 249; return 0; }
Since C++ is less strict than Delphi, it allows you to declare structure variables when you declare the structure itself. To declare structure variables at the same time you declare the structure, use the following syntax:
struct name_of_the_structure { field_1; field_n; } variable_1, variable_n; struct TBook { char Title[50]; int PageCount; char Authors[4][40]; } lang_guide, user_guide, reference; // declare 3 variables
If you want to pass a structure to a function, declare the parameter as a reference parameter to avoid copying the entire record when you call the function:
void ShowBook(TBook& b) { }
The new and delete operators can also be used to dynamically create and delete structures. When we dynamically create a structure, we can no longer use the dot syntax to access its fields. To access the fields of a dynamically created structure, you have to write this:
TBook *book = new TBook; (*book).PageCount = 100; delete book;
Although you can use the (*dynamic_structure).field syntax to access fields in a dynamically created structure, there's a more popular operator (especially in OOP) that can be used in this situation, the -> operator (indirect member selector):
TBook *book = new TBook; book->PageCount = 100; delete book;