logo

Strutture C++

In C++, le classi e le strutture sono progetti utilizzati per creare l'istanza di una classe. Le strutture vengono utilizzate per oggetti leggeri come Rettangolo, Colore, Punto, ecc.

A differenza della classe, le strutture in C++ sono di tipo valore piuttosto che di tipo riferimento. È utile se si hanno dati che non devono essere modificati dopo la creazione della struttura.

Java aggiunge stringa

Struttura C++ è una raccolta di diversi tipi di dati. È simile alla classe che contiene diversi tipi di dati.

La sintassi della struttura

 struct structure_name { // member declarations. } 

Nella dichiarazione precedente, una struttura viene dichiarata precedendo il parola chiave struct seguito dall'identificatore (nome della struttura). All'interno delle parentesi graffe possiamo dichiarare variabili membro di diverso tipo. Consideriamo la seguente situazione:

 struct Student { char name[20]; int id; int age; } 

Nel caso precedente, Student è una struttura che contiene tre variabili nome, id ed età. Quando la struttura viene dichiarata, non viene allocata memoria. Quando viene creata la variabile di una struttura, viene allocata la memoria. Cerchiamo di comprendere questo scenario.

Come creare l'istanza di Structure?

La variabile di struttura può essere definita come:

Studenti;

cos'è svn checkout

Qui, s è una variabile di struttura di tipo Alunno . Quando viene creata la variabile di struttura, la memoria verrà allocata. La struttura dello studente contiene una variabile carattere e due variabili intere. Pertanto, la memoria per una variabile char è 1 byte e due int saranno 2*4 = 8. La memoria totale occupata dalla variabile s è 9 byte.

Come accedere alla variabile di Struttura:

È possibile accedere alla variabile della struttura semplicemente utilizzando l'istanza della struttura seguita dall'operatore punto (.) e quindi dal campo della struttura.

Per esempio:

 s.id = 4; 

Nell'istruzione precedente, accediamo al campo id della struttura Student utilizzando il comando punto(.) operatore e assegna il valore 4 al campo id.

elenco delle freccette

Esempio di struttura C++

Vediamo un semplice esempio di struttura Rectangle che ha due membri dati, larghezza e altezza.

 #include using namespace std; struct Rectangle { int width, height; }; int main(void) { struct Rectangle rec; rec.width=8; rec.height=5; cout&lt;<'area of rectangle is: '<<(rec.width * rec.height)<<endl; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Area of Rectangle is: 40 </pre> <h2>C++ Struct Example: Using Constructor and Method</h2> <p>Let&apos;s see another example of struct where we are using the constructor to initialize data and method to calculate the area of rectangle.</p> <pre> #include using namespace std; struct Rectangle { int width, height; Rectangle(int w, int h) { width = w; height = h; } void areaOfRectangle() { cout&lt;<'area of rectangle is: '<<(width*height); } }; int main(void) { struct rec="Rectangle(4,6);" rec.areaofrectangle(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Area of Rectangle is: 24 </pre> <p> <strong>Structure v/s Class</strong> </p> <table class="table"> <tr> <th>Structure</th> <th>Class</th> </tr> <tr> <td>If access specifier is not declared explicitly, then by default access specifier will be public. </td> <td>If access specifier is not declared explicitly, then by default access specifier will be private.</td> </tr> <tr> <td>Syntax of Structure: <br> <br> struct structure_name <br> { <br> // body of the structure. <br> }</td> <td>Syntax of Class: <br> <br> class class_name <br> { <br> // body of the class. <br> }</td> </tr> <tr> <td>The instance of the structure is known as &apos;Structure variable&apos;. </td> <td>The instance of the class is known as &apos;Object of the class&apos;.</td> </tr> </table></'area></pre></'area>

Esempio di struttura C++: utilizzo del costruttore e del metodo

Vediamo un altro esempio di struttura in cui utilizziamo il costruttore per inizializzare i dati e il metodo per calcolare l'area del rettangolo.

 #include using namespace std; struct Rectangle { int width, height; Rectangle(int w, int h) { width = w; height = h; } void areaOfRectangle() { cout&lt;<\'area of rectangle is: \'<<(width*height); } }; int main(void) { struct rec="Rectangle(4,6);" rec.areaofrectangle(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Area of Rectangle is: 24 </pre> <p> <strong>Structure v/s Class</strong> </p> <table class="table"> <tr> <th>Structure</th> <th>Class</th> </tr> <tr> <td>If access specifier is not declared explicitly, then by default access specifier will be public. </td> <td>If access specifier is not declared explicitly, then by default access specifier will be private.</td> </tr> <tr> <td>Syntax of Structure: <br> <br> struct structure_name <br> { <br> // body of the structure. <br> }</td> <td>Syntax of Class: <br> <br> class class_name <br> { <br> // body of the class. <br> }</td> </tr> <tr> <td>The instance of the structure is known as &apos;Structure variable&apos;. </td> <td>The instance of the class is known as &apos;Object of the class&apos;.</td> </tr> </table></\'area>

Struttura v/s Classe

Struttura Classe
Se lo specificatore di accesso non è dichiarato esplicitamente, per impostazione predefinita lo specificatore di accesso sarà pubblico. Se lo specificatore di accesso non è dichiarato esplicitamente, per impostazione predefinita lo specificatore di accesso sarà privato.
Sintassi della struttura:

struttura nome_struttura
{
// corpo della struttura.
}
Sintassi della classe:

classe nome_classe
{
// corpo della classe.
}
L'istanza della struttura è nota come 'Variabile di struttura'. L'istanza della classe è nota come 'Oggetto della classe'.