logo

C++ 구조체

C++에서 클래스와 구조체는 클래스 인스턴스를 만드는 데 사용되는 청사진입니다. 구조체는 직사각형, 색상, 점 등과 같은 경량 객체에 사용됩니다.

클래스와 달리 C++의 구조체는 참조 유형이 아닌 값 유형입니다. 구조체 생성 후 수정하지 않으려는 데이터가 있는 경우 유용합니다.

AVL 나무

C++ 구조 다양한 데이터 유형의 모음입니다. 다양한 유형의 데이터를 보유하는 클래스와 유사합니다.

구조의 구문

 struct structure_name { // member declarations. } 

위 선언에서 구조체는 다음과 같이 선언됩니다. 구조체 키워드 그 뒤에 식별자(구조 이름)가 옵니다. 중괄호 안에는 다양한 유형의 멤버 변수를 선언할 수 있습니다. 다음 상황을 고려하십시오.

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

위의 경우 Student는 name, id, age 세 개의 변수를 포함하는 구조입니다. 구조가 선언되면 메모리가 할당되지 않습니다. 구조체의 변수가 생성되면 메모리가 할당됩니다. 이 시나리오를 이해해 봅시다.

Structure의 인스턴스를 만드는 방법은 무엇입니까?

구조 변수는 다음과 같이 정의할 수 있습니다.

학생;

타이프스크립트로 지도

여기서 s는 유형의 구조 변수입니다. 학생 . 구조체 변수가 생성되면 메모리가 할당됩니다. Student 구조에는 char 변수 1개와 정수 변수 2개가 포함되어 있습니다. 따라서 하나의 char 변수에 대한 메모리는 1바이트이고 두 개의 int는 2*4 = 8이 됩니다. s 변수가 차지하는 총 메모리는 9바이트입니다.

구조 변수에 액세스하는 방법:

구조의 변수는 구조의 인스턴스, 점(.) 연산자, 구조의 필드를 차례로 사용하여 간단히 액세스할 수 있습니다.

자바에서 인쇄

예를 들어:

 s.id = 4; 

위의 명령문에서 우리는 다음을 사용하여 Student 구조의 id 필드에 액세스하고 있습니다. 점(.) 연산자를 사용하고 id 필드에 값 4를 할당합니다.

C++ 구조체 예

두 개의 데이터 멤버 너비와 높이가 있는 Rectangle 구조체의 간단한 예를 살펴보겠습니다.

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

C++ 구조체 예제: 생성자와 메서드 사용

생성자를 사용하여 데이터를 초기화하고 메서드를 사용하여 사각형의 면적을 계산하는 구조체의 또 다른 예를 살펴보겠습니다.

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

구조와 클래스

구조 수업
액세스 지정자가 명시적으로 선언되지 않으면 기본적으로 액세스 지정자는 공개됩니다. 액세스 지정자가 명시적으로 선언되지 않으면 기본적으로 액세스 지정자는 비공개가 됩니다.
구조 구문:

구조체 구조체_이름
{
// 구조체의 본체.
}
클래스 구문:

수업 수업_이름
{
// 클래스의 본체.
}
구조의 인스턴스를 '구조 변수'라고 합니다. 클래스의 인스턴스는 '클래스의 개체'로 알려져 있습니다.