logo

Strukture C++

V C++ so razredi in strukture načrti, ki se uporabljajo za ustvarjanje primerka razreda. Strukture se uporabljajo za lahke predmete, kot so pravokotnik, barva, točka itd.

Za razliko od razreda so strukture v C++ vrednostni tip kot referenčni tip. Uporabno je, če imate podatke, ki niso namenjeni spreminjanju po ustvarjanju strukture.

naključni vrstni red sql

Struktura C++ je zbirka različnih tipov podatkov. Podobno je razredu, ki hrani različne vrste podatkov.

Sintaksa strukture

 struct structure_name { // member declarations. } 

V zgornji deklaraciji je struktura deklarirana s predhodnim ključna beseda struct ki mu sledi identifikator (ime strukture). Znotraj zavitih oklepajev lahko deklariramo članske spremenljivke različnih tipov. Razmislite o naslednji situaciji:

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

V zgornjem primeru je Student struktura, ki vsebuje tri spremenljivke ime, id in starost. Ko je struktura deklarirana, se pomnilnik ne dodeli. Ko je ustvarjena spremenljivka strukture, se dodeli pomnilnik. Razumejmo ta scenarij.

Kako ustvariti primerek strukture?

Strukturno spremenljivko lahko definiramo kot:

Študent s;

powershell manjše ali enako

Tukaj je s strukturna spremenljivka tipa študent . Ko je strukturna spremenljivka ustvarjena, bo pomnilnik dodeljen. Struktura študenta vsebuje eno spremenljivko char in dve celoštevilski spremenljivki. Zato je pomnilnik za eno spremenljivko char 1 bajt, dva inta pa bosta 2*4 = 8. Skupni pomnilnik, ki ga zaseda spremenljivka s, je 9 bajtov.

Kako dostopati do spremenljivke Struktura:

Do spremenljivke strukture lahko dostopate tako, da preprosto uporabite primerek strukture, ki mu sledi operator pika (.) in nato polje strukture.

java načrtovalni vzorec

Na primer:

 s.id = 4; 

V zgornjem stavku dostopamo do polja id strukture Student z uporabo pika(.) in dodeli vrednost 4 polju id.

Primer strukture C++

Oglejmo si preprost primer strukture Rectangle, ki ima dva podatkovna člana, širino in višino.

 #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>

Primer strukture C++: uporaba konstruktorja in metode

Oglejmo si še en primer strukture, kjer uporabljamo konstruktor za inicializacijo podatkov in metodo za izračun površine pravokotnika.

 #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>

Struktura proti razredu

Struktura Razred
Če specifikator dostopa ni izrecno deklariran, bo privzeto specifikator dostopa javen. Če specifikator dostopa ni izrecno deklariran, bo specifikator dostopa privzeto zaseben.
Sintaksa strukture:

struct ime_strukture
{
// telo strukture.
}
Sintaksa razreda:

razred ime_razreda
{
// telo razreda.
}
Primerek strukture je znan kot 'Strukturna spremenljivka'. Primerek razreda je znan kot 'predmet razreda'.