πŸ“š ν˜Όμžν•˜λŠ” C++ 곡뢀 -2-

πŸ“š ν˜Όμžν•˜λŠ” C++ 곡뢀 -2-

πŸ‘€ C++

μœ„ν‚€λ…μŠ€λ₯Ό 주둜 μ°Έμ‘°ν•˜μ—¬ κ³΅λΆ€ν–ˆλ‹€.
이전 κΈ€ πŸ“š ν˜Όμžν•˜λŠ” C++ 곡뢀 -1- λ³΄λŸ¬κ°€κΈ°

Structure

#include<iostream>

using std::cout;
using std::cin;
using std::endl;
    
struct account{
    enum{
        NAME_LEN = 20,
        PW_LEN = 15,
    };
    char accountName[NAME_LEN];
    char accountPassword[PW_LEN];
    int accountNumber;
    int moneyInAccount;
    
    void deposit(int depositMoney);
    
    void changeName(){
        cin >> accountName;
    }
    
    inline void changePassword(){
        cin >> accountPassword;
    }
};

void showAllInformations(const account& thisAccount){
    cout << thisAccount.accountName << endl;
    cout << thisAccount.accountPassword << endl;
    cout << thisAccount.accountNumber << endl;
    cout << thisAccount.moneyInAccount << endl;
}

void account::deposit(int money){
    moneyInAccount += money;
}

int main(void){
    account seunghun { "seunghun", "1234", 123456, 2100000000 };
    showAllInformations(seunghun);
    seunghun.changeName();
    showAllInformations(seunghun);
    seunghun.changePassword();
    showAllInformations(seunghun);
    seunghun.deposit(1);
    showAllInformations(seunghun);
    return 0;
}

seunghun
1234
123456
2100000000
jaehyeon
jaehyeon
1234
123456
2100000000
987654321
jaehyeon
987654321
123456
2100000000
jaehyeon
987654321
123456
2100000001

ꡬ쑰체λ₯Ό μ„ μ–Έν•˜μ—¬ μ‚¬μš©ν–ˆλ‹€. jaehyeon을 μž…λ ₯ν•΄ 이름이 jaehyeon으둜 λ°”λ€Œμ—ˆκ³ , 987654321을 μž…λ ₯ν•΄ λΉ„λ°€λ²ˆν˜Έλ₯Ό λ°”κΎΈμ—ˆλ‹€.

Class

#include<iostream>
#include<string.h>

using std::cout;
using std::cin;
using std::endl;
    
class account{
private :
    enum{
        NAME_LEN = 20,
        PW_LEN = 15,
    };
    char accountName[NAME_LEN];
    char accountPassword[PW_LEN];
    int accountNumber;
    int moneyInAccount;

public :
    void createAccount(const char* newName, const char* newPW, int newAN, int newDeposit);
    void deposit(int depositMoney);
    void showAllInformations();
    
    void changeName(){
        cin >> accountName;
    }
    
    inline void changePassword(){
        cin >> accountPassword;
    }
};

void account::showAllInformations(){
    cout << accountName << endl;
    cout << accountPassword << endl;
    cout << accountNumber << endl;
    cout << moneyInAccount << endl;
}

void account::createAccount(const char* newName, const char* newPW, int newAN, int newDeposit){
    strcpy(accountName, newName);
    strcpy(accountPassword, newPW);
    accountNumber = newAN;
    moneyInAccount = newDeposit;
}

void account::deposit(int money){
    moneyInAccount += money;
}

int main(void){
    account seunghun;
    seunghun.createAccount("seunghun", "1234", 123456, 2100000000);
    seunghun.showAllInformations();
    seunghun.changeName();
    seunghun.showAllInformations();
    seunghun.changePassword();
    seunghun.showAllInformations();
    seunghun.deposit(1);
    seunghun.showAllInformations();
    return 0;
}

seunghun
1234
123456
2100000000
jaehyeon
jaehyeon
1234
123456
2100000000
987654321
jaehyeon
987654321
123456
2100000000
jaehyeon
987654321
123456
2100000001

ꡬ쑰체와 λ™μΌν•˜κ²Œ μž‘λ™ν•œλ‹€. C++μ—μ„œ ꡬ쑰체와 클래슀의 차이점은 μ ‘κ·Όμ œν•œμžμ˜ μœ λ¬΄μ΄λ‹€. C++μ—μ„œ 클래슀의 μ ‘κ·Όμ œν•œμžλŠ” λ‹€μŒκ³Ό 같이 λ™μž‘ν•œλ‹€.

μ ‘κ·Όμ œν•œμž public protected private
μ™ΈλΆ€ μ ‘κ·Ό O X X
상속관계 μ ‘κ·Ό O O X
λ‚΄λΆ€ μ ‘κ·Ό O O O

κ΅¬μ‘°μ²΄λŠ” μ ‘κ·Όμ œν•œμžκ°€ λͺ¨λ‘ public으둜 μ„€μ •λœ ν΄λž˜μŠ€μ™€ κ°™λ‹€κ³  λ³Ό 수 μžˆλ‹€.

#ifndef HEADER_H
#define HEADER_H

//WRITE HEADER CODE HERE

#endif

ν—€λ”νŒŒμΌ μž‘μ„±μ‹œ ν—€λ”νŒŒμΌμ΄ μ—¬λŸ¬λ²ˆ include되면 μ½”λ“œκ°€ κΌ¬μ΄κ²Œλœλ‹€. λ”°λΌμ„œ ν—€λ”νŒŒμΌμ˜ 쀑볡 includeλ₯Ό ν”Όν•˜κΈ°μœ„ν•΄ ν—€λ”νŒŒμΌμ— μ „μ²˜λ¦¬λ₯Ό ν•΄μ£ΌλŠ”κ²ƒμ΄ μΌλ°˜μ μ΄λ‹€. μœ„μ™€κ°™μ΄ μ „μ²˜λ¦¬λ₯Ό ν•΄μ£Όλ©΄ ν•΄λ‹Ή ν—€λ”νŒŒμΌμ΄ includeλ˜μ—ˆλŠ”μ§€ 확인 ν•˜κ³  μ»΄νŒŒμΌν•œλ‹€. inlineν•¨μˆ˜λŠ” 헀더 λ‚΄μ—μ„œ μ •μ˜λ˜μ–΄μ•Όν•œλ‹€.

Const

//IN HEADER
void func() const;

//IN CPP
void func() const{
        ...
}

ν—€λ”μ—μ„œ ν•¨μˆ˜λ₯Ό const둜 μ„ μ–Έν•˜κ²Œλ˜λ©΄, ν•¨μˆ˜λŠ” ν•΄λ‹Ή 객체의 λ©€λ²„λ³€μˆ˜λ₯Ό μ‘°μ •ν•  수 μ—†μœΌλ©°, λ‚΄λΆ€μ—μ„œ λ‹€λ₯Έ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•΄μ„œ κ°„μ ‘μ μœΌλ‘œ 멀버 λ³€μˆ˜μ˜ 값을 λ³€κ²½ν•  μœ„ν—˜μ΄ μžˆκΈ°λ•Œλ¬Έμ— ν•¨μˆ˜ λ‚΄μ—μ„œ 호좜 κ°€λŠ₯ν•œ ν•¨μˆ˜λ„ constν•¨μˆ˜λ‘œ μ œν•œλœλ‹€. (Read Only)

Constructor, destructor

#include<iostream>

class Player{
private :
    int number;
    int age;
public :
    Player();
    Player(int newNumber);
    Player(int newNumber, int newAge);
    void setNumber(int newNumber);
    void setAge(int newAge);
    void getNumber() const;
    void getAge() const;
    void showInfo() const;
    ~Player();
};

Player::Player(){
    number = 0;
    age = 20;
}

Player::Player(int newNumber){
    number = newNumber;
    age = 20;
}

Player::Player(int newNumber, int newAge){
    number = newNumber;
    age = newAge;
}

Player::~Player(){
    std::cout << "Destructor Called" << std::endl;
}

void Player::setNumber(int newNumber){
    number = newNumber;    
}

void Player::setAge(int newAge){
    age = newAge;
}

void Player::getNumber() const{
    std::cout << "getNumber" << std::endl;
    std::cout << number << std::endl;
    std::cout << "********" << std::endl;
}

void Player::getAge() const{
    std::cout << "getAge" << std::endl;
    std::cout << age << std::endl;
    std::cout << "********" << std::endl;
}

void Player::showInfo() const{
    std::cout << "showInfo" << std::endl;
    std::cout << number << std::endl;
    std::cout << age << std::endl;
    std::cout << "********" << std::endl;
}

int main(void){
    Player A;
    Player B(10);
    Player C(30, 50);
    A.showInfo();
    A.getAge();
    A.getNumber();
    B.showInfo();
    B.setNumber(999);
    C.showInfo();
    C.setAge(999);
    B.showInfo();
    C.getAge();
    A.~Player();
    A.showInfo();
    B.showInfo();
    C.showInfo();
    return 0;
}

50
****
showInfo
999
20
****
getAge
999
****
Destructor Called
showInfo
0
20
****
showInfo
999
20
****
showInfo
30
999
****
Destructor Called
Destructor Called
Destructor Called

μƒμ„±μž μ•žμ— ~κ°€ λΆ™μ€κ²Œ μ†Œλ©Έμžκ°€ λœλ‹€.

This

#include<iostream>

using std::cout;
using std::endl;

class House{
private:
    int squareMeter;
    int level;
public :
    House(){
        squareMeter = 30;
        level = 1;
    }
    House(int newSquare, int newLevel){
        this->squareMeter = newSquare;
        this->level = newLevel;
    }
    void showInfo(){
        cout << "address : " << this << endl;
        cout << "square meter : " << this->squareMeter << endl;
        cout << "level : " << this->level << endl;
    }
};

int main(void){
    House houseA;
    House houseB(100, 4);
    House& houseC = houseB;
    houseA.showInfo();
    houseB.showInfo();
    houseC.showInfo();
    cout << "Manual Address of house A : " << &houseA << endl;
    cout << "Manual Address of house B : " << &houseB << endl;
    cout << "Manual Address of house C : " << &houseC << endl;
    return 0;
}

address : 0x7fff4525cf70
square meter : 30
level : 1
address : 0x7fff4525cf78
square meter : 100
level : 4
address : 0x7fff4525cf78
square meter : 100
level : 4
Manual Address of house A : 0x7fff4525cf70
Manual Address of house B : 0x7fff4525cf78
Manual Address of house C : 0x7fff4525cf78

클래슀 λ‚΄λΆ€ μ—μ„œ thisλ₯Ό μ‚¬μš©ν•˜λ©΄ κ·Έ ν•¨μˆ˜κ°€ 호좜된 객체λ₯Ό 가리킨닀.

Self-reference return

#include<iostream>

using std::cout;
using std::endl;

class object{
private : 
    int value;
public :
    object(int newValue){
        this->value = newValue;
    }
    
    object& selfAdd(int operandValue){
        this->value = this->value + operandValue;
        return *this;
    }
    
    void showInfo(){
        cout << "ADDRESS : " << this << endl;
        cout << "VALUE : " << this->value <<endl;
    }
};

int main(void){
    object a(3);
    a.showInfo();
    object& b = a.selfAdd(5);
    a.showInfo();
    b.showInfo();
    a = b.selfAdd(8);
    a.showInfo();
    b.showInfo();
    return 0;
}

ADDRESS : 0x7ffe362b71d4
VALUE : 3
ADDRESS : 0x7ffe362b71d4
VALUE : 8
ADDRESS : 0x7ffe362b71d4
VALUE : 8
ADDRESS : 0x7ffe362b71d4
VALUE : 16
ADDRESS : 0x7ffe362b71d4
VALUE : 16

슀슀둜의 referenceλ₯Ό λ°˜ν™˜ν•΄μ„œ 전달할 수 μžˆλ‹€.

Copy constructor

#include<iostream>

using std::cout;
using std::endl;

class object{
private : 
    int value;
public :
    object(){
        this->value = 0;
    }
    object(int newValue){
        this->value = newValue;
    }
    object(const object &target){
        this->value = target.value + 1;
    }
    void showInfo(){
        cout<< "adrs : " << this << endl;
        cout<< "vlue : " << this->value<< endl;
    }
};

int main(void){
    object A(3);
    object B = object();
    object C(A);
    object D;
    D = B;
    A.showInfo();
    B.showInfo();
    C.showInfo();
    D.showInfo();
    return 0;
}

adrs : 0x7fff1ee6fb20
vlue : 3
adrs : 0x7fff1ee6fb24
vlue : 0
adrs : 0x7fff1ee6fb28
vlue : 4
adrs : 0x7fff1ee6fb2c
vlue : 0

μƒμ„±μ‹œ λ‹€λ₯Έ 객체λ₯Ό λŒ€μž…ν•˜κ±°λ‚˜, μƒμ„±μžμ˜ 인자둜 λ‹€λ₯Έ 객체둜 λ„£μ–΄μ£Όλ©΄ 볡사생성을 ν•  수 μžˆλ‹€. 볡사생성은 λ ˆνΌλŸ°μŠ€μ™€ 달리 λ‹€λ₯Έ λ©”λͺ¨λ¦¬ 곡간에 객체가 λ³΅μ‚¬λ˜λŠ”κ²ƒμ΄λ‹€. μ•”λ¬΅μ μœΌλ‘œλŠ” λ‹¨μˆœνžˆ 볡사가 μ΄λ€„μ§€μ§€λ§Œ, λͺ…μ‹œμ μœΌλ‘œ ν–‰μœ„λ₯Ό μΆ”κ°€μ μœΌλ‘œ μ •μ˜ν•΄μ€„ μˆ˜λ„ μžˆλ‹€.

Explicit

#include<iostream>

using std::cout;
using std::endl;

class object{
private :
    int value;
public : 
    explicit object(int newValue){
        value = newValue;
    }
    void showInfo(){
        cout << "adrs : " << this << endl;
        cout << "vlue : " << this->value << endl;
    }
};

int main(void){
    object A(3);                    //NORMAL CONSTRUCTOR
    A.showInfo();
    object B(A);                    //COPY CONSTRUCTOR
    B.showInfo();
    object C = object(10);          //ANOTHER EXPRESSION
    C.showInfo();
    object D = (object)12;          //EXPLICIT
    D.showInfo();
    /*
    object E = 10;                   //IMPLICIT
    E.showInfo();
    */
    return 0;
}

adrs : 0x7ffd89b96010
vlue : 3
adrs : 0x7ffd89b96014
vlue : 3
adrs : 0x7ffd89b96018
vlue : 10
adrs : 0x7ffd89b9601c
vlue : 12

C++μ—μ„œ μƒμ„±μžλŠ” λ‹€μ–‘ν•œ λ°©μ‹μœΌλ‘œ 호좜될 수 μžˆλŠ”λ°, νŠΉμ΄ν•˜κ²Œλ„ (python 처럼) λ³€μˆ˜μ˜ ν˜•νƒœλ‘œ 호좜이 κ°€λŠ₯ν•˜λ‹€. (암묡적 볡사생성) 이런 경우 μ›ν•˜μ§€ μ•ŠλŠ” κ°’μ˜ μžλ£Œν˜•μœΌλ‘œ 객체가 μƒμ„±λ˜λŠ” κ²½μš°κ°€ λ°œμƒν•  수 μžˆλ‹€. 이λ₯Ό λ°©μ§€ν•˜κΈ°μœ„ν•΄μ„œ μƒμ„±μžμ˜ μ•žμ— explicit을 λΆ™μ—¬μ£Όλ©΄ 암묡적인 생성을 λΆˆκ°€λŠ₯ν•˜κ²Œ ν•  수 μžˆλ‹€.

λ‹€μŒ κΈ€ πŸ“š ν˜Όμžν•˜λŠ” C++ 곡뢀 -3- λ³΄λŸ¬κ°€κΈ°

Share: Twitter Facebook
Seunghun Yang's Picture

About Seunghun Yang

Seunghun is undergraduate student at Computer Science Engineering in CNU(Chungnam National University).

Daejeon, South Korea

Comments