Broad Network


User Types from Fundamental Types in C++

C++ Just After the Basics - Part 7

Forward: In this article, we see how you can define your own object type from other object types, especially fundamental objects.

By: Chrysanthus Date Published: 22 Aug 2012

Introduction

The fundamental object types in C++ are char, int, float, bool and void. int is divided into the categories, short int, int, long int, unsigned short int, unsigned int, and unsigned long int. float is divided into float, double and long double. Derived object types that are closely related to the fundamental object types are the pointer, the enumeration and the array. In this article, I explain how you can declare your own object type from other object types, especially fundamental objects and the derived types.

You need basic knowledge in C++ in order to understand this article. If you do not have that knowledge, then read the series in this blog, whose first part is titled, “Getting Started with C++”. To reach the series, type the title and my name Chrys in the Search Box of this page and click Search. You should also have knowledge on the different categories of the int and float types.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Syntax
The syntax to create your own object type, from other objects, is as follows:

    typedef existingType newTypeName;

typedef is a C++ reserved word. existingType is an existing type such as “char” or “unsigned long int”. newTypeName is the name (identifier) you want to give for your user type. For the rest of this article, we shall look at examples.

Existing Type of Char
Read and try the following code:

#include <iostream>
using namespace std;

int main()
    {
        typedef char C;
        
        C myChar = 'A';

        cout << myChar;

        return 0;
    }

The first statement in main defines the user type called, C. The existing type for this is char. The second statement uses C as an object type to initialize a C object called myChar, whose value is “A’. The third statement displays the value, ‘A’. In this code, the user type, C and the fundamental type, char are synonyms. In the code, C is considered as a new object type and myChar is an identifier whose name is chosen by you.

Existing Type of Unsigned Int
Read and try the following code:

#include <iostream>
using namespace std;

int main()
    {
        typedef unsigned int WORD;
        
        WORD theWrd = 25;

        cout << theWrd;

        return 0;
    }

The first statement in main defines the user type called, WORD. The existing type for this is the rather long phase, “unsigned int”. The second statement uses WORD as an object type to initialize a WORD object called theWrd, whose value is 25. The third statement displays the value, 25. In this code, the user type WORD and the fundamental type “unsigned int” are synonyms. In the code, WORD is considered as a new object type and theWrd is an identifier whose name is chosen by you.

Existing Type of Pointer to Int
Read and try the following code:

#include <iostream>
using namespace std;

int main()
    {
        typedef int * interge;
        
        interge intPtr;
        *intPtr = 36;

        cout << *intPtr;

        return 0;
    }

The first statement in main defines the user type called, interge. It is a pointer to an int; this means the pointer object will hold the address of an int object. Note the position of the asterisk in the first statement. The existing type for this is “int*”, which means pointer to an int. The second statement uses interge as an object type to initialize an interge object called intPtr. The third statement assigns a value to the object pointed to by intPtr. The fourth statement displays the value of the pointed object. In this code, the user type interge and the fundamental type “int*” are synonyms. With the user type, interge, we have been able to declare a pointer without using the asterisk. The following code segment shows how you would initialize a pointer of type interge, using the & operator and still without using the asterisk.

        typedef int * interge;
        
        int anInt = 36;
        interge intPtr = &anInt;

        cout << *intPtr;

Note that interge means int*, so the initialization statement is equivalent to

        int *intPtr = &anInt;

Note: the asterisk has actually been used indirectly in the typedef statement. In the code, interge is considered as a new object type and intPtr is a normal pointer identifier, whose name is chosen by you.

Existing Type of Array
Read and try the following code:

#include <iostream>
using namespace std;

int main()
    {
        typedef char arr[10];

        arr myArr;
        myArr[5] = 'A' ;

        cout << myArr[5];

        return 0;
    }

The first statement in main has the normal declaration of an array, but it is preceded by the reserved word, typedef. Because of the preceding reserved word, typedef, the array name becomes an array type, which you can use to declare any array having the same number of elements but without the [] operator and preceding fundamental object type (char). The second statement declares an array, myArr, without the [] operator and without any preceding fundamental object type. This new array has 10 elements even though the [] operator has not been used. The third statement assigns a value to the sixth element of the new array. The last statement displays the value. In this code, the user type, arr and the expression, “char array[10]” are synonyms. In the code, arr is considered as a new object type and myArr is a normal array name, which you choose.

From the above examples, you can declare your own type using other fundamental objects. One good use of user-defined type is when the original type is a long phrase.

That is what I have prepared for “User Types from Fundamental Types in C++”. We continue in the next part of the series.

Chrys

Related Courses

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

Comments

Become the Writer's Fan
Send the Writer a Message