0% found this document useful (0 votes)
2 views

Data structures - C++ Tutorials

This document provides a tutorial on data structures in C++, specifically focusing on the definition and usage of structures. It explains how to declare structures, access their members, and demonstrates examples of using structures with arrays and pointers. Additionally, it covers the concept of nesting structures and the use of the arrow operator for accessing members through pointers.

Uploaded by

abdl rasyid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data structures - C++ Tutorials

This document provides a tutorial on data structures in C++, specifically focusing on the definition and usage of structures. It explains how to declare structures, access their members, and demonstrates examples of using structures with arrays and pointers. Additionally, it covers the concept of nesting structures and the use of the arrow operator for accessing members through pointers.

Uploaded by

abdl rasyid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

6/3/2021 Data structures - C++ Tutorials

Search: Go
Not logged in

Tutorials C++ Language Data structures register log in

C++
Information
Tutorials
Reference
Articles
Forum

Tutorials Data structures


C++ Language
Ascii Codes Data structures
Boolean Operations
A data structure is a group of data elements grouped together under one name. These data elements, known as
Numerical Bases
members, can have different types and different lengths. Data structures can be declared in C++ using the following
C++ Language syntax:
Introduction:
struct type_name {
Compilers
member_type1 member_name1;
Basics of C++:
member_type2 member_name2;
Structure of a program member_type3 member_name3;
Variables and types .
Constants .
Operators } object_names;
Basic Input/Output
Program structure: Where type_name is a name for the structure type, object_name can be a set of valid identifiers for objects that have the
Statements and flow control type of this structure. Within braces {}, there is a list with the data members, each one is specified with a type and a
Functions valid identifier as its name.
Overloads and templates
Name visibility For example:
Compound data types:
Arrays 1 struct product {
Character sequences
2 int weight;
3 double price;
Pointers
4 } ;
Dynamic memory 5
Data structures 6 product apple;
Other data types 7 product banana, melon;
Classes:
Classes (I)
Classes (II) This declares a structure type, called product, and defines it having two members: weight and price, each of a different
Special members fundamental type. This declaration creates a new type (product), which is then used to declare three objects (variables)
Friendship and inheritance of this type: apple, banana, and melon. Note how once product is declared, it is used just like any other type.
Polymorphism
Other language features: Right at the end of the struct definition, and before the ending semicolon (;), the optional field object_names can be
Type conversions used to directly declare objects of the structure type. For example, the structure objects apple, banana, and melon can
Exceptions be declared at the moment the data structure type is defined:
Preprocessor directives
Standard library: 1 struct product {
Input/output with files
2 int weight;
3 double price;
4 } apple, banana, melon;

In this case, where object_names are specified, the type name (product) becomes optional: struct requires either a
type_name or at least one name in object_names, but not necessarily both.

It is important to clearly differentiate between what is the structure type name (product), and what is an object of this
type (apple, banana, and melon). Many objects (such as apple, banana, and melon) can be declared from a single
structure type (product).

Once the three objects of a determined structure type are declared (apple, banana, and melon) its members can be
accessed directly. The syntax for that is simply to insert a dot (.) between the object name and the member name. For
example, we could operate with any of these elements as if they were standard variables of their respective types:

1 apple.weight
2 apple.price
3 banana.weight
4 banana.price
5 melon.weight
6 melon.price

Each one of these has the data type corresponding to the member they refer to: apple.weight, banana.weight, and
melon.weight are of type int, while apple.price, banana.price, and melon.price are of type double.

Here is a real example with structure types in action:

1 // example about structures Enter title: Alien


2 #include <iostream> Enter year: 1979 Edit
3 #include <string> &
4 #include <sstream> My favorite movie is: Run
5 using namespace std; 2001 A Space Odyssey (1968)
6 And yours is:
7 struct movies_t { Alien (1979)
8 string title;
9 int year;
10 } mine, yours;
11
12 void printmovie (movies_t movie);
13
14 int main ()

https://www.cplusplus.com/doc/tutorial/structures/ 1/4
6/3/2021 Data structures - C++ Tutorials
15 {
16 string mystr;
17
18 mine.title = "2001 A Space Odyssey";
19 mine.year = 1968;
20
21 cout << "Enter title: ";
22 getline (cin,yours.title);
23 cout << "Enter year: ";
24 getline (cin,mystr);
25 stringstream(mystr) >> yours.year;
26
27 cout << "My favorite movie is:\n ";
28 printmovie (mine);
29 cout << "And yours is:\n ";
30 printmovie (yours);
31 return 0;
32 }
33
34 void printmovie (movies_t movie)
35 {
36 cout << movie.title;
37 cout << " (" << movie.year << ")\n";
38 }

The example shows how the members of an object act just as regular variables. For example, the member yours.year
is a valid variable of type int, and mine.title is a valid variable of type string.

But the objects mine and yours are also variables with a type (of type movies_t). For example, both have been passed
to function printmovie just as if they were simple variables. Therefore, one of the features of data structures is the
ability to refer to both their members individually or to the entire structure as a whole. In both cases using the same
identifier: the name of the structure.

Because structures are types, they can also be used as the type of arrays to construct tables or databases of them:

1 // array of structures Enter title: Blade Runner


2 #include <iostream> Enter year: 1982
3 #include <string> Enter title: The Matrix
4 #include <sstream> Enter year: 1999
5 using namespace std; Enter title: Taxi Driver
6 Enter year: 1976
7 struct movies_t {
8 string title; You have entered these movies:
9 int year; Blade Runner (1982)
10 } films [3]; The Matrix (1999)
11 Taxi Driver (1976)
12 void printmovie (movies_t movie);
13
14 int main ()
15 {
16 string mystr;
17 int n;
18
19 for (n=0; n<3; n++) Edit
20 { &
21 cout << "Enter title: "; Run
22 getline (cin,films[n].title);
23 cout << "Enter year: ";
24 getline (cin,mystr);
25 stringstream(mystr) >> films[n].year;
26 }
27
28 cout << "\nYou have entered these movies:\n";
29 for (n=0; n<3; n++)
30 printmovie (films[n]);
31 return 0;
32 }
33
34 void printmovie (movies_t movie)
35 {
36 cout << movie.title;
37 cout << " (" << movie.year << ")\n";
38 }

Pointers to structures
Like any other type, structures can be pointed to by its own type of pointers:

1 struct movies_t {
2 string title;
3 int year;
4 };
5
6 movies_t amovie;
7 movies_t * pmovie;

Here amovie is an object of structure type movies_t, and pmovie is a pointer to point to objects of structure type
movies_t. Therefore, the following code would also be valid:

pmovie = &amovie;

The value of the pointer pmovie would be assigned the address of object amovie.

https://www.cplusplus.com/doc/tutorial/structures/ 2/4
6/3/2021 Data structures - C++ Tutorials
Now, let's see another example that mixes pointers and structures, and will serve to introduce a new operator: the
arrow operator (->):

1 // pointers to structures Enter title: Invasion of the body snatchers


2 #include <iostream> Enter year: 1978
3 #include <string>
4 #include <sstream> You have entered:
5 using namespace std; Invasion of the body snatchers (1978)
6
7 struct movies_t {
8 string title;
9 int year;
10 };
11
12 int main ()
13 {
14 string mystr;
15
16 Edit
movies_t amovie;
17 movies_t * pmovie; &
18 pmovie = &amovie; Run
19
20 cout << "Enter title: ";
21 getline (cin, pmovie->title);
22 cout << "Enter year: ";
23 getline (cin, mystr);
24 (stringstream) mystr >> pmovie->year;
25
26 cout << "\nYou have entered:\n";
27 cout << pmovie->title;
28 cout << " (" << pmovie->year << ")\n";
29
30 return 0;
31 }

The arrow operator (->) is a dereference operator that is used exclusively with pointers to objects that have members.
This operator serves to access the member of an object directly from its address. For example, in the example above:

pmovie->title

is, for all purposes, equivalent to:

(*pmovie).title

Both expressions, pmovie->title and (*pmovie).title are valid, and both access the member title of the data
structure pointed by a pointer called pmovie. It is definitely something different than:

*pmovie.title

which is rather equivalent to:

*(pmovie.title)

This would access the value pointed by a hypothetical pointer member called title of the structure object pmovie
(which is not the case, since title is not a pointer type). The following panel summarizes possible combinations of the
operators for pointers and for structure members:

Expression What is evaluated Equivalent


a.b Member b of object a
a->b Member b of object pointed to by a (*a).b
*a.b Value pointed to by member b of object a *(a.b)

Nesting structures
Structures can also be nested in such a way that an element of a structure is itself another structure:

1 struct movies_t {
2 string title;
3 int year;
4 };
5
6 struct friends_t {
7 string name;
8 string email;
9 movies_t favorite_movie;
10 } charlie, maria;
11
12 friends_t * pfriends = &charlie;

After the previous declarations, all of the following expressions would be valid:

1 charlie.name
2 maria.favorite_movie.title
3 charlie.favorite_movie.year
4 pfriends->favorite_movie.year

https://www.cplusplus.com/doc/tutorial/structures/ 3/4
6/3/2021 Data structures - C++ Tutorials
(where, by the way, the last two expressions refer to the same member).

Previous: Next:
Dynamic memory Other data types
Index

Home page | Privacy policy


© cplusplus.com, 2000-2021 - All rights reserved - v3.2
Spotted an error? contact us

https://www.cplusplus.com/doc/tutorial/structures/ 4/4

You might also like