본문 바로가기
2018 모각코/진도

7 / 31 일 모각코 제 5회 결과

by 데구르르르 2018. 7. 31.
728x90

객체 배열 



#include <iostream>
#include <cstring>

usingnamespacestd;

classPerson{
private:
    char* name;
    intage;
public:
    Person(char*myname , intmyage){
        intlen = strlen(myname)+1;
        name=newchar[len];
        strcpy(name,myname);
        age= myage;
    }
    Person(){
        name= NULL;
        age=0;
        cout<< "called Person()"<<endl;
    }
    voidSetPersonInfo(char* myname , intmyage){
        name= myname;
        age= myage;
    }
    voidShowPersonInfo() const{
        cout<< "이름: "<< name << ",";
        cout<< "나이: "<< age << endl;
    }
    ~Person(){
        delete[] name;
        cout<< "called destructor!"<< endl;
    }
};

intmain(void){

    Personparr[3];
    charnamestr[100];
    char* strptr;
    intage;
    intlen;
    
    for(inti=0; i<3; i++){
        cout<< "이름: ";
        cin>> namestr;
        cout<< "나이: ";
        cin>> age;
        len =strlen(namestr)+1;
        strptr = newchar[len];
        strcpy(strptr,namestr);
        parr[i].SetPersonInfo(strptr, age);
    }
    
    parr[0].ShowPersonInfo();
    parr[1].ShowPersonInfo();
    parr[2].ShowPersonInfo();
    return0;
}


called Person()
called Person()
called Person()
이름: 한지수
나이: 21
이름: 양은정
나이: 31
이름: 이한영
나이: 34
이름: 한지수,나이: 21
이름: 양은정,나이: 31
이름: 이한영,나이: 34
called destructor!
called destructor!
called destructor!
Program ended with exit code: 0



객체 포인터 배열



#include <iostream>
#include <cstring>

usingnamespace std;

class Person{
private:
    char* name;
    intage;
public:
    Person(char*myname , intmyage){
        intlen = strlen(myname)+1;
        name=newchar[len];
        strcpy(name,myname);
        age= myage;
    }
    Person(){
        nameNULL;
        age=0;
        cout<< "called Person()"<<endl;
    }
    voidSetPersonInfo(char* myname , intmyage){
        name= myname;
        age= myage;
    }
    voidShowPersonInfo() const{
        cout<< "이름: "<< name << ",";
        cout<< "나이: "<< age << endl;
    }
    ~Person(){
        delete[] name;
        cout<< "called destructor!"<< endl;
    }
};

int main(void){
    Person* parr[3];
    charnamestr[100];
    intage;
    
    for(inti=0; i<3; i++){
        cout<< "이름: ";
        cin>> namestr;
        cout<< "나이: ";
        cin>> age;
        
        parr[i] = newPerson(namestr,age);
    }
    
    parr[0] -> ShowPersonInfo();
    parr[1] -> ShowPersonInfo();
    parr[2] -> ShowPersonInfo();
    deleteparr[0];
    deleteparr[1];
    deleteparr[2];
    return0;
}


이름안수희
나이31
이름이한오
나이39
이름강인한
나이19
이름: 안수희,나이: 31
이름: 이한오,나이: 39
이름: 강인한,나이: 19
called destructor!
called destructor!
called destructor!
Program ended with exit code: 0



this 포인터의 이해



#include <iostream>
#include <cstring>

usingnamespace std;

class SoSimple{
private:
    intnum;
public:
    SoSimple(intn) : num(n){
        cout<< "num = "<< num<< ",";
        cout<< "address = "<<this<<endl;
    }
    voidShowSimpleDeta(){
        cout<< num<<endl;
    }
    SoSimple* GetThisPointer(){
        returnthis;
    }
};

int main(void){
    SoSimple  sim1(100);
    SoSimple* ptr1 = sim1.GetThisPointer(); // sim1 객체의 주소 값 저장
    cout<< ptr1 << ",";
    ptr1 -> ShowSimpleDeta();
    
    SoSimplesim2(200);
    SoSimple*ptr2 = sim2.GetThisPointer(); // sim2 객체의 주소 값 저장
    cout<< ptr2 << ",";
    ptr2 -> ShowSimpleDeta();

    return 0;
}


num = 100,address = 0x7ffeefbff468
0x7ffeefbff468,100
num = 200,address = 0x7ffeefbff458
0x7ffeefbff458,200
Program ended with exit code: 0



this 포인터의 활용



#include <iostream>

usingnamespacestd;

class TwoNumber{
private:
    intnum1;
    intnum2;
public:
    TwoNumber(intnum1, intnum2){
        this-> num1= num1;
        this-> num2= num2;
    }
    voidShowTowNumber(){
        cout<< this-> num1<< endl;
        cout<< this-> num2<< endl;
    }
};

int main(void){
    TwoNumbertwo (2,4);
    two.ShowTowNumber();
    return0;
}


2
4
Program ended with exit code: 0





예정된 목표량 달성 실패

4-04 까지 완료

728x90

'2018 모각코 > 진도' 카테고리의 다른 글

7 / 31 모각코 제 5회 목표  (0) 2018.07.31
7 / 25 일 모각코 제 4회 결과  (0) 2018.07.25
7 / 25 모각코 제 4회 목표  (0) 2018.07.25
7 /24 모각코 제 3회 결과  (0) 2018.07.24
7 / 24 모각코 제 3회 목표  (0) 2018.07.24

댓글