작성자 | LingTone | ||
---|---|---|---|
작성일 | 2018-06-15 13:15:13 KST | 조회 | 629 |
제목 |
C++ 간만에 하려니까 어렵다
|
#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;
}
밑에 연산자 오버로딩 하는 거 올라온 글 보고 조금 고쳐봤읍니다
|
||
|
|
||
|
|
||
|
© PlayXP Inc. All Rights Reserved.