객체 포인터 변수 선언
클래스명 *객체 포인터 변수;
화살표(->) 연산자 사용가능
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
using namespace std;
 
class Complex
{
private:
    int real;
    int image;
public:
    Complex(int r = 0int i = 0);
    void ShowComplex() const;
};
 
Complex::Complex(int r, int i) : real(r), image(i)
{
}
 
void Complex::ShowComplex() const
{
    cout << "( " << real << " + " << image << "i )" << endl;
}
 
void main() {
    Complex x(1020);
    Complex y;
    cout << " Object x => ";
    x.ShowComplex();
    cout << " Object y => ";
    y.ShowComplex();
 
    Complex *pCom;
    pCom = &x;
    cout << "\n pCom->ShowComplex() => ";
    pCom->ShowComplex();
 
    pCom = &y;
    cout << "pCom->ShowComplex() => ";
    pCom->ShowComplex();
}
cs

▲객체 포인터 사용



내부 포인터 this

객체의 맴버함수를 호출할때 사용할 수 있는 포인터

컴파일러가 맴버변수 앞에 자동적으로 붙여주므로 생략이 가능하지만 매개변수와 이름이 같을 때 구분하기위해 사용할 수 있음


객체 간의 값 치환

클래스는 c의 구조체를 확장한 개념으로 비교는 할 수 없으나 치환은 가능함.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
using namespace std;
 
class Complex
{
private:
    int real;
    int image;
public:
    Complex(int r = 0int i = 0);
    void ShowComplex() const;
    void SetComplex(int x = 0int i = 0);
};
 
Complex::Complex(int r, int i) : real(r), image(i)
{
}
 
void Complex::ShowComplex() const
{
    cout << "( " << real << " + " << image << "i )" << endl;
}
 
//내부포인터 this를 이용함
void Complex::SetComplex(int real, int image)
{
    this->real = real;
    this->image = image;
}
 
void main() {
    Complex x(1020);
    Complex y;
    cout << " Object x => ";
    x.ShowComplex();
    cout << " Object y => ";
    y.ShowComplex();
 
    cout << "------------------------------------ \n";
    //객체 단위로 값 치환
    y = x;
    cout << " x => ";
    x.ShowComplex();
    cout << " y => ";
    y.ShowComplex();
 
    cout << "------------------------------------ \n";
    y.SetComplex(3040);
    cout << " x => ";
    x.ShowComplex();
    cout << " y => ";
    y.ShowComplex();
}
cs

 

▲this와 객체 치환


참조에 의한 호출 방식으로도 값을 치환할 수 있다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
using namespace std;
 
class Complex
{
private:
    int real;
    int image;
public:
    Complex(int r = 0int i = 0);
    void ShowComplex() const;
    void SetComplex(int x = 0int i = 0);
};
 
Complex::Complex(int r, int i) : real(r), image(i)
{
}
 
void Complex::ShowComplex() const
{
    cout << "( " << real << " + " << image << "i )" << endl;
}
 
//내부포인터 this를 이용함
void Complex::SetComplex(int real, int image)
{
    this->real = real;
    this->image = image;
}
void CopyComplex(Complex *des, Complex src)
{
    *des = src;
}
void main() {
    Complex x(1020);
    Complex y;
    cout << " Object x => ";
    x.ShowComplex();
    cout << " Object y => ";
    y.ShowComplex();
 
    cout << "------------------------------------ \n";
    CopyComplex(&y, x);
    cout << " x => ";
    x.ShowComplex();
    cout << " y => ";
    y.ShowComplex();
}
cs
▲Call by Reference 치환방식



객체를 주소에 의한 호출을 사용하여 객체가 다른 것을 참조할 수 있도록 할 수 있다.


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
using namespace std;
 
class Complex
{
private:
    int real;
    int image;
public:
    Complex(int r = 0int i = 0);
    void ShowComplex() const;
    void SetComplex(int x = 0int i = 0);
};
 
Complex::Complex(int r, int i) : real(r), image(i)
{
}
 
void Complex::ShowComplex() const
{
    cout << "( " << real << " + " << image << "i )" << endl;
}
 
//내부포인터 this를 이용함
void Complex::SetComplex(int real, int image)
{
    this->real = real;
    this->image = image;
}
Complex & CopyComplex(Complex &des, const Complex &src)
{
    des = src;
    return des;
}
void main() {
    Complex x(1020);
    Complex y;
    Complex z;
 
    z = CopyComplex(y, x);
    cout << " x => ";
    x.ShowComplex();
    cout << " y => ";
    y.ShowComplex();
    cout << " z => ";
    z.ShowComplex();
}
cs

 ▲모두 x를 참조하는 객체가 되는 예제


정적 멤버변수

특정 클래스의 모든 객체가 공유하는 것(전역변수 원리와 동일)

사용 조건

 

  • 정적 멤버변수는 특정 클래스 내부에 선언해야 함
  • 정적 멤버변수는 클래스 밖에서 별도로 초기화되어야 함

 정적 멤버함수에서는 this 래퍼런스 사용못함. 인스턴스 변수 사용할 수 없음. 오버라이딩 되지 않음

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
using namespace std;
 
class StaticTest {
private:
    static int a;
    int b;
public:
    StaticTest();
    static void SetA(int new_a);
    static int GetA();
};
int StaticTest::a = 10;
 
StaticTest::StaticTest() {
    b = 20;
}
 
void StaticTest::SetA(int new_a) {
    a = new_a;
}
 
int StaticTest::GetA() {
    return a;
}
 
void main() {
    cout << " StaticTest::GetA() => " << StaticTest::GetA() << "\n\n";
 
    StaticTest s1, s2;
    s1.SetA(10000);
    cout << " s1.GetA() -> " << s1.GetA() << "\t";
    cout << " s2.GetA() -> " << s2.GetA() << "\n\n";
}
cs

▲정적 멤버변수,함수 사용 예제


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
using namespace std;
 
class CStud {
private:
    char name[30];
    char hphone[20];
    char email[30];
    static int cnt;
public :
    CStud(char *n= "성윤정"char *= "017-777-7777"char*= "pink@daum.net");
    ~CStud();
    void prn();
    static void prn_cnt();
};
int CStud::cnt = 0;
CStud::CStud(char *n, char *h, char *e)
{
    strcpy(name, n);
    strcpy(hphone, h);
    strcpy(email, e);
    cnt++;
}
CStud::~CStud() {
    cnt--;
}
 
void CStud::prn() {
    cout << "이름\t:" << name << endl;
    cout << "핸드폰\t:" << hphone << endl;
    cout << "이메일\t:" << email << endl;
}
 
void CStud::prn_cnt() {
    cout << "\n현재까지 등록된 인원수 : " << cnt << "\n\n";
}
 
void main() {
    CStud::prn_cnt();
 
    CStud man1("전수빈""019-9087-0975""subin@pride.com");
    man1.prn();
    CStud man2("전원지""017-9087-0975""won@pride.com");
    man2.prn();
 
    cout << "\n# 중간에 인원수를 파악합니다.";
    man2.prn_cnt();
    CStud man3;
    man3.prn();
    cout << "\n클래스의 할당된 메모리 사이즈 : " << sizeof(CStud) << endl;
    CStud::prn_cnt();
}
cs

▲정적 멤버변수 사용 이용

 


객체 배열

선언 : 클래스명 객체배열명[원소의 개수];

참조 객체배열명[인덱스].멤버변수or함수;

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
using namespace std;
 
class Complex
{
private:
    int real;
    int image;
public:
    Complex(int r = 0int i = 0);
    void ShowComplex() const;
};
 
Complex::Complex(int r, int i) : real(r), image(i)
{
}
 
void Complex::ShowComplex() const
{
    cout << "( " << real << " + " << image << "i )" << endl;
}
 
void main() {
    Complex arr[4= {
        Complex(2,4),
        Complex(4,8),
        Complex(8,16),
        Complex(16,32)
    };
    for (int i = 0; i < 4; i++)
        arr[i].ShowComplex();
}
cs

▲객체 배열 사용 예제


 

객체 배열과 포인터 

포인터 변수에 객체 배열 인덱스를 줌으로써 사용가능

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
using namespace std;
 
class Complex
{
private:
    int real;
    int image;
public:
    Complex(int r = 0int i = 0);
    void ShowComplex() const;
};
 
Complex::Complex(int r, int i) : real(r), image(i)
{
}
 
void Complex::ShowComplex() const
{
    cout << "( " << real << " + " << image << "i )" << endl;
}
 
void main() {
    Complex arr[4= {
        Complex(2,4),
        Complex(4,8),
        Complex(8,16),
        Complex(16,32)
    };
    Complex *pCom = arr;
    pCom->ShowComplex();
    (pCom+1)->ShowComplex();
 
}
cs

▲객체 배열을 포인터로 참조한 예제


프렌드 함수

외부 함수에서 접근하고자 할때 사용

함수 앞에 friend를 붙인다.

데이터를 보안때문에 private를 사용한것이기 때문에 허용여부를 잘 판단해야됨

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
using namespace std;
 
class Complex
{
private:
    int real;
    int image;
public:
    Complex(int r = 0int i = 0);
    void ShowComplex() const;
 
    friend void prn(Complex *pCom);
};
 
Complex::Complex(int r, int i) : real(r), image(i)
{
}
 
void Complex::ShowComplex() const
{
    cout << "( " << real << " + " << image << "i )" << endl;
}
 
void prn(Complex *pCom) {
    for (int i = 0; i < 4; i++) {
        cout << "( " << pCom[i].real << " + " << pCom[i].image << "i )" << endl;
    }
}
 
void main() {
    Complex arr[4= {
        Complex(2,4),
        Complex(4,8),
        Complex(8,16),
        Complex(16,32)
    };
    Complex *pCom = arr;
    prn(arr);
}
cs

▲프렌드 함수 예제


+ Recent posts