Last active
December 20, 2015 14:19
-
-
Save yonghanjung/6146024 to your computer and use it in GitHub Desktop.
이론 리뷰
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class 는 Structs 의 확장된 형태로, 함수까지도 담을 수 있다. | |
Object는 Class안에 들어있는 것을 말한다. 즉, int number 에서 int를 class, number를 object로 볼 수 있다. | |
class class_name { | |
access_specifier_1: | |
member1; | |
access_specifier_2: | |
member2; | |
... | |
} object_names; | |
의 형태로 이루어진다. 여기서 Class의 Body에는 data 와 함수가 모두 들어갈 수 있다. 그리고 accessed specifier 가 들어갈 수 있는데, 여기에는 private, public, protected 등이 있다. | |
Private 에서는 같은 class의 다른 member 혹은 friend 함수로만 호출이 된다. public은 그냥 다 호출된다. By default 로 private으로 호출이 되기 때문에, 보통 public 선언을 한다. | |
class CRectangle { | |
int x, y; | |
public: | |
void set_values (int,int); | |
int area (void); | |
} rect; | |
와 같다. 이 Class를 분석하면, 이는 4개의 Member를 가지고 있고, 두 개의 int member x,y는 private, 그리고 public 은 함수를 가지고 있다. 선언만 했고, 아직 정의는 안 했다. | |
rect.set_values (3,4); | |
myarea = rect.area(); | |
이렇게 호출할 수 있다. | |
// classes example | |
#include <iostream> | |
using namespace std; | |
class CRectangle { | |
int x, y; | |
public: | |
void set_values (int,int); | |
int area () {return (x*y);} | |
}; | |
void CRectangle::set_values (int a, int b) { | |
x = a; | |
y = b; | |
} | |
int main () { | |
CRectangle rect; | |
rect.set_values (3,4); | |
cout << "area: " << rect.area(); | |
return 0; | |
} | |
를 분석하자. 여기서 ::는 어떻게 사용되었을까? 이는 Class 바깥에서, Class 속을 채우기 위해 밖에서 안에 접속하는 열쇠와 같은 느낌으로 쓰인다. 즉, Set value를 바깥에서 정의하기 위해, :: 를 사용한 것이다. CRectangle이라는 집을 두들기고, 열쇠를 통해서 set_value에 접속한 것이다. 접속했고, 그 집의 금고에 있는 private value x,y에 바깥에서 가져온 value a,b를 넣어두었다. | |
함수가 rect.set_value(3,4)로 호출되었을 때, 이 바깥에서 가져온 함수는 위와 같은 과정을 거쳐 집 안 금고에 들어가고 계산이 되어 return 값을 뱉는다. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class myclass{ | |
private: | |
int a; | |
public: | |
void set_a (int num){ | |
a = num; | |
} | |
int get_a(){ | |
return a; | |
} | |
}; | |
int main(){ | |
myclass ob1, ob2; | |
ob1.set_a(10); | |
ob2.set_a(99); | |
// cout << ob1.a << endl; | |
cout << ob1.get_a() << endl; | |
cout << ob2.get_a() << endl; | |
cout << endl; | |
return 0; | |
} | |
에서, Private 에 선언된 변수는 같은 Class 내에서만 호출이 가능하다. | |
위의 함수의 알고리즘은 다음과 같다. | |
1. myclass 라는 Class 를 정의했다. | |
2. ob1, ob2 라는 myclass type 변수를 선언했다. (int haha 라는 int type 변수 선언과 같다.) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// classes example | |
#include <iostream> | |
using namespace std; | |
class CRectangle { | |
int x, y; | |
public: | |
void set_values (int,int); | |
int area () {return (x*y);} | |
}; | |
void CRectangle::set_values (int a, int b) { | |
x = a; | |
y = b; | |
} | |
int main () { | |
CRectangle rect; | |
rect.set_values (3,4); | |
cout << "area: " << rect.area(); | |
cout << endl; | |
return 0; | |
} | |
1. Class의 Private은 주로 Class 에서 처리하는 모수가 들어간다. 그리고 public 은 class에서 처리하는 함수를 선언한다. | |
2. Classname::classfunction 의 형식으로 각 함수를 정의한다. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct structure_name { | |
member_type1 member_name1; | |
member_type2 member_name2; | |
member_type3 member_name3; | |
. | |
. | |
} object_names; | |
의 형태를 갖는다. 즉, 하나의 상자에 여러 타입의 변수를 집어넣을 때 사용한다. | |
struct product { | |
int weight; | |
float price; | |
} apple, banana, melon; 로 예시를 들 수 있다. 이 경우, struct type의 변수는 apple, banana, melon 3개가 된다. | |
호출은 다음과 같이 한다. | |
apple.weight | |
apple.price | |
banana.weight | |
banana.price | |
melon.weight | |
melon.price | |
포인터도 설정할 수 있다. | |
struct movies_t { | |
string title; | |
int year; | |
}; | |
movies_t amovie; | |
movies_t * pmovie; | |
pmovie = &amovie; | |
pmovie -> year 로 movie_t.year를 호출할 수 있다. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
headers = { | |
# Dictionary # | |
} | |
request = urllib2.Request(url_needed_login , headers = headers) | |
contents = urllib.urlopen(request).read() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <ctime> | |
#include <cstring> | |
#include <cstdlib> | |
#include <vector> | |
#include <algorithm> | |
#include <cstdio> | |
#include <cmath> | |
#include <sstream> | |
#include <fstream> | |
using namespace std; | |
struct Employee { | |
char name[10]; | |
char personID[20]; | |
char address[30]; | |
int salary; | |
}; | |
// 전역함수 | |
void setAddress(Employee &, char *); | |
void setSalary(Employee &, int); | |
void display(const Employee &); | |
int main() { | |
Employee hong = {"Hong", "1", "천안시 동남구", 500 }; | |
Employee park = {"Park", "2", "서울시 강북구", 1000}; | |
display(hong); | |
display(park); | |
setSalary(park, 200); | |
display(hong); | |
display(park); | |
} | |
void setAddress(Employee &emp, char * _address) { // 주소를 수정하는 함수 | |
strcpy(emp.address, _address); | |
} | |
void setSalary(Employee &emp, int _salary) { // 급여를 수정하는 함수 | |
emp.salary = _salary; | |
} | |
void display(const Employee &emp) { | |
cout << emp.name << ", " << emp.personID << ", " << | |
emp.address << ", " << emp.salary << "만원" << endl; | |
} | |
알고리듬은 다음과 같다. | |
1. Struct 를 통해서 직원의 정보를 저장하는 구조체를 만들었다. | |
2. 각각 변수를 수정하는 함수를 만들고 정의했다. | |
2-1. 함수는, 구조체를 담고 있는 그릇의 주소를 공통적으로 모수로 담는다. | |
2-2. char 의 경우에는 왜 포인터로 변수를 선언했냐하면, 배열로 선언하는 것과 같기 때문이다. 즉, 주소가 들어오면, 이를 받아서, 구조체의 주소에 저장한다. | |
** 여기서 중요한 것은 구조체는 포인터로 호출할 수 있다는 것이다. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <ctime> | |
#include <cstring> | |
#include <cstdlib> | |
#include <vector> | |
#include <algorithm> | |
#include <cstdio> | |
#include <cmath> | |
#include <sstream> | |
#include <fstream> | |
using namespace std; | |
// 사원을 표현하는 새로운 구조체 자료형 | |
struct Employee { | |
char name[10]; | |
char personID[20]; | |
char address[30]; | |
int salary; | |
//구조체 안에 자료와 함수가 공존 | |
void setAddress(char * _address) { // 주소 수정 | |
strcpy(address, _address); | |
} | |
void setSalary(int _salary) { // 급여 변경 | |
salary = _salary; | |
} | |
void display() { | |
cout << name << ", " << personID << ", " << | |
address << ", " << salary << "만원" << endl; | |
} | |
}; | |
int main() { | |
Employee hong = {"Hong", "1", "천안시 동남구", 500 }; | |
Employee park = {"Park", "2", "서울시 강북구", 1000}; | |
cout << hong.name << endl; // hong의 멤버 변수 | |
hong.display(); // hong의 멤버 함수 | |
park.display(); | |
park.setSalary(200); | |
hong.display(); | |
park.display(); | |
cout << endl; | |
return 0; | |
} | |
알고리듬 | |
1. 구조체에 이와 관련한 함수를 같이 선언 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
int *input; | |
stacksize = (user로부터 입력받는다) | |
input = (int*)malloc(sizeof(int)*stacksize) | |
<포인터> | |
사실, 포인터는 우리가 앞에서 보았던 int 나 char 변수들과 다른 것이 전혀 아닙니다. | |
포인터도 '변수' 입니다. int 형 변수가 정수 데이터, float 형 변수가 실수 데이터를 보관했던 것 처럼, 포인터도 | |
특정한 데이터를 보관하는 '변수' 입니다. 그렇다면 포인터는 무엇을 보관하고 있을 까요? 주소값을 저장합니다. | |
그리고 포인터에도 type이 있다. 따라서 type선언을 해야 한다. | |
배열은 같은 Type의 변수를 갖는 값을 메모리에 일정 간격으로 저장하는 변수 | |
포인터는 주소값을 값으로 갖는 변수 | |
예를들어 포인터 P가 변수값 a의 주소값을 갖고 싶다면, | |
int *P= &a; | |
혹은 | |
int *p; | |
p = &a; | |
한 마디로, *p 는 주소값을 갖는다고 선언한다. | |
그리고 p는 그 주소의 값을 갖는다. | |
한편, *p는 그 주소가 담는 값을 담는다. | |
그릇안의 사과를 생각하면, | |
*p 는 사과 // p 는 그릇 그 자체를 말한다. | |
int *p; | |
int a; | |
p = &a; | |
*p = 3; | |
은 a와 b모두 3을 갖는다. | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
생성자는 함수 안의 member를 초기화하는 역할을 한다. 꼭 필요한 역할이며, 그것은 다음과 같다. | |
class person{ | |
public: | |
int number; | |
char *name; | |
/*생성자 */ | |
person(){ | |
number = 0; name = "Noname"; | |
} | |
void print(){ | |
cout << "Number: " << number << " Name: " << name << endl; | |
} | |
} | |
이렇게 형성되는 생산자를 default 생산자라고 한다. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment