Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Structures

In this tutorial, you’ll learn about structures in C++ programming; what is it, how to define it and use it in your program with the help of examples.


Structure

  • A structure is a user-defined data type in C++.
  • A structure creates a data type that can be used to group items of possibly different types into a single line.
  • Structure are different form array because arrays only hold data of similar data types, on the other hand structure can store data of multiple data types.
  • It is similar to a class in that, both holds a collecion of data of different data types.
  • Each element in the structure is called a member.

For example:

  • You want to store some information about someone: their name, citizenship and age.
  • You can easily create different variables name, citz, age to store these information separately
  • However, you may need to store information about many persons in the future. Now, you’d need to create different variables for each information per person: name1, citz1, age1, name2, citz2, age2.
  • You can easily visualize how big and messy the code would look.
  • Also, since no relation between the variables (information) would exist, it’s going to be a daunting task.

A better approach will be to have a collection of all related information under a single name Person, and use it for every person. Now, the code looks much cleaner, readable and efficient a well.

This collection of all related information under a single name Person is a structure.


Syntax

A structure is defined with the struct keyword. A structure is a possible collection of primary data types and other structures.

  • The structure_name holds the name we want to give to our structure.
  • data_type variable is the C++ variables of different data types like intcharfloat, etc.
struct structure_name {
// data_type variable 1
// data_type variable 2
// data_type variable 3
...
};

Structure in C++ can contain two types of members:

  • Data Member: These members are normal C++ variables. we can create a structure with variables of different data types in C++.
  • Member Functions: These members are normal C++ functions. Along with variables. we can also include functions inside a structure declararion.

Example:

// Data Members
char name[50];
int age;
int marks;

// Member Functions
void studentDetails(){
cout << "Name: " << name<< endl;
cout <<"Age: " << age << endl;
cout << "Marks: " << marks;
}

In the above structure, the data members are two integer variables and one character variable to store age, marks ans name of any student and member function is studentDetails() which is printing all of the above details of any student.


How to declare a structure in C++ programming?

  • The struct keyword defines a structure type followed by an identifier (name of the structure).
  • The struct members are added within curly braces. These members probably belong to different data types.

For example:

struct Person
{
char name[20];
int age;
int citizenship;
};

In the above example,

  • A structure person is defined which has three members: nameage and citizenship.
  • When a structure is created, no memory is allocated.
  • The structure definition is only the blueprint for the creating of variables. You can imagine it as a datatype.

When you define an integer as below:

int cool;

The int specifies that, variable cool can hold integer element only.

Similarly, structure definition only specifies that, what property a structure variable holds when it is defined.

Note: Remember to end the declaration with a semicolon (;)


How to define a structure variable?

  • Structure members cannot be initialized with declaration.
  • Once you declare a structure person as above.

You can define a structure variable as:

Person city;

Here, a structure variable city is defined which is of type structure Person.

When structure variable is defined, only then the required memory is allocated by the compiler.

Hence, 58 bytes of memory is allocated for structure variable city.


How to access members of a structure?

The Structure members are accessed using dot (.) operator.

Suppose, you want to access age of structure variable p and assign it 20 to it. You can perform this task by using following code below:

p.age = 20;

Example 1: C++ Program to find the area of rectangle

// C++ Program to find the area of rectangle using struct.
#include <iostream>
using namespace std;                                                         
struct Rectangle{
    int width, height;
};
                                                                      
int main(){
    struct Rectangle rec;
    rec.width = 6;
    rec.height = 4;
                                                                      
    cout << "Area of Rectangle is: " << (rec.width * rec.height) << endl;
                                                                      
    return 0;
}

Output

Area of Rectangle is: 24

In the above example Rectangle is declared which has two data members width and height

Inside main() function, a structure variable rec is defined.


Example 2: C++ Program to assign data to a structure variable and display it

// C++ Program to assign data to members of a structure variable and display it.
#include <iostream>
using namespace std;
                                                                      
struct Person{
    char name[30];
    int age;
    int citizenship;
};
                                                                      
int main(){
    Person p1;
    
    cout << "Enter Full name: ";
    cin.get(p1.name, 30);
    cout << "Enter age: ";
    cin >> p1.age;
    cout << "Enter citizenship: ";
    cin >> p1.citizenship;
                                                                      
    cout << "\nDisplay Person Information." << endl;
    cout << "Name: " << p1.name << endl;
    cout <<"Age: " << p1.age << endl;
    cout << "citizenship: " << p1.citizenship;
                                                                      
    return 0;
}

Output

Enter Full name: Sunny Sirohi
Enter age: 20
Enter citizenship: 1

Display Person Information.
Name: Sunny Sirohi
Age: 20
Citizenship: 1

Here a structure Person is declared which has three members nameage and citizenship.

Inside main() function, a structure variable p1 is defined. Then, the user is asked to enter information and data entered by the user is displayed.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!