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
#include <iostream>
#include <string>
using namespace std;
 
 
class Smart{
    int app;
    string color;
 
public:
 
    
    Smart():app(10),color("white"){
        cout<<"Default 생성자"<<endl;
    }
    Smart( int x, string y):app(x),color(y){
        cout<<"매개변수가 있는 생성자"<<endl;
    }
    ~Smart(){
        cout<<"소멸자"<<endl;
    }
    int getApp(){
        return app;
    }//getApp()
 
    string getColor() {
        return color;
    }//getColor()
    void setApp(int num){
        app = num;
    }
    void setColor(string _color){
        color = _color;
    }
    void display(){
        cout<<getApp()<<' '<<getColor()<<endl; 
    }//display()
    
    Smart(Smart& a):app(a.app),color(a.color){
        cout<<"복사생성자"<<endl;
    }
 
 
};
 
 
 
void main(){
    Smart* a  = new Smart[3];
    a->setApp(10);
    a->setColor("pink");
    
    Smart* p = a;
    (p+1)->setApp(20);
    (p+1)->setColor("RED");
 
    (p+2)->setApp(30);
    (p+2)->setColor("BLUE");
 
    forint i =0; i<3; i++,p++){
        cout<<p->getApp()<<' '<<a[i].getApp()<<endl;
        cout<<p->getColor()<<' ' <<a[i].getColor()<<endl;
    }
    //Smart a[3]={Smart(20,"red"), //임시 객체
    //            Smart(30,"pink"),
    //            Smart(120,"blue")};
 
    delete[] a;
}


설정

트랙백

댓글

class 배열

전전컴/C++ 2013. 10. 23. 13:12



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
#include <iostream>
#include <string>
using namespace std;
 
 
class Smart{
    int app;
    string color;
 
public:
 
    
    Smart():app(10),color("white"){
        cout<<"Default 생성자"<<endl;
    }
    Smart( int x, string y):app(x),color(y){
        cout<<"매개변수가 있는 생성자"<<endl;
    }
    ~Smart(){
        cout<<"소멸자"<<endl;
    }
    int getApp(){
        return app;
    }//getApp()
 
    string getColor() {
        return color;
    }//getColor()
 
    void display(){
        cout<<getApp()<<' '<<getColor()<<endl; 
    }//display()
    
    Smart(Smart& a):app(a.app),color(a.color){
        cout<<"복사생성자"<<endl;
    }
 
 
};
 
 
 
void main(){
    
    Smart a[3]={Smart(20,"red"), //임시 객체
                Smart(30,"pink"),
                Smart(120,"blue")};
    Smart *p = a;
    for(int i = 0; i<3; i++,p++){
        a[i].display();
        (p)->display();
    }
 
}


설정

트랙백

댓글

5주차

전전컴/C++ 2013. 10. 23. 12:29

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
#include <iostream>
#include <string>
using namespace std;
 
class Battery{
    int volt;
    string stuff;
public:
    int getVolt(){ return volt; }
    string getstuff(){         return stuff;    }
    void display(){    cout<<volt<<" "<<stuff<< endl;}
    
    Battery():volt(10),stuff("nothing"){ cout<<"Battery default 생성자"<<endl;    } //default 생성자
    Battery(int _volt, string _stuff="nothing"):volt(_volt),stuff(_stuff){ cout<<"Battery 매개변수가 있는 생성자"<<endl; }
    //int _app=10 한다면 overload에 대한 모호성 존재
 
    ~Battery(){    cout<<"Battery 소멸자"<<endl;    }
    Battery(Battery& a){
        volt=a.volt;        stuff=a.stuff;
        cout<<"Battery default 복사 생성자"<<endl;
    }
};
 
class Smart{
    int app;
    string color;
    Battery b; //객체멤버
public:
    int getApp(){ return app; }
    string getcolor(){         return color;    }
    void display(){    cout<<app<<" "<<color <<" "<<b.getVolt()<<" "<<b.getstuff()<< endl;}
    
    Smart():app(10),color("white"){ cout<<"default 생성자"<<endl;    } //default 생성자
    Smart(int _app, string _color, int _volt ,string _stuff):app(_app),color(_color),b(_volt,_stuff) { cout<<"매개변수가 있는 생성자"<<endl; }
    //int _app=10 한다면 overload에 대한 모호성 존재
 
    ~Smart(){    cout<<"소멸자"<<endl;    }
    Smart(Smart& a):b(a.b){
        app=a.app;    color=a.color;
        cout<<"default 복사 생성자"<<endl;
    }
 
};
 
void main(){
    Smart a(30,"red",4,"Li");
    Smart b(a);
    a.display();
    b.display();
 
}



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
#include <iostream>
#include <string>
using namespace std;
 
class Smart{
    int app;
    string color;
    static int count;
public:
    
 
    int getApp() const{ return app; }
    string getcolor() const{         return color;    }
 
    void display(){    cout<<app<<"=="<<color << endl;} 
 
    void talk(){ cout<<"통화"<<endl;}
 
    static int getCount(){    return count; }  
    //정적함수, 객체가 공유, 정적변수와 지역변수만 사용가능
    Smart():app(10),color("white"){ 
        cout<<"default 생성자"<<endl;
        count++;
    } //default 생성자
    Smart(int _app, string _color):app(_app),color(_color) {
        cout<<"매개변수가 있는 생성자"<<endl; 
        count++;
    }
    //int _app=10 한다면 overload에 대한 모호성 존재
 
    ~Smart(){
        cout<<"소멸자"<<endl;    
        count--;
    }
    
    Smart(Smart& a){
        app=a.app;    color=a.color;
        cout<<"default 복사 생성자"<<endl;
    }
    
 
};
 
int Smart::count = 0; //정적변수의 초기화는 클래스 밖에서 함
 
void main(){
 
    Smart a(20,"red");
    Smart b;
    cout<<b.getCount()<<endl;
    Smart c(30,"blue");
    cout<<c.getCount()<<endl;
    cout<<Smart::getCount()<<endl;
    a.display(); 
    int d= b.getCount();
 
}


설정

트랙백

댓글





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
#include <iostream>
using namespace std;

void main(){
    int a[][3]={1,3,5,7,9,11};
    
    int* ptr;
    
    for(int i=0; i<2; i++){
        for(int j=0; j<3; j++){
            cout<<a[i][j]<<' ';
        }
        cout<<endl;
    }
    ptr = a[0];
    cout<<*ptr<<endl;
    ptr++;
    cout<<*ptr<<endl;
    ptr++;
    cout<<*ptr<<endl;


    cout<<"*a[0]="<<*a[0]<<endl;
    cout<<"*a[1]="<<*a[1]<<endl;
    
    //**a=*a[0]
    cout<<"**a="<<**a<<endl;
    
    cout<<"포인터 상수를 이용하여 값 표시"<<endl;
    for(int i=0; i<2; i++){
        for(int j=0; j<3; j++){
            cout<<*(a[i]+j)<<' ';  //포인터 상수를 이용하여 값 표시
        }
        cout<<endl;
    }

    for(int i=0; i<2; i++){
        for(int j=0; j<3; j++){
            cout<<*(*(a+i)+j)<<' ';  //포인터 상수를 이용하여 값 표시
        }
        cout<<endl;
    }

    cout<<"-"<<**(a+1)<<"-"<<endl; //**(a+1)== **(a[0]+1) -->7가 출력


    int (*p)[3]= a;  //int (*p)[열크기]

    cout<<"size "<<sizeof(p[3])<<endl;
    for(int i=0; i<2; i++,p++){  //---->p++하면 12번지 증가함
        cout<<p<<endl;
        for(int j=0; j<3; j++){
            cout<<*((*p)+j)<<' ';
        }
        cout<<endl;
    }


    //char* c[행크기] = {"Hello", "Hi"};
    char* c[2] = {"Hello", "Hi"};
    cout<<c[1]<<endl;

    int *aa;
    aa=new int;
    *aa =5;
    cout<<*aa+1    <<endl;
    cout<<*aa <<endl;
    delete aa;

}


설정

트랙백

댓글

동적2차원 배열

전전컴/C++ 2013. 10. 23. 00:42

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

void main(){
    char **temp = new char*[5];
    for(int i=0; i<5; i++){
        temp[i]= new char[3];
    }

    temp[4][2] = 5;

    for(int i=0; i<5 ; i++){
        delete[] temp[i];
    }
    delete[] temp;

}


설정

트랙백

댓글