๊ตฌ์กฐ์ฒด
๋ฐ์ดํฐ ๊ตฌ์กฐ ๋ ํ๋ ์ ์ด๋ฆ์ผ๋ก ํจ๊ป ๊ทธ๋ฃนํ๋ ๋ฐ์ดํฐ ์์ ๊ทธ๋ฃน์
๋๋ค. ๊ตฌ์ฑ์ ์ด๋ผ๊ณ ํ๋ ์ด๋ฌํ ๋ฐ์ดํฐ ์์ ๋ ์ ํ๊ณผ ๊ธธ์ด๊ฐ ๋ค๋ฅผ ์ ์์ต๋๋ค. ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ ๋ค์ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ C++์์ ์ ์ธํ ์ ์์ต๋๋ค. ์ฌ๊ธฐ์ ๋ ๊ตฌ์กฐ ์ ํ์ ์ด๋ฆ์ด๊ณ ์ด ๊ตฌ์กฐ ์ ํ ์ ๊ฐ์ง ๊ฐ์ฒด์ ๋ํ ์ ํจํ ์๋ณ์ ์งํฉ์ผ ์ ์์ต๋๋ค. ์ค๊ดํธ ์์ ๋ฐ์ดํฐ ๋ฉค๋ฒ๊ฐ ์๋ ๋ชฉ๋ก์ด ์์ผ๋ฉฐ ๊ฐ ๋ฉค๋ฒ๋ ์ ํ๊ณผ ์ด๋ฆ์ผ๋ก ์ ํจํ ์๋ณ์๋ก ์ง์ ๋ฉ๋๋ค. ์๋ฅผ ๋ค์ด:
struct type_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
type_nameobject_name{}
struct product {
int weight;
double price;
} ;
product apple;
product banana, melon;
์ด๊ฒ์ ๋ผ๊ณ ํ๋ ๊ตฌ์กฐ ์ ํ์ ์ ์ธํ๊ณ product๋ ๊ฐ์ ๋ฉค๋ฒ( weight๋ฐ price, ๊ฐ๊ฐ ๋ค๋ฅธ ๊ธฐ๋ณธ ์ ํ)๋ฅผ ๊ฐ๋ ๊ตฌ์กฐ ์ ํ์ ์ ์ํฉ๋๋ค. ์ด ์ ์ธ์ ์ ์ ํ( product)์ ๋ง๋ ๋ค์ ์ด ์ ํ์ ์ธ ๊ฐ์ฒด(๋ณ์)๋ฅผ ์ ์ธํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค . apple, banana๋ฐ melon. ํ ๋ฒ product์ ์ธ๋๋ฉด ๋ค๋ฅธ ์ ํ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์ฌ์ฉ๋ฉ๋๋ค.
์ ์ ๋ struct๊ณผ ๋ ์ธ๋ฏธ์ฝ๋ก ( ;) ์์ ์ ํ์ ํ๋ object_names๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์กฐ ์ ํ์ ๊ฐ์ฒด๋ฅผ ์ง์ ์ ์ธํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ๊ตฌ์กฐ ๊ฐ์ฒด apple, banana๋ฐ melon๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ ์ ํ์ด ์ ์๋๋ ์๊ฐ ์ ์ธ๋ ์ ์์ต๋๋ค.
struct product {
int weight;
double price;
} apple, banana, melon;
์ด ๊ฒฝ์ฐ object_names๊ฐ ์ง์ ๋๋ฉด ์ ํ ์ด๋ฆ( product)์ด ์ ํ ์ฌํญ struct์ด type_name๋ฉ๋๋ค object_names. ๊ตฌ์กฐ ์ ํ ์ด๋ฆ( )๊ณผ ์ด ์ ํ์ ๊ฐ์ฒด( , , ๋ฐ )
๋ฅผ ๋ช
ํํ๊ฒ ๊ตฌ๋ณํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค . ๋จ์ผ ๊ตฌ์กฐ ์ ํ( )์์ ๋ง์ ๊ฐ์ฒด(์: , ๋ฐ )๋ฅผ ์ ์ธํ ์ ์์ต๋๋ค . ๊ฒฐ์ ๋ ๊ตฌ์กฐ ์ ํ์ ์ธ ๊ฐ์ฒด๊ฐ ์ ์ธ๋๋ฉด( , , ๋ฐ ) ํด๋น ๋ฉค๋ฒ์ ์ง์ ์ก์ธ์คํ ์ ์์ต๋๋ค. ๊ทธ ๊ตฌ๋ฌธ์ ๋จ์ํ ์ (productapplebananamelonapplebananamelonproduct
applebananamelon.) ๊ฐ์ฒด ์ด๋ฆ๊ณผ ๊ตฌ์ฑ์ ์ด๋ฆ ์ฌ์ด. ์๋ฅผ ๋ค์ด, ์ด๋ฌํ ์์๋ฅผ ํด๋น ์ ํ์ ํ์ค ๋ณ์์ธ ๊ฒ์ฒ๋ผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
apple.weight
apple.price
banana.weight
banana.price
melon.weight
melon.price
์ด๋ค ๊ฐ๊ฐ์ ์ฐธ์กฐํ๋ ๋ฉค๋ฒ์ ํด๋นํ๋ ๋ฐ์ดํฐ ์ ํ์ ๊ฐ์ต๋๋ค. apple.weight, banana.weight, melon.weightare type , intwhile apple.price, are type . ๋ค์์ ๊ตฌ์กฐ ์ ํ์ด ์๋ํ๋ ์ค์ ์์ ๋๋ค.banana.pricemelon.pricedouble
// example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
} mine, yours;
void printmovie (movies_t movie);
int main ()
{
string mystr;
mine.title = "2001 A Space Odyssey";
mine.year = 1968;
cout << "Enter title: ";
getline (cin,yours.title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> yours.year;
cout << "My favorite movie is:\n ";
printmovie (mine);
cout << "And yours is:\n ";
printmovie (yours);
return 0;
}
void printmovie (movies_t movie)
{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}
์ด ์์ ๋ ๊ฐ์ฒด์ ๋ฉค๋ฒ๊ฐ ์ผ๋ฐ ๋ณ์์ฒ๋ผ ์๋ํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค. ์๋ฅผ ๋ค์ด, ๋ฉค๋ฒ yours.year๋ ์ ํ ์ ์ ํจํ ๋ณ์ int์ด๊ณ mine.title์ ํ์ ์ ํจํ ๋ณ์์
๋๋ค string.
๊ทธ๋ฌ๋ ๊ฐ์ฒด mine๋ฐ yours๋ํ ์ ํ(์ ํ movies_t)์ ๊ฐ์ง ๋ณ์์
๋๋ค. ์๋ฅผ ๋ค์ด ๋ ๋ค printmovie๋จ์ํ ๋ณ์์ธ ๊ฒ์ฒ๋ผ ํจ์์ ์ ๋ฌ๋์์ต๋๋ค. ๋ฐ๋ผ์ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ๊ธฐ๋ฅ ์ค ํ๋๋ ๋ ๊ตฌ์ฑ์์ ๊ฐ๋ณ์ ์ผ๋ก ์ฐธ์กฐํ๊ฑฐ๋ ์ ์ฒด ๊ตฌ์กฐ๋ฅผ ์ ์ฒด์ ์ผ๋ก ์ฐธ์กฐํ ์ ์๋ค๋ ๊ฒ์
๋๋ค. ๋์ผํ ์๋ณ์๋ฅผ ์ฌ์ฉํ๋ ๋ ๊ฒฝ์ฐ ๋ชจ๋ ๊ตฌ์กฐ์ ์ด๋ฆ์
๋๋ค.
๊ตฌ์กฐ๋ ์ ํ์ด๊ธฐ ๋๋ฌธ์ ํ
์ด๋ธ์ด๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๊ตฌ์ฑํ๊ธฐ ์ํด ๋ฐฐ์ด ์ ํ์ผ๋ก ์ฌ์ฉํ ์๋ ์์ต๋๋ค.
// array of structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
} films [3];
void printmovie (movies_t movie);
int main ()
{
string mystr;
int n;
for (n=0; n<3; n++)
{
cout << "Enter title: ";
getline (cin,films[n].title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> films[n].year;
}
cout << "\nYou have entered these movies:\n";
for (n=0; n<3; n++)
printmovie (films[n]);
return 0;
}
void printmovie (movies_t movie)
{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}
๊ตฌ์กฐ์ฒด์ ๋ํ ํฌ์ธํฐ
๋ค๋ฅธ ์ ํ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๊ตฌ์กฐ์ฒด๋ ์์ฒด ํฌ์ธํฐ ์ ํ์ผ๋ก ๊ฐ๋ฆฌํฌ ์ ์์ต๋๋ค.
struct movies_t {
string title;
int year;
};
movies_t amovie;
movies_t * pmovie;
๋ค์ amovie์ ๊ตฌ์กฐ ์ ํ์ ๊ฐ์ฒด์ด๊ณ ๋ ๊ตฌ์กฐ ์ ํ์ ๊ฐ์ฒด๋ฅผ movies_t๊ฐ๋ฆฌํค๋ pmovieํฌ์ธํฐ movies_t์
๋๋ค. ๋ฐ๋ผ์ ๋ค์ ์ฝ๋๋ ์ ํจํฉ๋๋ค.
pmovie = &amovie;โ
ํฌ์ธํฐ์ ๊ฐ์๋ pmovieobject์ ์ฃผ์๊ฐ ํ ๋น๋ฉ๋๋ค amovie.
์ด์ ํฌ์ธํฐ์ ๊ตฌ์กฐ๋ฅผ ํผํฉํ๊ณ ์๋ก์ด ์ฐ์ฐ์์ธ ํ์ดํ ์ฐ์ฐ์( ->)๋ฅผ ๋์
ํ๋ ๋ ๋ค๋ฅธ ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
// pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
};
int main ()
{
string mystr;
movies_t amovie;
movies_t * pmovie;
pmovie = &amovie;
cout << "Enter title: ";
getline (cin, pmovie->title);
cout << "Enter year: ";
getline (cin, mystr);
(stringstream) mystr >> pmovie->year;
cout << "\nYou have entered:\n";
cout << pmovie->title;
cout << " (" << pmovie->year << ")\n";
return 0;
}
ํ์ดํ ์ฐ์ฐ์( ->)๋ ๋ฉค๋ฒ๊ฐ ์๋ ๊ฐ์ฒด์ ๋ํ ํฌ์ธํฐ์ ํจ๊ป ๋ ์ ์ ์ผ๋ก ์ฌ์ฉ๋๋ ์ญ์ฐธ์กฐ ์ฐ์ฐ์์ ๋๋ค. ์ด ์ฐ์ฐ์๋ ํด๋น ์ฃผ์์์ ์ง์ ๊ฐ์ฒด์ ๊ตฌ์ฑ์์ ์ก์ธ์คํ๋ ์ญํ ์ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด ์์ ์์์:
pmovie->title
๋ชจ๋ ๋ชฉ์ ์์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
(*pmovie).title
pmovie->title๋ฐ ๋ ํํ์ ๋ชจ๋ (*pmovie).title์ ํจํ๋ฉฐ title๋ผ๋ ํฌ์ธํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ๋ฉค๋ฒ์ ์ก์ธ์คํฉ๋๋ค pmovie. ํ์คํ ๋ค์๊ณผ ๋ค๋ฆ ๋๋ค.
*pmovie.title
์ด๋ ๋ค์๊ณผ ๋ค์ ๋์ผํฉ๋๋ค.
*(pmovie.title)
title์ด๊ฒ์ ๊ตฌ์กฐ์ฒด ๊ฐ์ฒด์ ๊ฐ์ ํฌ์ธํฐ ๋ฉค๋ฒ๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ ์ ๊ทผํ ๊ฒ์ ๋๋ค ( ํฌ์ธํฐ ์ ํ์ด ์๋๊ธฐ pmovie๋๋ฌธ์ ๊ทธ๋ ์ง ์์ต๋๋ค). title๋ค์ ํจ๋์ ํฌ์ธํฐ ๋ฐ ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ์ ๋ํ ์ฐ์ฐ์์ ๊ฐ๋ฅํ ์กฐํฉ์ ์์ฝํฉ๋๋ค.
ํํํ๊ฐ๋๋ ๊ฒ๋๋ฑํ
Expression | What is evaluated | Equivalent |
a.b | b๊ฐ์ฒด์ ๊ตฌ์ฑ์a | |
a->b | b๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด์ ๋ฉค๋ฒa | (*a).b |
*a.b | b๊ฐ์ฒด์ ๋ฉค๋ฒ๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐa | *(a.b) |
์ค์ฒฉ ๊ตฌ์กฐ
๊ตฌ์กฐ์ฒด๋ ๊ตฌ์กฐ์ฒด์ ์์ ์์ฒด๊ฐ ๋ ๋ค๋ฅธ ๊ตฌ์กฐ์ฒด๊ฐ ๋๋ ๋ฐฉ์์ผ๋ก ์ค์ฒฉ๋ ์๋ ์์ต๋๋ค.
struct movies_t {
string title;
int year;
};
struct friends_t {
string name;
string email;
movies_t favorite_movie;
} charlie, maria;
friends_t * pfriends = &charlie;
์ด์ ์ ์ธ ์ดํ์๋ ๋ค์ ํํ์์ด ๋ชจ๋ ์ ํจํฉ๋๋ค.
charlie.name
maria.favorite_movie.title
charlie.favorite_movie.year
pfriends->favorite_movie.year
'๐ Development Study > ๐จ๐พโ๐ป C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํด๋์ค(1) (0) | 2022.08.19 |
---|---|
๊ธฐํ ๋ฐ์ดํฐ ํ์ (0) | 2022.08.19 |
๋์ ๋ฉ๋ชจ๋ฆฌ (0) | 2022.08.19 |
ํฌ์ธํฐ (0) | 2022.08.19 |
๋ฌธ์์ด (0) | 2022.08.19 |
๋๊ธ