본문 바로가기
공부,일/C++

클래스

by fromnothing1 2021. 8. 19.

 

또한 private 등 생성 지시자가 범위로 정해진다. 

#include <iostream>

class Animal {
 private:
  int food;
  int weight;

 public:
  void set_animal(int _food, int _weight) {
    food = _food;
    weight = _weight;
  }
  void increase_food(int inc) {
    food += inc;
    weight += (inc / 3);
  }
  void view_stat() {
    std::cout << "이 동물의 food   : " << food << std::endl;
    std::cout << "이 동물의 weight : " << weight << std::endl;
  }
};  // 세미콜론 잊지 말자!

int main() {
  Animal animal;
  animal.set_animal(100, 50);
  animal.increase_food(30);

  animal.view_stat();
  return 0;
}

 

 

 

'공부,일 > C++' 카테고리의 다른 글

Temporary Object  (0) 2021.10.17
string 클래스  (0) 2021.08.25
배열  (0) 2021.08.25
화면에 출력  (0) 2021.08.18
공부 블로그  (0) 2021.08.18

댓글