열혈 c++ 프로그래밍을 보고 요약정리합니다.
int num = 20;
int num(20);
SoSimple sim1(15, 20);
SoSimple sim2 = sim1;
explicit SoSimple(const SoSimple ©) : num1(copy.num1), num2(copy.num2)
{
//empty!!
}
AAA obj = 3; //AAA obj(3) 으로 변환
Person(const Person & person)
{
int len = ::strlen(person.name) + 1;
name = new char[len];
strcpy(name, person.name);
age = person.age;
}
int num1 = num2;
num1이라는 이름의 메모리 공간을 할당과 동시에 num2에 저장된 값으로 초기화시키는 문장이다.
int SimpleFunc(int n)
{
...
}
int main(void)
{
int num = 10;
SimpleFunc(num);
}
SimpleFunc 함수가 호출되는 순간에 매개변수 n이 할당과 동시에 변수 num에 저장되어 있는 값으로 초기화된다.
int SimpleFunc(int n)
{
...
return n;
}
int main(void)
{
int num = 10;
cout << SimpleFunc(num) << endl;
}
반환하는 순간 메모리 공간이 할당되면서 동시에 초기화된다.
Temporary(10));
const Temporary &ref = Temporary(300); //바로 소멸되지 x
복사 생성자를 정의
class Account{
private:
int id;
int balance;
char *name;
Account(int id, int balance, char *name) {
this->id = id;
this->balance = balance;
this->name = new char[::strlen(name) + 1];
::strcpy(this->name, name);
}
public:
Account() {
}
Account(const Account & account)
{
this->id = account.id;
this->balance = account.balance;
this->name = new char[strlen(account.name) + 1];
strcpy(this->name, account.name);
}
static Account Of(int _id, int _balance, char* _name) {
return Account(_id, _balance, _name);
}
bool IsSame(int id) {
return this->id == id;
}
void Deposit(int money) {
this->balance += money;
}
void Withdraw(int money) {
if (this->balance < money) {
cout << "잔액부족\n";
return;
}
this->balance -= money;
cout << "출금완료\n";
}
void ShowInfo() {
cout << "계좌ID: " << id << "\n";
cout << "이 름: " << name << "\n";
cout << "잔 액: " << balance << "\n";
}
Account* Copy() {
return new Account(this->id, this->balance, this->name);
}
};