(adsbygoogle = window.adsbygoogle || []).push({ google_ad_client: "ca-pub-8242763509535969", enable_page_level_ads: true }); 'code/C++' 카테고리의 글 목록 :: 깜냥깜냥
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<iostream>
using namespace std;
 
const int MAX = 10;
 
template <typename T>
class Memory {
 
protected:
    T *data;
    int top;
    int front;
    T a;
public:
    Memory() {
        data = new T[10];
        top = 0;
    }
 
    void push(T num) {
        if (top == MAX) {
            cout << "FULL!!" << endl;
            return;
        }
        else {
            data[top++] = num;
        }
    }
    virtual T pop() = 0;
    virtual ~Memory() {
        delete[]data;
        cout << "소멸자 함수" << endl;
    }
};
template <typename T>
class Stack :public Memory<T> {
public:
    T pop() {
        if (top == 0) {
            cout << "EMPTY!!!" << endl;
            return 0;
        }
        else {
            return data[--top];
        }
    }
};
 
template <typename T>
class Queue :public Memory<T> {
 
public:
    Queue() {
        front = 0;
    }
    T pop() {
        if (top == front) {
            cout << "EMPTY!!!" << endl;
            return 0;
        }
        else {
            if (front == MAX) {
                front = 0;
            }
            return data[(front++)];
        }
    }
};
 
void main() {
 
    Memory<double> *p;
    Stack<double> s;
    Queue<double> q;
    
 
    int num;
    double a;
 
    do {
        cout << "1.stack" << "\t" << "2.queue" << "\t" << "3.Quit" << endl;
        cin >> num;
 
        if (num == 1) {
            p = &s;
        }
        else if (num == 2) {
            p = &q;
 
        }
        else if (num == 3) {
            continue;
        }
        else {
            cout << "Not num" << endl;
            cout << "Bye" << endl;
            num = 3;
            continue;
        }
 
        cout << "1.push" << "\t" << "2.pop" << endl;
        cin >> num;
 
        if (num == 1) {
 
            cout << "Data input : ";
            cin >> a;
            p->push(a);
 
 
        }
        else {
            cout << " output Data : " << p->pop() << endl;
        }
    } while (num != 3);
}


'code > C++' 카테고리의 다른 글

[C++] 템플릿  (0) 2017.04.20
setw() 함수  (0) 2017.04.20
[c++] 연산자 함수  (0) 2017.04.20
[c++] private 상속  (0) 2017.04.20
[C++] protected 상속  (0) 2017.04.20
오버라이딩을 사용하였을 때
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
#include<iostream>
using namespace std;
 
void change(int &a, int &b) {
    int c = a;
    a = b;
    b = c;
}
void change(char &a, char &b) {
    char c= a;
    a = b;
    b = c;
 
}
 
void change(double &a, double &b) {
    double c = a;
    a = b;
    b = c;
}
void main() {
    int a = 1, b = 2;
    char c = '3', d = '4';
    double e = 5.3, f = 4.7;
 
    change(a, b);
    cout << a << "\t" << b << endl;
 
    change(c, d);
    cout << c << "\t" << d << endl;
 
    change(e, f);
    cout << e << "\t" << f << endl;
}



템플릿을 사용 했을 때

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;
 
//template<class T>//T는 미결정 타입입니다. 아무거나 적어도 상관없습니다. class는 구표준입니다.
template<typename T>
void change(T &a, T &b) {
    T c = a;
    a = b;
    b = c;
}
void main() {
    int a = 1, b = 2;
    char c = '3', d = '4';
    double e = 5.3, f = 4.7;
 
    change(a, b);
    cout << a << "\t" << b << endl;
 
    change(c, d);
    cout << c << "\t" << d << endl;
 
    change(e, f);
    cout << e << "\t" << f << endl;
}

오버라이딩과 다르게 같은 함수를 다른 타입도 쓸 수 있습니다.


템플릿을 밑으로 정의 했을 때
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
#include<iostream>
using namespace std;
 
template<typename T>
void change(T &a, T &b);
 
//template<class T>//T는 미결정 타입입니다. 아무거나 적어도 상관없습니다. class는 구표준입니다.
void main() {
    int a = 1, b = 2;
    char c = '3', d = '4';
    double e = 5.3, f = 4.7;
 
    change(a, b);
    cout << a << "\t" << b << endl;
 
    change(c, d);
    cout << c << "\t" << d << endl;
 
    change(e, f);
    cout << e << "\t" << f << endl;
}
template<typename T>
void change(T &a, T &b) {
    T c = a;
    a = b;
    b = c;
}
 

 


'code > C++' 카테고리의 다른 글

[C++] Stack&Queue (예외처리, 소멸자, 템플릿 포함)  (0) 2017.04.20
setw() 함수  (0) 2017.04.20
[c++] 연산자 함수  (0) 2017.04.20
[c++] private 상속  (0) 2017.04.20
[C++] protected 상속  (0) 2017.04.20

setw()

1.setw()는 C++ 함수이다..
2.setw() #include<iomanip>에 공표되어있는 함수이다.
3.setw()는 데이터가 출력 된 화면을 지정한 수만큼 폭을 정렬해준다.



ex)
1
2
3
4
5
6
7
8
9
10
#include <iostream>  
#include <iomanip>
 
using namespace std;
 
int main () {
  char a[10]={"hello"};
  cout <<setw(10)<< a <<endl;
  return 0;
}
cs

위와 같이 setw(int num)형식을 써준다. cout.setf(ios::left),cout.setf(ios::right)를 써준다면 좌측,우측 정렬을 할 수 있다.

ex)
1
2
3
4
//////////////생략///////////
  cout.setf(ios::left);
  cout <<setw(10)<< a <<endl;
//////////////생략///////////
cs
좌측 정렬

1
2
3
4
//////////////생략///////////
  cout.setf(ios::right);
  cout <<setw(10)<< a <<endl;
//////////////생략///////////
cs

우측 정렬 

'code > C++' 카테고리의 다른 글

[C++] Stack&Queue (예외처리, 소멸자, 템플릿 포함)  (0) 2017.04.20
[C++] 템플릿  (0) 2017.04.20
[c++] 연산자 함수  (0) 2017.04.20
[c++] private 상속  (0) 2017.04.20
[C++] protected 상속  (0) 2017.04.20
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<cstring>
#pragma warning(disable:4996)
using namespace std;
 
class A{
    int a;
public:
    A(int a=1){
        this->a=a;
    }
    void setA(int a){
        this->a=a;
    }
    int getA()const{
        return a;
    }
    /*int operator+(const A &bb){
        return a+bb.a;
    }*/
    int operator+(int a){
        return this->a+a;
    }
    friend ostream& operator<<(ostream &out,const A &aa);
    friend istream& operator>>(istream &in, A &aa);
};
    //int operator+(const A &aa,const A &bb){
        //return aa.getA()+bb.getA();
//}
//int operator+(int a,const A &aa){
        //return a+aa.a;
    //}
ostream& operator<<(ostream &out,const A &aa){
    out<<aa.a;
    return out;
    }
istream& operator>>(istream &in,A &aa){
    in>>aa.a;
    return in;
    }
void main(){
    A aa;
    A bb(9);
    //cout<<aa.getA()+bb.getA()<<endl;
    //cout<<aa+bb<<endl;//aa.operator+(bb)//내부
                      //operator+(aa,bb)//외부
 
 
    //cout<<aa+40<<endl;//멤버함수
    //cout<<40+aa<<endl;//외부함수->friend써야함
 
    cout<<aa;//operator<<(cout,aa);
    cout<<bb;
    cout<<aa<<bb<<endl;//void형이면 aa까지 가고 void<<bb로 인식됨.
 
 
    cin>>aa>>bb;//100,200
    cout<<aa<<bb;
}


'code > C++' 카테고리의 다른 글

[C++] 템플릿  (0) 2017.04.20
setw() 함수  (0) 2017.04.20
[c++] private 상속  (0) 2017.04.20
[C++] protected 상속  (0) 2017.04.20
[C++] public 상속  (0) 2017.04.20

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include<iostream>
#include<string>
#pragma warning(disable:4996)
using namespace std;
 
class A {
    int a1;
protected:
    int b1;
public:
    int c1;
    void setA1(int a1) {
        this->a1 = a1;
    }
    int getA1()const
    {
        return a1;
    }
    void setB1(int b1) {
        this->b1 = b1;
    }
    int getB1()const
    {
        return b1;
    }
    void setC1(int c1) {
        this->c1 = c1;
    }
    int getC1()const
    {
        return c1;
    }
};
class B : private A {
    int a2; ;//a1,b1,c1//b1,c1접근 가능
protected:
    int b2;
public:
    int c2;
    void setA1B(int a1) {
        setA1(a1);
    }
    int getA1B()const
    {
        return getA1();
    }
    void setB1B(int b1) {
        setB1(b1);
    }
    int getB1B()const
    {
        return getB1();
    }
    void setC1B(int c1) {
        setC1(c1);
    }
    int getC1B()const
    {
        return getC1();
    }
    void setA2(int a2) {
        this->a2 = a2;
    }
    int getA2()const
    {
        return a2;
    }
    void setB2(int b2) {
        this->b2 = b2;
    }
    int getB2()const
    {
        return b2;
    }
    void setC2(int c2) {
        this->c2 = c2;
    }
    int getC2()const
    {
        return c2;
    }
 
};
 
class C : private B { //제일 최하위 class
    int a3; //a1,a2,b1,b2,c1,c2 //b2,c2접근 가능
 
protected:
    int b3;
public:
    int c3;
    void setA1C(int a1) {
        setA1B(a1);
    }
    int getA1C()const
    {
        return getA1B();
    }
    void setB1C(int b1) {
        setB1B(b1);
    }
    int getB1C()const
    {
        return getB1B();
    }
    void setC1C(int c1) {
        setC1B(c1);
    }
    int getC1C()const
    {
        return getC1B();
    }
    void setA2C(int a2) {
        setA2(a2);
    }
    int getA2C()const
    {
        return getA2();
    }
    void setB2C(int b2) {
        this->b2 = b2;
    }
    int getB2C()const
    {
        return getB2();
    }
    void setC2C(int c2) {
        this->c2 = c2;
    }
    int getC2C()const
    {
        return getC2();
    }
    void setA3(int a3) {
        this->a3 = a3;
    }
    int getA3()const
    {
        return a3;
    }
    void setB3(int b3) {
        this->b3 = b3;
    }
    int getB3()const
    {
        return b3;
    }
    void setC3(int c3) {
        this->c3 = c3;
    }
    int getC3()const
    {
        return c3;
    }
};
 
void main() {
    C cc; //c1,c2,c3는 접근이 가능
    cc.setA1C(1);
    cc.setB1C(2);
    cc.setC1C(3);
    cc.setA2C(4);
    cc.setB2C(5);
    cc.setC2C(6);
    cc.setA3(7);
    cc.setB3(8);
    cc.setC3(9);
 
    cout << cc.getA1C() << "\t" << cc.getB1C() << "\t" << cc.getC1C() << "\t" << cc.getA2C() << "\t"
        << cc.getB2C() << "\t" << cc.getC2C() << "\t" << cc.getA3() << "\t" << cc.getB3() << "\t" << cc.getC3() << endl;
}


'code > C++' 카테고리의 다른 글

setw() 함수  (0) 2017.04.20
[c++] 연산자 함수  (0) 2017.04.20
[C++] protected 상속  (0) 2017.04.20
[C++] public 상속  (0) 2017.04.20
[C++] 다중 상속  (0) 2017.04.20

+ Recent posts