회원 로그인
|
클래스
C++ 클래스
1. 프로그램의 모든 것들을 객체(Object)로 표현하는 프로그래밍 패러다임
2. 객체란? 세상 또는 게임의 모든 요소들 (플레이어, 몬스터, 아이템, 무기, 방어구, 총알 ....)
[객체 지향의 프로그래밍 특징]
1. 추상화 : 실제 객체에서 주요한 특징을 뽑아 프로그래밍적인 코드로 표현해야 한다.
자동차 -> 핸들, 바퀴, 프레임, 운전하다(), 달린다(), 정지한다() ....
2. 캡슐화 : 객체의 요소들 중 꼭 필요한 것들만 외부에서 접근 가능하도록 해야 한다.
자동차의 엔진은 외부에서 감춰져 있음
3. 상속성 : 공통된 기능을 가진 객체들의 공통적 요소를 뽑아 독립된 하나의 객체를 만들고 재사용하도록 한다.
차 -> 바퀴, 핸들, 운전하다, 달린다() <= 오토바이, 자전거, 자동차의 공통요소
4. 다형성 : 객체는 수행하는 기능에 따라 여러개의 객체 형태를 가질 수 있게 한다.
홍길동 -> 예비군, 아빠, 프로그래머, 길드장
[클래스]
- 객체를 만들기 위한 코드 설계도 (자동차 객체를 만들기 위해 자동차 설계도와 틀을 만듬)
- 커스텀 자료형? 클래스는 여러가지 자료형들을 가진 변수들의 조합을 가지고 만든 또다른 자료형임
- 객체를 표현하는 값을 속성, 객체의 동작 및 기능들을 메소드라 함
[클래스 문법]
1. 클래스 선언
class 클래스명 {
접근 지정자:
자료형 멤버 변수;
접근 지정자:
자료형 멤버 함수();
}
2. 클래스 정의
자료형 클래스명::멤버함수() {
...
...
}
3. 객체 생성
- 클래스타입 객체명;
- 클래스타입* 객체인스턴스명 = new 클래스타입();
4. 접근 지정자
- private : 클래스 내에서만 사용이 가능한 멤버 변수, 멤버 함수
- public : 클래스 외부에서도 사용이 가능함 멤버 변수, 멤버 함수
4. 메소드 오버로딩
- 다형성의 한 형태로 두개 이상의 메소드가 같은 목적을 가질경우 메소드 이름은 같고 매개변수 구성은 다르게 사용할 수 있는 것을 말한다.
#include <iostream> #include <stdlib.h> /* srand, rand */ #include <time.h> /* time */ #include <string> using namespace std; class Monster { public: string name; // 멤버 변수 int speed; int damage; // 방식2 // 멤버 함수, 메소드 // 매개변수가 없는 것은 class안에 소속되어 있음으로 void printName(){ cout << "몬스터 이름: " << name << endl; } void move(){ cout << "몬스터가 " << speed << " 속도로 이동합니다." << endl; } void attack(){ cout << "몬스터가 " << damage << " 공격력으로 대상을 피격합니다." << endl; } }; // 방식1 함수 따로 void PrintMonsterName(Monster monster) { cout << "몬스터의 이름은 " << monster.name << "입니다." << endl; } void MoveMonster(Monster monster) { cout << "몬스터가 " << monster.speed << " 속도로 이동합니다." << endl; } void AttackMonster(Monster monster) { cout << "몬스터가 " << monster.damage << " 공격력으로 대상을 피격합니다." << endl; } void main6(){ Monster monster; // 변수 선언 monster.name = "오우거"; monster.speed = 10; monster.damage = 50; // 방식1 PrintMonsterName(monster); MoveMonster(monster); AttackMonster(monster); // 방식2 (선호) monster.printName(); monster.move(); monster.attack(); }
객체별(class의 반복)
객체를 가진 몬스터가 많을 경우 배열 사용
3 몬스터의 이름, 속도, 체력, 공격력을 입력 받아 출력
// 예제 #include <iostream> #include <stdlib.h> /* srand, rand */ #include <time.h> /* time */ #include <string> #include <array> using namespace std; // 배열 예제 class Monster { public: string name; int speed; int hp; int damage; void printInfor(int index){ cout << "몬스터[" << index << "] 번째 이름 출력: " << name << endl; cout << "몬스터[" << index << "] 번째 속도 출력: " << speed << endl; cout << "몬스터[" << index << "] 번째 체력 출력: " << hp << endl; cout << "몬스터[" << index << "] 번째 공격력 출력: " << damage << endl; } }; // 상수 사용 #define arrayCount 5 void main(){ // 상수 값으로 배열 공간 확보 Monster monster[arrayCount]; int i = 0; // 입력 while (i < arrayCount) { cout << "몬스터[" << i << "] 번째 이름 입력 : "; cin >> monster[i].name; cout << "몬스터[" << i << "] 번째 속도 입력 : "; cin >> monster[i].speed; cout << "몬스터[" << i << "] 번째 체력 입력 : "; cin >> monster[i].hp; cout << "몬스터[" << i << "] 번째 공격력 입력 : "; cin >> monster[i].damage; i++; } cout << endl; // 출력 for (int i = 0; i < arrayCount; i++) { monster[i].printInfor(i); } }
C++에서 유니티 구성 구현
#include <iostream> // cin, cout #include <string> // string #include <stdio.h> // rand() #include <time.h> // time() #include <Windows.h> // Sleep() using namespace std; // 위치 정보 클래스 12bite class Position { public: float x; float y; float z; }; // 회전 정보 클래스 12bite class Rotation { public: float x; float y; float z; }; // 스케일 정보 클레스 12bite class Scale { public: float x; float y; float z; }; // 트레스폼 클래스 36bite // (컴포넌트=객체) class Transform { public: Position position; Rotation rotation; Scale scale; }; // 유니티 관점에선 Sprite Renderer 객체도 있다. // (컴포넌트=객체) class SpriteRenderer{ public: // ~~~ // ~~~ }; // 게임 오브젝트 클래스 // 객체 class GameObject { public: string name; Transform transform; SpriteRenderer spriteRenderer; // 컴포넌트=객체 추가 }; void main() { GameObject green1; // 그린1 생성 green1.name = "Green1"; // 이름 정의 // 초기화 green1.transform.position.x = 0; green1.transform.position.y = 0; green1.transform.position.z = 0; cout << green1.name << " position x, y, z : " << green1.transform.position.x << ", " << green1.transform.position.y << ", " << green1.transform.position.z << ")" << endl; green1.transform.position.y += 1; cout << green1.name << " position x, y, z : " << green1.transform.position.x << ", " << green1.transform.position.y << ", " << green1.transform.position.z << ")" << endl; GameObject green2; // 그린2 생성 green2.name = "Green2"; green2.transform.position.x = 0; green2.transform.position.y = 0; green2.transform.position.z = 0; cout << green2.name << " position x, y, z : " << green2.transform.position.x << ", " << green2.transform.position.y << ", " << green2.transform.position.z << ")" << endl; }
댓글 0개
| 엮인글 0개
285개(1/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
285 | [Python] 동적 import - 모듈을 변수로 받아오기 | 푸딩뱃살 | 412 | 2022.10.27 10:45 |
284 | [Python] 파이썬 3.7.7과 3.9.7의 os.path.expanduser() 차이 | 푸딩뱃살 | 455 | 2022.08.18 12:22 |
283 | [Python] error: Microsoft Visual C++ 9.0 is required. | 푸딩뱃살 | 684 | 2022.08.03 13:35 |
282 | [Python] pyscript | 푸딩뱃살 | 453 | 2022.06.09 11:21 |
281 | [Python] float is / float not is | 푸딩뱃살 | 593 | 2022.03.02 15:03 |
280 | [Python] 이터널 문자열 f | 푸딩뱃살 | 833 | 2022.01.27 16:35 |
279 | [Python] is와 ==의 차이 | 푸딩뱃살 | 485 | 2021.11.25 15:54 |
278 |
[Python] Error: ImportError: file |
푸딩뱃살 | 916 | 2021.11.16 11:24 |
277 | [Python] 파이썬 디컴파일 - uncompyle6 | 푸딩뱃살 | 774 | 2021.11.10 14:46 |
276 | [Python] 파이썬 확장자 설명 | 푸딩뱃살 | 550 | 2021.11.03 14:38 |
275 | [참고] 웹 fbx 뷰어 | 푸딩뱃살 | 470 | 2021.10.19 15:46 |
274 | [Python] enumerate() | 푸딩뱃살 | 500 | 2021.10.13 14:44 |
273 | [Python] 아나콘다에서 가상 환경 | 푸딩뱃살 | 720 | 2020.11.21 00:26 |
272 | [Python] pip로 설치 때 퍼미션 에러 | 푸딩뱃살 | 1265 | 2020.06.06 17:13 |
271 | [Python] OpenCV 10-3. 이미지 Thresholding - Otsu's Binarizatio | 푸딩뱃살 | 674 | 2020.06.05 14:01 |
270 | [Python] OpenCV 10-2. 이미지 Thresholding - Adaptive Threshold | 푸딩뱃살 | 694 | 2020.06.05 13:58 |
269 | [Python] OpenCV 10-1. 이미지 Thresholding | 푸딩뱃살 | 578 | 2020.06.05 13:56 |
268 | [Python] OpenCV 9-2. 색 추적 | 푸딩뱃살 | 772 | 2020.06.02 23:29 |
267 | [Python] OpenCV 9-1. 색공간 바꾸기 | 푸딩뱃살 | 639 | 2020.06.02 23:27 |
266 | [Python] OpenCV 8-3. 이미지 비트 연산 | 푸딩뱃살 | 529 | 2020.06.02 23:21 |