Wednesday, April 24, 2024

Structures in c++:

 

We frequently come around circumstances where we want to store a gathering of information whether of comparable information types or non-comparable information types. We have seen Clusters in C++ which are utilized to store set of information of comparative information types at bordering memory areas.

Dissimilar to Exhibits, Designs in C++ are client characterized information types which are utilized to store gathering of things of non-comparable information types.


What is a construction?


A design is a client characterized information type in C/C++. A construction makes an information type that can be utilized to bunch things of potentially various sorts into a solitary kind.


Structures in C++:

A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. 


How to make a design?


The 'struct' watchword is utilized to make a construction. The overall punctuation to make a design is as displayed beneath:


struct structureName{ member1;

    member2;
    member3;
    .
    .
    .
    memberN; 


Structures in C++ can contain two sorts of individuals:


Information Part: These individuals are typical C++ factors. We can make a design with factors of various information types in C++.

Part Works: These individuals are ordinary C++ capabilities. Alongside factors, we can likewise incorporate capabilities inside a design statement.

Example:  

// Data Members
int roll;
int age;
int marks;
     
// Member Functions
void printDetails()
{
    cout<<"Roll = "<<roll<<"\n";
    cout<<"Age = "<<age<<"\n";
    cout<<"Marks = "<<marks;
}

In the above structure, the data members are three integer variables to store roll number, age and marks of any student and the member function is printDetails() which is printing all of the above details of any student.


How to pronounce structure factors?


A construction variable can either be proclaimed with structure statement or as a different statement like fundamental sorts.


C++

// A variable declaration with structure declaration.
struct Point
{
   int x, y;
} p1;  // The variable p1 is declared with 'Point'
 
 
// A variable declaration like basic data types
struct Point
{
   int x, y;
};
 
int main()
{
   struct Point p1;  // The variable p1 is declared like a normal variable
}


Note: In C++, the struct watchword is discretionary before in statement of a variable. In C, it is obligatory.


How to instate structure individuals?

Structure individuals can't be instated with statement. For instance the accompanying C program flops in accumulation.

Be that as it may, is considered right in C++ 11 or more.


 

struct Point
{
   int x = 0;  // COMPILER ERROR:  cannot initialize members here
   int y = 0;  // COMPILER ERROR:  cannot initialize members here
};

The justification behind above blunder is straightforward, when a datatype is pronounced, no memory is designated for it. Memory is designated just when factors are made.


Structure individuals can be introduced with statement in C++. For Instance the accompanying C++ program Executes Effectively without tossing any Mistake.



// In C++ We can Initialize the Variables with Declaration in Structure.
 
#include <iostream>
using namespace std;
 
struct Point {
    int x = 0; // It is Considered as Default Arguments and no Error is Raised
    int y = 1;
};
 
int main()
{
    struct Point p1;
 
    // Accessing members of point p1
    // No value is Initialized then the default value is considered. ie x=0 and y=1;
    cout << "x = " << p1.x << ", y = " << p1.y<<endl;
   
    // Initializing the value of y = 20;
    p1.y = 20;
    cout << "x = " << p1.x << ", y = " << p1.y;
    return 0;
}
// This code is contributed by Samyak Jain
 x=0, y=1
 x=0, y=20

Structure members can be initialized using curly braces ‘{}’. For example, following is a valid initialization.

struct Point {
    int x, y;
};
 
int main()
{
    // A valid initialization. member x gets value 0 and y
    // gets value 1.  The order of declaration is followed.
    struct Point p1 = { 0, 1 };
}

  
How to access structure elements? 
Structure members are accessed using dot (.) operator. 

#include <iostream>
using namespace std;
 
struct Point {
    int x, y;
};
 
int main()
{
    struct Point p1 = { 0, 1 };
 
    // Accessing members of point p1
    p1.x = 20;
    cout << "x = " << p1.x << ", y = " << p1.y;
 
    return 0;
}
Output:
x = 20, y = 1

What is an array of structures?

Like other primitive data types, we can create an array of structures. 

#include <iostream>
using namespace std;
 
struct Point {
    int x, y;
};
 
int main()
{
    // Create an array of structures
    struct Point arr[10];
 
    // Access array members
    arr[0].x = 10;
    arr[0].y = 20;
 
    cout << arr[0].x << " " << arr[0].y;
    return 0;
}
Output:
10 20

  
What is a structure pointer?
 
Like primitive types, we can have pointer to a structure. If we have a pointer to structure, members are accessed using arrow ( -> ) operator instead of the dot (.) operator.

#include <iostream>
using namespace std;
 
struct Point {
    int x, y;
};
 
int main()
{
    struct Point p1 = { 1, 2 };
 
    // p2 is a pointer to structure p1
    struct Point* p2 = &p1;
 
    // Accessing structure members using
    // structure pointer
    cout << p2->x << " " << p2->y;
    return 0;
}
Output:
1 2

What is structure part arrangement?

In C++, a construction is equivalent to a class with the exception of a couple of contrasts. The most significant of them is security. A Construction isn't secure and can't conceal its execution subtleties from the end client while a class is secure and can conceal its customizing and planning subtleties. Find out about the distinctions among Designs and Class in C++.


No comments:

Post a Comment

last post

C language Vs C++ language in 2024

Popular Posts