PlayXP

새로운 사이트를 준비하는 동안
여러분의 이야기가 이어질 작은 공간을 마련했습니다.

PlayXP 서비스를 준비하며 함께 작업하고 있는 운영진 일러스트

C++ 간만에 하려니까 어렵다

조회 수 767

#include <iostream>

#include <cstring>

 

class Account {

private:

    char* name;

    char* number;

    int password;

    int money;

 

public:

    Account(const char* name, const char* number,

            int password, int money) : password(password), money(money) {

        int lenName = (int)strlen(name);

        int lenNumber = (int)strlen(number);

        

        this->name = new char[lenName + 1];

        this->number = new char[lenNumber + 1];

        

        strcpy(this->name, name);

        strcpy(this->number, number);

    }

    

    Account& operator=(const Account& ref) {

        delete[] name;

        delete[] number;

        

        int lenName = (int)strlen(ref.name);

        int lenNumber = (int)strlen(ref.number);

        

        this->name = new char[lenName + 1];

        this->number = new char[lenNumber + 1];

        

        strcpy(this->name, ref.name);

        strcpy(this->number, ref.number);

        

        this->password = ref.password;

        this->money = ref.money;

        

        return *this;

    }

    

    void ShowInfo() {

        printf("이름: %s\n", name);

        printf("번호: %s\n", number);

        printf("비밀번호: %d\n", password);

        printf("금액: %d\n", money);

    }

    

    ~Account() {

        delete[] name;

        delete[] number;

    }

};

 

int main() {

    Account A("홍길동", "333", 4321, 500);

    Account B("김복남", "777", 1234, 1200);

    

    A = B;

    

    A.ShowInfo();

    std::cout << "\n";

    B.ShowInfo();

    

    return 0;

}

 

밑에 연산자 오버로딩 하는 거 올라온 글 보고 조금 고쳐봤읍니다

목록으로

댓글

댓글을 불러오는 중...