본문 바로가기

2학년 1학기/윈도우즈프로그래밍II

20150325 윈도우즈프로그래밍II 4주차

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
 
using std::cout;
using std::endl;
 
class Dog {
private:
    int age;
    double weight;
public:
    int getage(){
        return age;
    }
    void setage(int ninput){
        age = ninput;
    }
    double getweight(){
        return weight;
    }
    void setweight(double ninput){
        weight = ninput;
    }
};
 
int main(void){
 
    Dog Happy;                        // Dog class의 Happy객체 정의
    //  Dog.Age=2;                   // ① Dog는 class
    cout<<"초기화 하지 않고 출력 : "<<Happy.getage()<<endl;    // 초기화 하지 않고 사용 했을 경우
    Happy.setage(3);               // ② Age는 private멤버로 클래스 밖에서 접근 불가
    cout<<"Happy의 나이 : "<<Happy.getage()<<endl;    // ③ Age는 전용멤버로 접근 불가
    Happy.setweight(3.5);
    cout<<"Happy의 몸무게 : "<<Happy.getweight()<<endl;
    return 0;
}
cs


클래스

캡슐화


예제 소스