January 13, 2021

C++ Data Structures

In this lesson, we would learn about C++ data structures. We’ll see how to create them and how to use them in programs. We’ll cover the following sub-topics:

  1. What are Structures?
  2. Declaring a Structure
  3. Accessing Members of a Structure
  4. Using the typedef Keyword

 

1. What are Structures?

A structure in C++ is a collection of variables of different data type under a common name. It is similar to a record in a table. So while a C++ Array is a collection of variables of same type, a structure contains variables of different types.

Use cases include:

  • a student information made up of id, name, admission date, age, and department
  • a book record consisting of if, title, author and publish date

You may probably think of other examples as well

 

2. Declaring  a Structure

Use use the struct keyword to define a structure. Specify the struct keyword, followed by an identifier. Then inside curly braces, you can declare all the variables (called members) that make up the structure.

For example:

struct Student {
	int id;
	string firstname;
	string lastname;
	int age;
	string department;
};

Here a structure Student is defined and is has five members: id, firstname, lastname, age and department.

However, when a struct is created, unlike arrays, no memory is allocated. It’s similar to declaring a variable without initialization.

After declaring your struct, you can then declare structure type variable in same way you declare other variables. For example

Student archana;

Here, we have declare a variable archana which is of type Student. At this point, the compiler then allocates the required memory.

 

3. Accessing Member Variables of Struct

Members of a struct variable are accessed using the member access operator, dot (.).

Assuming we want to access department of structure variable archana and assign a value to it. We can achieve it using

<struct_name>.<member_name>

Like so

archana.department = "Computer Science"

The program below show how a struct works. We define a struct, then assign value from the user input

#include <iostream>

using namespace std;

struct Student {
	int id;
	string firstname;
	string lastname;
	int age;
	string department;
};

// main function
int main () {

	Student archana;

	archana.id = 1;

	cout << "Enter Student firstname: ";
	cin>> archana.firstname;
	cout << "Enter Student lastname: ";
	cin >> archana.lastname;
	cout << "Enter age: ";
	cin >> archana.age;
	cout << "Enter department: ";
	cin >> archana.department;

	cout << "\n******* Student Details ********"<<endl;
	cout << "ID:         " << archana.id        <<endl;
	cout << "Firstname:  " << archana.firstname <<endl;
	cout << "Lastname:   " << archana.lastname  <<endl;
	cout << "Age:        " << archana.age       <<endl;
	cout << "Department: " << archana.department<<endl;

   return 0;
}

One thing to note is that the structure is defined outside the main method.

The output of this program is given below:

Enter Student firstname: Kindson
Enter Student lastname: Munonye
Enter age: 34
Enter department: Computer Science

******* Student Details ********
ID:         1
Firstname:  Kindson
Lastname:   Munonye
Age:        34
Department: Computer

 

4. Using the typedef Keyword

Another way to create a structure is to use the typedef keyword. This make it kind of easier. For example, the code below creates a student structure.

typedef struct {
	int regNo;
	string name;
	int email;
	string course;
} Student;

With this, you can then create Student objects just like before.

In addition to creation of structures, the typedef keyword can also be used to define variables of other data types. Here are a few examples:

typedef long int myInt; //I just created my own data type!

myInt x, y; //x any y are now long ints

So this is the much we have for C++ Structures!

One thought on “C++ Data Structures

Leave a Reply

Your email address will not be published. Required fields are marked *