π νΌμνλ C++ κ³΅λΆ -3-
π C++
μν€λ
μ€λ₯Ό μ£Όλ‘ μ°Έμ‘°νμ¬ κ³΅λΆνλ€.
μ΄μ κΈ π νΌμνλ C++ κ³΅λΆ -2- 보λ¬κ°κΈ°
Shallow copy
#include<iostream>
#include<cstring>
using std::cout;
using std::endl;
class student{
private :
char* name;
int age;
public :
student(){
name = "";
age = 19;
}
student(char* newName, int newAge){
int len = strlen(newName) + 1;
name = new char[len];
strcpy(name, newName);
age = newAge;
}
/*
student(const student& copy){
int len = strlen(copy.name) + 1;
name = new char[len];
strcpy(name, copy.name);
age = copy.age;
}
*/
void changeName(char* newName){
delete [] name;
int len = strlen(newName) + 1;
name = new char[len];
strcpy(name, newName);
}
void showInfo(){
cout << "NAME : " << name << endl;
cout << "AGE : " << age << endl;
}
~student(){
delete [] name;
cout << "DESTRUCTOR CALLED" << endl;
}
};
int main(void){
student a("apple", 23);
student b(a);
a.showInfo();
b.showInfo();
a.changeName("lemon");
a.showInfo();
b.showInfo();
return 0;
}
NAME : apple
AGE : 23
NAME : apple
AGE : 23
NAME : lemon
AGE : 23
NAME : lemon
AGE : 23
DESTRUCTOR CALLED
** Error in `./a.outβ: double free or corruption (fasttop): 0x0000000001d8ec20 **
Aborted (core dumped)
볡μ¬μμ±μλ₯Ό μ μν΄μ£Όμ§ μκ³ λ³΅μ¬μμ±μνλ©΄ ν¬μΈν°λ³μμ κ²½μ° μ£Όμλ§ λ³΅μ¬λλ―λ‘ λ³΅μ¬μμ±λ κ°μ²΄μ μλμ κ°μ²΄κ° κ°μ λ©λͺ¨λ¦¬ 곡κ°μ κ°λ¦¬ν€λ μμ볡μ¬κ° μ΄λ€μ§λ€. μμ μ½λμμ 볡μ¬μμ±μλ₯Ό λ°λ‘ μ μ ν΄μ£Όμ§ μμκ³ nameλ³μκ° ν¬μΈν°λ³μμ΄λ―λ‘ κ°μ²΄ aμ bμ nameμ΄ κ°μ 곡κ°μ κ°λ¦¬ν€κ² λκ³ , aκ° μλ©Έλλ©° bμ nameλ ν¨κ» μλ©Έλμ΄ μ€λ₯κ° λ°μν κ²μ΄λ€.
Deep copy
#include<iostream>
#include<cstring>
using std::cout;
using std::endl;
class student{
private :
char* name;
int age;
public :
student(){
name = "";
age = 19;
}
student(char* newName, int newAge){
int len = strlen(newName) + 1;
name = new char[len];
strcpy(name, newName);
age = newAge;
}
student(const student& copy){
int len = strlen(copy.name) + 1;
name = new char[len];
strcpy(name, copy.name);
age = copy.age;
}
void changeName(char* newName){
delete [] name;
int len = strlen(newName) + 1;
name = new char[len];
strcpy(name, newName);
}
void showInfo(){
cout << "NAME : " << name << endl;
cout << "AGE : " << age << endl;
}
~student(){
delete [] name;
cout << "DESTRUCTOR CALLED" << endl;
}
};
int main(void){
student a("apple", 23);
student b(a);
a.showInfo();
b.showInfo();
a.changeName("lemon");
a.showInfo();
b.showInfo();
return 0;
}
NAME : apple
AGE : 23
NAME : apple
AGE : 23
NAME : lemon
AGE : 23
NAME : apple
AGE : 23
DESTRUCTOR CALLED
DESTRUCTOR CALLED
μμ볡μ¬μ μμ μ 볡μ¬μμ±μλ₯Ό μΆκ°λ‘ ꡬννμ¬ κ°μ 볡μ¬νλλ‘ ν΄μ£Όμλ€. 볡μ¬μμ±μμμ nameμ λν΄ μλ‘μ΄ κ³΅κ°μ λ§λ€μ΄ κ°μ λ£μ΄μ£Όλλ‘ νκΈ° λλ¬Έμ μμ볡μ¬μ²λΌ κ°μ λ©λͺ¨λ¦¬κ³΅κ°μ κ°λ¦¬ν€μ§ μκ²λκ³ , aκ° μλ©Έλμ΄λ bμλ λ¬Έμ κ° μκΈ°μ§ μλλ€.
Object as function parameter
#include<iostream>
using std::cout;
using std::endl;
class data{
private :
int value;
public :
data(int newValue){
value = newValue;
}
~data(){
cout << "DESTRUCTOR CALLED" << endl;
}
void showData(){
cout << "VALUE : " << this->value << endl;
}
};
void showData_Parameter(data dt){
cout << "SHOW DATA OF PARAMETER" << endl;
dt.showData();
}
int main(void){
data a(3);
a.showData();
showData_Parameter(a);
return 0;
}
VALUE : 3
SHOW DATA OF PARAMETER
VALUE : 3
DESTRUCTOR CALLED
DESTRUCTOR CALLED
ν¨μμ μΈμλ‘ κ°μ²΄λ₯Ό μ λ¬νκ²λλ©΄ κ°μ²΄κ° 볡μ¬λμ΄ μ λ¬λκ³ , ν¨μκ° μ€νλ λ€ μλ©Έμκ° μ€νλλ€. μ μν¨μ showDataλ μΈμλ‘ dataκ°μ²΄λ₯Ό λ°μ dataμ showDataλ₯Ό νΈμΆνκ³ μ’
λ£λλ€. mainμμ μ μν¨μ showDataλ₯Ό νΈμΆνμΌλ―λ‘ κ°μ²΄ aκ° λ³΅μ¬λμ΄ ν¨μκ° μ€νλκ³ μλ©Έμ΄ μΌμ΄λλ€. mainμ΄ μ’
λ£λ λ μλμ aλ μλ©Έλλ―λ‘ μλ©Έμκ° μ΄ 2λ² νΈμΆλλ κ²μ΄λ€.
Const object
#include<iostream>
using std::cout;
using std::endl;
class data{
private :
int value;
public :
data(int newValue){
value = newValue;
}
void add(int operand){
value = value + operand;
}
void showInfo() const{
cout << value << endl;
}
};
int main(void){
data a(3);
const data b(3);
a.showInfo();
b.showInfo();
a.add(3);
//b.add(3).
a.showInfo();
b.showInfo();
return 0;
}
3
3
6
3
constκ°μ²΄λ constν¨μλ§ μ¬μ©κ°λ₯νλ€. constλ‘ μμ±λ κ°μ²΄λ λ©€λ²λ³μμ κ°μ λ°κΏ μ μλ€.
Friend class
#include<iostream>
using std::cout;
using std::endl;
class one;
class two;
class one{
friend class two;
private :
int value;
public :
one(){
value = 1;
}
void friendInfo(two& tw);
void showInfo(){
cout << this->value << endl;
}
};
class two{
private :
int value;
public :
two(){
value = 2;
}
void friendInfo(one& on);
void showInfo(){
cout << this->value << endl;
}
friend class one;
};
void one::friendInfo(two& tw){
cout << tw.value << endl;
}
void two::friendInfo(one& on){
cout << on.value << endl;
}
int main(void){
one a;
two b;
a.showInfo();
b.showInfo();
a.friendInfo(b);
b.friendInfo(a);
return 0;
}
1
2
2
1
friend classλ₯Ό ν΅ν΄ ν΄λμ€μ λ΄λΆ μ΄λμμλ λ€λ₯Έ ν΄λμ€λ₯Ό μΉκ΅¬ ν΄λμ€λ‘ μ μΈν΄μ€ μ μλ€. μ΄λ κ² μΉκ΅¬ ν΄λμ€λ₯Ό μ μΈν΄μ£Όλ©΄ ν΄λΉ ν΄λμ€μ privateμ μ κ·Όν μ μκ²λλ€..!
Friend method
#include<iostream>
#include<cstring>
using std::cout;
using std::endl;
class kitty{
private :
char * name;
int age;
public :
kitty(char* newName, int newAge){
int len = strlen(newName) + 1;
name = new char[len];
strcpy(name, newName);
age = newAge;
}
friend void showInfo(kitty& cat);
};
void showInfo(kitty& cat){
cout << cat.name << endl;
cout << cat.age << endl;
}
int main(void){
kitty a("Kiki", 2);
showInfo(a);
return 0;
}
Kiki
2
ν΄λμ€ μΈλΆμμ κ°μ²΄μ λ νΌλ°μ€λ₯Ό μΈμλ‘ λ°λ ν¨μλ‘ μ μΈν΄μ£Όκ³ ν¨μ λ΄λΆμμ friendλ‘ μ μΈν΄μ£Όμ΄ μ¬μ©ν μ μλ€.
Static variable
#include<iostream>
using std::cout;
using std::endl;
void staticCounter(){
static int count;
cout << count++ <<endl;
}
void nonStaticCounter(){
int count;
cout << count++ <<endl;
}
int main(void){
for(int i = 0; i < 5; i++){
staticCounter();
nonStaticCounter();
}
return 0;
}
0 0 1 0 2 0 3 0 4 0
λ³μμ staticμ΄ λΆμΌλ©΄ μ μ λ³μλ‘, mainμ΄ μ’
λ£λ λ ν¨κ» μλ©Έλλ€. ν¨μκ° μ’
λ£λκ±°λ κ°μ²΄κ° μλ©Έλμ΄λ μλ©Έλμ§ μλλ€.
Static object
#include<iostream>
using std::cout;
using std::endl;
class test{
public :
test(){
cout << this << " CREATED" <<endl;
}
~test(){
cout << this << " DESTROYED" <<endl;
}
};
void createObject(){
static test testObject;
}
int main(void){
int x = 0;
if(x == 0){
test a;
createObject();
}
return 0;
}
0x7ffc3f417d1b CREATED
0x6021b9 CREATED
0x7ffc3f417d1b DESTROYED
0x6021b9 DESTROYED
κ°μ²΄μ staticμ΄ λΆμΌλ©΄ μ μ κ°μ²΄λ‘, μ μ λ³μμ λ§μ°¬κ°μ§λ‘ mainμ΄ μ’
λ£λ λ μλ©Έλλ€.
Static member variable
#include<iostream>
using std::cout;
using std::endl;
class test{
public :
static int id;
test(){
cout<<this<<" CREATED"<<endl;
}
void showInfo(){
cout<<this<<" "<<this->id<<endl;
}
};
int test::id = 999;
int main(void){
test a;
test b;
test c;
a.showInfo();
b.showInfo();
c.showInfo();
return 0;
}
0x7ffd0bfdf4ed CREATED
0x7ffd0bfdf4ee CREATED
0x7ffd0bfdf4ef CREATED
0x7ffd0bfdf4ed 999
0x7ffd0bfdf4ee 999
0x7ffd0bfdf4ef 999
λ©€λ²λ³μμλ staticμ λΆμ¬ μ μ λ©€λ²λ³μλ‘ λ§λ€ μ μλ€. μ μ λ©€λ²λ³μλ‘ μ μΈλλ©΄ ν΄λΉ ν΄λμ€μ λͺ¨λ κ°μ²΄λ€μ΄ λͺ¨λ λμΌν κ°μ κ°λλ€. μ μ λ©€λ²λ³μμ μ΄κΈ°νλ ν΄λμ€μ μΈλΆμμ μ΄λ€μ ΈμΌνλ©° κ°μ²΄λ₯Ό μμ±ν λ μμ±μλ μ΄λ₯Ό 무μν΄λ λλ€.
Static method
#include<iostream>
using std::cout;
using std::endl;
class test{
public :
static int id;
static void idAdder(){
id++;
}
test(){
cout<<this<<" CREATED"<<endl;
}
void showInfo(){
cout<<this<<" "<<this->id<<endl;
}
};
int test::id = 999;
int main(void){
test a;
test b;
test c;
a.showInfo();
b.showInfo();
c.showInfo();
test::idAdder();
a.showInfo();
b.showInfo();
c.showInfo();
return 0;
}
0x7ffcd656dccd CREATED
0x7ffcd656dcce CREATED
0x7ffcd656dccf CREATED
0x7ffcd656dccd 999
0x7ffcd656dcce 999
0x7ffcd656dccf 999
0x7ffcd656dccd 1000
0x7ffcd656dcce 1000
0x7ffcd656dccf 1000
λ³μ λν staticμ΄ λΆμΌλ©΄ μ μ λ©μλκ° λλ€. μ μ λ©μλλ λ€λ₯Έ μ μ λ©€λ² λ³μ νΉμ μ μ λ©μλμλ§ μ κ·Όν μ μλ€. μ μ λ³μμ λ©μλλ κ°μ²΄μλ 무κ΄νκ² λμνλ―λ‘ thisλ μ¬μ© λͺ»νκ³ , ::μ ν΅ν΄ μ κ·Όνλ€.
Comments