C++

Arrow Operator C++

C++ is a very vast language and comes up with many new aspects and concepts in it, i.e., classes, structures, unions, pointers, and variables. We can access and make use of all these data structures in C++ in different ways. One of these data structures commonly used in C++ is the arrow operator. The Arrow operator in C++ has been used to access or reference some data member of any data structure like class, structure, or union. Therefore, we will be using the arrow operator in C++ while working in the Ubuntu 20.04 system. Let’s start by opening the shell console application of the Ubuntu 20.04 system with the utilization of the “Ctrl+Alt+T” shortcut. On the other side, make sure you have a C++ compiler already configured on your end.

Within the opened console application, we have to generate a new C++ document, i.e., with a c++ extension. The terminal uses the “touch” instruction for the creation of a new C++ file. The generated C++ empty file has been found in the system’s home folder. The command has been stated in the attached picture.

There are a lot of editors supported by the Linux system to edit the files, i.e., gnu nano, text, and vim editor. Thus, you need to use one of them to open the already created new file. Therefore, we have tried the “gnu nano” editor to open the file with the “nano” command as shown beneath.

Example 01

The empty file “arrow.cc” has been opened within the GNU Nano 4.8 editor of Ubuntu 20.04. C++ requires the header files to be added within the code because, without them, our C++ code will not be executable or may throw errors at run time. The very first header file is the input and output stream. This can be added using the hash sign and the keyword “include” at the very start of your new file, as you can see. The C++ code also utilized some input and output statements to get the standard input from the user and output that data to the screen. For that, C++ uses the standard namespace. To add that standard namespace, try it with the word “using,” as illustrated from the image beneath.

Now, we have utilized the structure data type in our code to further make use of it to illustrate the arrow operator. The structure is named “test” and contains the declaration of a single integer type member named “age”. The pointers will be used to access the structure data members. Thus, we have declared a new structure test type pointer “p” as NULL after the structure declaration. The compilation of any C++ code has always been started from its main() method. Thus, we have started the main method after the pointer declaration.

Within the main function, we have assigned the memory to the pointer “p” of the structure test as per its needs using the malloc function method.  Now, the pointer “p” has been utilized to add the value to the data member “age” of structure “test” while using the arrow pointer “->”. At the very next line, we have used the standard “cout” statement to display the value of a variable “age” with the help of a pointer “p” pointing towards the variable “age” using the “->” arrow operator. The main function has been closed here, and we have saved our C++ code using “Ctrl+S”.

Let’s quit the C++ code file, i.e., arrow.cc, by a shortcut “Ctrl+X”. This code is now ready to be compiled and executed. Firstly, we will be compiling it with the C++ compiler, i.e., “g++”. The command for the compilation of a file “arrow.cc” can be seen in the image below. The compilation returns nothing in returns shows that the C++ code is logically and syntactically correct. Let’s begin with the file execution. As seen below, this can be performed with the simple “./a.out” query in the Ubuntu 20.04 terminal. The code returns “Age: 25” in return as the arrow operator was successful in getting the variable value.

Example 02

This example will be quite similar to the first example because it contains the same test structure, main function, and pointer. The only change might be the addition of some extra variables and functions. So, the very first change is the addition of a new header file, “string” that will be helpful to use some built-in functions in this code. The next change is the addition of the character type variable “name” in the “test” structure. The third change has been done within the main() function of this C++ code. So, we have used the “strcpy” function to add the value “Aqsa” to the variable “name” of structure “test” with the help of an arrow operator preceded by pointer “p”. The cout statement has been updated to show the value of variable age and name.

The C++ code compilation and running has been done with the same two commands as we used before. The output for the use of the “arrow operator” with pointer has been shown below.

Example 03

Let’s have a new example to illustrate the working of an arrow operator in C++. We have utilized the same header file of the input-output stream with the “#include” keyword. After this, we again used the line “using namespace std” to add the extension of the standard namespace of C++ to take input and show output. We have started the new class “test” declaration. The class contains the private type two data members. One of them is the integer type variable “marks” to store the scores, while the other stores the calculated float type percentage. Then, this class contains the user-defined method named “cal” returning nothing having 1 parameter of integer type. This function is getting argument value as a score from the main() function and assigning it to the variable “marks” using the “arrow” operator, i.e., “->”.

The variable “marks” has been used to calculate the percentage. The calculated value would be saved to the float variable “percentage”. Another user-defined method, “show” with no return type, has been implemented to display the percentage calculated and marks within the shell using the standard cout statement. The main function is declaring an object of pointer type. This object pointer is used with an arrow operator to pass the value to the function “cal” and access the function show(). This has been done two times, i.e., two different values have been passed; thus, two different outputs will be generated from the “show” method.

The output shows two different results for two different arguments passed to the function “cal” i.e., 42% and 92%.

Conclusion

This article contains the demonstration of using the arrow operator in C++ to add data to some variables and access the data members as well. This has been done using the pointer variable or pointer object with the arrow operator. We have utilized the structures and class to enhance the demonstration of the arrow operator in C++. All the examples discussed above are quite simple and easy to understand. To sum up, we hope this guide will prove its worth.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.