구조체

struct

C++에서 구조체는 완벽한 타입으로 처리한다. struct 키워드를 사용하지 않는다.

실습 예제

ch04ex4_book.cpp

제목, 저자, 페이지, 가격 정보를 포함한 book 구조체를 생성한다.

#include <iostream>
#include <string>
using namespace std;

struct book {
	string title;
	string author;
	int pages;
	int price;
};

void display(book& t);

int main()
{
	book b[3] = {
		{"컴활1급 실기", "영진", 700, 40000},
		{"CPP", "PITCA", 300, 35000},
		{"Python", "org", 150, 27000}
	};

	for (int i = 0; i < 3; i++)
	{
		display(b[i]);
		cout << "----------------------" << endl;
	}

	return 0;
}

void display(book& t)
{
	cout << "제목: " << t.title << endl;
	cout << "저자: " << t.author << endl;
	cout << "페이지: " << t.pages << endl;
	cout << "가격: " << t.price << endl;
}

실습 문제

ch04lab4_person.cpp

인물 구조체(Person)의 내용을 출력하는 display_person()함수를 구현하는 코드를 작성하시오. 인물 구조체는 이름(name, 100자), 성별(gender, 10자), 나이(age, 정수) 등의 변수로 구성된다.

Last updated