구조체 사용하기 함수 매개변수로서의 구조체 구조체 포인터와 레퍼런스...
DESCRIPTION
ㅎㅎ. C++ 프로그래밍 기초 : 객체지향의 시작. 구조체. 구조체 사용하기 함수 매개변수로서의 구조체 구조체 포인터와 레퍼런스 구조체 배열. 7 장 . 구조체. 학습목표 struct를 사용해 새로운 자료형을 정의한다. 구조체를 다양하게 사용한다. 매개변수가 구조체인 함수를 정의한다. 구조체 포인터를 이용해 구조체를 간접 참조한다. 구조체로 배열을 선언하고 사용한다. 구조체 - PowerPoint PPT PresentationTRANSCRIPT
77 장장 ㅎㅎ
• 구조체 사용하기• 함수 매개변수로서의 구조체• 구조체 포인터와 레퍼런스• 구조체 배열
구조체구조체
동의대 김 미 진
C++ C++ 프로그래밍 기초프로그래밍 기초 : 객체지향의 시작
2
학습목표 struct 를 사용해 새로운 자료형을 정의한다 . 구조체를 다양하게 사용한다 . 매개변수가 구조체인 함수를 정의한다 . 구조체 포인터를 이용해 구조체를 간접 참조한다 . 구조체로 배열을 선언하고 사용한다 .
7 장 . 구조체
3
구조체예약어 struct 를 사용해 새로운 자료형을 정의할 수 있는데 struct 로 정의된 자료형을 구조체라고 한다 .
구조체 선언
Section 01 구조체 사용하기
struct 구조체명 {
자료형 멤버변수 ;
자료형 멤버변수 ;
:
:
};
4
구조체 변수 선언
Section 01 구조체 사용하기
struct 구조체명 구조체변수명 1, 구조체변수명 2, .... 구조체변수명 n;
5
Section 01 구조체 사용하기
6
[ 실습하기 7-1] 구조체 정의하고 사용하기
01 #include<iostream>02 using namespace std;03 struct namecard{04 char name[20];05 char job[30];06 char tel[20];07 char email[40];08 };09 void main()10 {11 struct namecard man; 12 13 cout<<" 이름을 입력하세요 =>";14 cin>>man.name;15 cout<<" 직업을 입력하세요 =>";16 cin>>man.job17 cout<<" 연락처를 입력하세요 =>";18 cin>>man.tel;19 cout<<" 이메일을 입력하세요 =>";20 cin>>man.email;21 22 cout<<"\n 입력받은 데이터를 출력합니다 . ";23 cout<<"\n 이름 \t 직업 \t 연락처 \t 이메일 ";24 cout<<"\n ====================================================";25 cout<<"\n "<<man.name<<"\t "<< man.job << "\t "<< man.tel <<"\t "<< man.email;26 cout<<"\n ====================================================";27 cout<<"\n sizeof(namecard) => "<<sizeof(namecard);28 cout<<"\n 구조체 변수의 메모리 사이즈 =>"<<sizeof(namecard)<<"\n";29 }
[ 예제 7-1]7_1.cpp
7
구조체 변수 초기화
Section 01 구조체 사용하기
8
[ 실습하기 7-2] 구조체 정의하고 사용하기
01 #include<iostream>02 using namespace std;03 struct namecard{04 char name[20];05 char job[30];06 char tel[20];07 char email[40];08 };09 void main()10 {11 namecard x={ " 김주현 ", "MCSE 전문강사 ", "418-9876","[email protected]"},12 y={ " 박혜경 ", " 웹마스터 ", "551-6986","[email protected]"},13 z={ " 김동식 ", " 기획 A 팀대리 ", "318-3961","[email protected]"};14 15 cout<<"\n 이름 직업 연락처 이메일 ";16 cout<<"\n =====================================================";17 cout<<"\n "<<x.name <<"\t"<< x.job <<"\t"<< x.tel <<"\t"<< x.email;18 cout<<"\n "<<y.name <<"\t"<< y.job <<"\t"<< y.tel <<"\t"<< y.email;19 cout<<"\n "<<z.name <<"\t"<< z.job <<"\t"<< z.tel <<"\t"<< z.email;20 cout<<"\n =====================================================\n";21 }
[ 예제 7-2]7_2.cpp
9
구조체 단위로 값을 대입하기
Section 01 구조체 사용하기
10
[ 실습하기 7-3] 구조체 단위로 값 복사하기
01 #include<iostream>02 using namespace std;03 struct namecard{04 char name[20];05 char job[30];06 char tel[20];07 char email[40];08 };09 void main()10 {11 namecard x={ " 김주현 ", "MCSE 전문강사 ", "418-9876","[email protected]"};12 namecard y;13 14 y = x;15 16 cout<<"\n\t\t 이름 \t 직업 \t\t 연락처 \t 이메일 ";17 cout<<"\n\t\t =====================================";18 cout<<"\n< 구조체 변수 x>";19 cout<<"\t"<<x.name <<"\t"<< x.job <<"\t"<< x.tel <<"\t"<< x.email;20 cout<<"\n< 구조체 변수 y>";21 cout<<"\t"<<y.name<<"\t"<<y.job<<"\t"<<y.tel<<"\t"<< y.email<<"\n";22 }
[ 예제 7-3]7_3.cpp
11
함수 매개변수로서의 구조체
Section 02 함수 매개변수로서의 구조체
12
[ 실습하기 7-4] 구조체를 매개변수로 하는 값에 의한 전달 방식의 함수
01 #include<iostream>02 using namespace std;03 struct namecard{04 char name[20];05 char job[30];06 char tel[20];07 char email[40];08 };09 void structPrn(namecard temp); // 함수의 선언10 void main()11 {12 namecard x={" 김주현 ", "MCSE 전문강사 ", "418-9876", "[email protected]"},13 y={" 박혜경 ", " 웹마스터 ", "551-6986", "[email protected]"},14 z={" 김동식 ", " 기획 A 팀대리 ", "318-3961", "[email protected]"};15 16 cout<<"\n 이름 직업 연락처 이메일 ";17 cout<<"\n=====================================================";18 structPrn(x); // 함수의 호출19 structPrn(y);20 structPrn(z);21 cout<<"\n ==============================================\n";22 }23 24 void structPrn(namecard temp) // 함수의 정의25 {26 cout<<"\n"<<temp.name<<"\t"<<temp.job<<"\t"27 <<temp.tel<<"\t"<<temp.email;28 }
[ 예제 7-4]7_4.cpp
13
구조체를 반환값으로 갖는 함수
Section 02 함수 매개변수로서의 구조체
14
구조체를 반환값으로 갖는 함수 ( 구조체 단위 입력함수 작성하기 )
Section 02 함수 매개변수로서의 구조체
15
[ 실습하기 7-5] 구조체 단위 입력함수 작성하기
01 #include<iostream>02 using namespace std;03 struct namecard{04 char name[20];05 char job[30];06 char tel[20];07 char email[40];08 };09 10 void structPrn(namecard temp); 11 namecard structInput( );12 13 void main()14 {15 namecard x, y, z;16 17 x=structInput( );18 y=structInput( );19 z=structInput( );20 21 cout<<"\n 이름 직업 연락처 이메일 ";22 cout<<"\n===========================================";23 structPrn(x); 24 structPrn(y);25 structPrn(z);26 cout<<"\n========================================\n";27 }28
[ 예제 7-5]7_5.cpp
16
[ 실습하기 7-5] 구조체 단위 입력함수 작성하기
29 void structPrn(namecard temp) // 함수의 정의30 {31 cout<<"\n"<<temp.name<<"\t"<<temp.job <<"\t"<<temp.tel<<"\t"<<temp.email;32 }33 34 namecard structInput( )35 {36 namecard temp;37 cout<<"\n 이름을 입력하세요 =>";38 cin>>temp.name;39 cout<<" 직업을 입력하세요 =>";40 cin>>temp.job;41 cout<<" 연락처를 입력하세요 =>";42 cin>>temp.tel;43 cout<<" 이메일을 입력하세요 =>";44 cin>>temp.email;45 46 return temp;47 }
[ 예제 7-5]7_5.cpp
17
구조체 변수와 포인터의 관계 (1/2)
Section 03 구조체 포인터와 레퍼런스
자료형 * 포인터 변수 ;
18
구조체 변수와 포인터의 관계 (2/2)
Section 03 구조체 포인터와 레퍼런스
19
구조체 포인터로 멤버 접근하기 (1/2)
잘못된 표현
잘된 표현
Section 03 구조체 포인터와 레퍼런스
20
구조체 포인터로 멤버 접근하기 (2/2)
구조체 변수가 주어졌을 때 멤버변수에 접근하기 위해서는 닷 (.) 연산자를 사용하고 구조체포인터에 대해서는 애로우 (->) 연산자를 사용한다 .
Section 03 구조체 포인터와 레퍼런스
21
[ 실습하기 7-6] 구조체 변수와 포인터의 관계 알아보기
01 #include<iostream>02 using namespace std;03 struct namecard{04 char name[20];05 char job[30];06 char tel[20];07 char email[40];08 };09 10 void structPrn(namecard temp); 11 namecard structInput( );12 13 void main()14 {15 namecard x={" 전원지 "," 디자이너 ","345-0876","[email protected]"},16 y={" 전수빈 "," 의사 ","356-0868","[email protected]"};17 namecard *p;18 p=&x;19 cout<<"\n 이름 직업 연락처 이메일 ";20 cout<<"\ n=======================================================";21 cout<<"\n"<<(*p).name<<"\t"<<(*p).job<<"\t"22 <<(*p).tel<<"\t"<<(*p).email;23 p=&y;24 cout<<"\n"<<p->name<<"\t"<<p->job<<"\t"<<p->tel<<"\t"<<p->email;25 cout<<"\n========================================================";26 27 cout<<"\n\n sizeof(x) => "<<sizeof(x);28 cout<<"\n sizeof(p) => "<<sizeof(p)<<"\n";29 }
[ 예제 7-6]7_6.cpp
22
[ 실습하기 3-1] if 문을 사용해 절대값 구하기
23
[ 실습하기 7-7] 구조체 변수와 포인터의 관계 알아보기
01 #include<iostream>02 using namespace std;03 struct namecard{04 char name[20];05 char job[30];06 char tel[20];07 char email[40];08 };09 10 void structPrn(namecard temp); 11 void structInput(namecard *pTemp);12 13 void main()14 {15 namecard x, y, z;16 17 structInput(&x); // 함수의 호출18 structInput(&y);19 structInput(&z);20 21 cout<<"\n 이름 직업 연락처 이메일 ";22 cout<<"\n========================================================";23 structPrn(x); 24 structPrn(y);25 structPrn(z);26 cout<<"\n=======================================================\n";27 }28
[ 예제 7-7]7_7.cpp
24
[ 실습하기 7-7] 구조체 변수와 포인터의 관계 알아보기
01 #include<iostream>02 using namespace std;03 struct namecard{04 char name[20];05 char job[30];06 char tel[20];07 char email[40];08 };09 10 void structPrn(namecard temp); 11 void structInput(namecard *pTemp);12 13 void main()14 {15 namecard x, y, z;16 17 structInput(&x); // 함수의 호출18 structInput(&y);19 structInput(&z);20 21 cout<<"\n 이름 직업 연락처 이메일 ";22 cout<<"\n========================================================";23 structPrn(x); 24 structPrn(y);25 structPrn(z);26 cout<<"\n=======================================================\n";27 }28 29 void structPrn(namecard temp) // 함수의 정의30 {31 cout<<"\n"<<temp.name<<"\t"<<temp.job<<"\t"<<temp.tel<<"\t"<<temp.email;32 }33 34 void structInput(namecard *pTemp)35 {36 cout<<"\n 이름을 입력하세요 =>";37 cin>>pTemp->name;38 cout<<" 직업을 입력하세요 =>";39 cin>>pTemp->job;40 cout<<" 연락처를 입력하세요 =>";41 cin>>pTemp->tel;42 cout<<" 이메일을 입력하세요 =>";43 cin>>pTemp->email;44 }
[ 예제 7-7]7_7.cpp
25
구조체 포인터로 멤버 접근하기 (3/4)
Section 03 구조체 포인터와 레퍼런스
26
[ 실습하기 7-8] 구조체 레퍼런스를 매개변수로 갖는 함수 작성하기
01 #include<iostream>02 using namespace std;03 struct namecard{04 char name[20];05 char job[30];06 char tel[20];07 char email[40];08 };09 10 void structPrn(namecard temp); 11 void structInput(namecard &temp);12 13 void main()14 {15 namecard x, y, z;16 17 structInput(x); // 함수의 호출18 structInput(y);19 structInput(z);20 21 cout<<"\n 이름 직업 연락처 이메일 ";22 cout<<"\n===========================================================";23 structPrn(x); 24 structPrn(y);25 structPrn(z);26 cout<<"\n=======================================================\n";27 }28 29 void structPrn(namecard temp) // 함수의 정의30 {31 cout<<"\n"<<temp.name<<"\t"<<temp.job<<"\t"<<temp.tel<<"\t"<<temp.email;32 }33 34 void structInput(namecard &temp)35 {36 cout<<"\n 이름을 입력하세요 =>";37 cin>>temp.name;38 cout<<" 직업을 입력하세요 =>";39 cin>>temp.job;40 cout<<" 연락처를 입력하세요 =>";41 cin>>temp.tel;42 cout<<" 이메일을 입력하세요 =>";43 cin>>temp.email;44 }
[ 예제 7-8]7_8.cpp
27
구조체 배열
Section 04 구조체 배열
28
[ 실습하기 7-9] 구조체 배열 사용하기
01 #include<iostream>02 using namespace std;03 struct namecard{04 char name[20];05 char job[30];06 char tel[20];07 char email[40];08 };09 void main()10 {11 namecard x[3]={{" 김주현 ", "MCSE 전문강사 ", "418-9876", "[email protected]"},12 {" 박혜경 ", " 웹마스터 ", "551-6986", "[email protected]"},13 {" 김동식 ", " 기획 A 팀대리 ", "318-3961", "[email protected]"}14 };15 cout<<"\n 이름 직업 연락처 이메일 ";16 cout<<"\n========================================================";17 for(int i = 0 ; i < 3; i++)18 cout<<"\n"<<x[i].name<<"\t"<<x[i].job<<"\t"19 <<x[i].tel<<"\t"<<x[i].email;20 cout<<"\n========================================================\n";21 }
[ 예제 7-9]7_9.cpp
29
[ 실습하기 7-9] 구조체 배열 사용하기
30
요약
예약어 struct 를 사용해 새로운 자료형을 정의할 수 있는데 struct로 정의된 자료형을 구조체라고 한다 .
구조체의 멤버를 참조하기 위한 연산자로 닷 연산자 (.) 와 애로우 연산자 (->) 가 제공된다 .
함수의 매개변수를 구조체로 선언하여 구조체 단위로 값을 전달할 수 있다 .
함수의 반환값으로 구조체 변수를 사용하면 함수의 자료형이 구조체가 된다 .
특정 함수 내에서 다른 함수에 선언된 구조체 변수를 제어하기 위해서는 구조체 포인터를 형식 매개변수로 하는 주소 호출방식으로 함수를 정의하거나 레퍼런스 변수로 형식 매개변수를 정의한다 .
동일한 구조체 형으로 선언된 기억 공간을 같은 목적으로 사용할 때는 구조체 배열을 사용하면 유용하다 .
Thank you