본문 바로가기

IT 관련자료/C++

객체지향 프로그래밍의 기초

class를 사용해서 코드를 간편하게 만드는 작업인데, 구조체랑 차이점은 잘 모르겠다. 다음강의에서 정리하고 수정하자

>>가장 큰 차이점은 접근지정자(access specifier) 로 class밖에서 접근할 수 있는 범위를 지정할 수 있다는점....!

 

 

//Object
//Friend : name, address, age, height, weight, ...
// print();

class Friend // 객체(object)라는 개념을 프로그램 언어(문법)로써 구현하는게 class
{
public: // access specifier (public, pribate, protected)
//_ 이나 m_를 써서 멤버임을 표시하는 경우가 많음
string _name;
string _address;
int _age;
double _height;
double _weight;

void print()
{
cout << _name << " " << _address << " " << _age << " " << _height <<
" " << _weight << endl;
}

};



int main()
{
// 실제로 메모리 공간을 차지하도록 선언하는것.(instanciation)
// main_Fr 이라는 instance 생성
Friend main_Fr; 
main_Fr.print();
//print(main_Fr);

vector<Friend> my_friends;
my_friends.resize(2);

for (auto& element : my_friends)
element.print();




return 0;
}

'IT 관련자료 > C++' 카테고리의 다른 글

객체지향 기본개념 정리코드  (0) 2022.02.24
생성자맴버 초기화  (0) 2022.02.24
단언하기(Assert)  (0) 2022.02.22
방어적 프로그래밍의 개념  (0) 2022.02.21
스택과 힙  (0) 2022.02.21