π νΌμνλ 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μΌλ‘ μ€μ λ ν΄λμ€μ κ°λ€κ³ λ³Ό μ μλ€.
Header
#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- 보λ¬κ°κΈ°
Comments