Harold Serrano

View Original

User-Defined Types in C++: Part 1

Data types such as int, float, char are known as built-in types. Although useful, built-in types do not provide the flexibility required to write complex applications. C++ circumvents this issue by providing a mechanism to create user-defined types. User-defined types provide a better representation of program objects used in complex applications.

User-defined types are made up of built-in types and the mechanism use to create them are:

  • Structures
  • Classes

In this article, I will talk you about Structures. In Part II of this article, I will talk about Classes.

Structures

A Data Structure provides a mechanism to organize several built-in types into a single entity. A Data Structure consist of the following syntax:

struct User_Defined_Type_Name{

built_in_type;
built_in_type;
built_in_type;

}

The struct keyword declares a structure of type "User_Defined_Type_Name" and defines the built-in types within the structure. Built-in data types within a structure are known as Data Members.

For example, the code snippet below declares a Structure of type Student with several data members.

struct Student{

    //Data members of the structure
    int age; 
    float GPA;
    char grade;
};

Student is an User-Defined Type. When you define a variable of type "Student," an object is created with a memory location and storage.

For example, in the snippet below, line 1 defines a structure, harold, of type Student. The definition allocates enough memory to hold all three built-in data types. In this instance: grade, GPA, and age.

int main(){

    Student harold; //1. Declaring a structure variable

    return 0;
}

Accessing data members

To access a data member in a structure, you must use the dot "." operator. For example, in the snippet below, the data member "age" is accessed using the dot operator (line 2).

int main(){

    Student harold; //1. Declaring a structure variable

    harold.age=29; //Access the structure data member "age"

    return 0;
}

The dot operator is used when a Structure is local to a function, has been declared globally or is "passed by value" to a function.

For example, in line 6, the structure is "passed by value" to the function "printAge." In line 2, the data member is accessed using the dot operator.

void printAge(Student uStudent); //1. Declaring function with "pass by value" argument

void printAge(Student uStudent){

    int age=uStudent.age; //2. Accessing the structure data member with "." operator

    std::cout<<"Print Age: "<<age<<std::endl; //3. Print student age

}

int main(){

    Student harold; //4. Declare a structure variable

    harold.age=29; //5. Set age of student

    printAge(harold); //6. call function and pass the structure by value

    return 0;
}

The "->" is also used to access the structure's data members. However, the "->" operator is used when a structure is "passed by reference" to a function.

For example, in line 5, the structure is "passed by reference" to the function "changeAge." In line 2, the data member is accessed and modified using the "->" operator.

void changeAge(Student *uStudent); //1. Declaring function with "pass by reference" argument

void changeAge(Student *uStudent){

    uStudent->age=30; //2. Accessing the structure data member with "->" operator

}

int main(){

    Student harold; //3. Declare a structure variable

    harold.age=29; //4. Set age of student

    changeAge(&harold); //5. call function and pass the structure by reference

    std::cout<<"Print age:"<<harold.age<<std::endl; //6. Print the new age of student

    return 0;
}

In Part II of this article, I will talk about the second mechanism used to create user-defined types. i.e., Classes.

Hope this helps.