상속

부모 클래스를 물려받아 자식클래스가 그대로 사용할 수 있는 것

이미 정의된 클래스를 상속받아 사용 할 수 있는 장점

부모가 되는 클래스 : 기반 클래스, 기초 클래스, 베이스 클래스

자식 클래스 : 파생 클래스


 접근 지정자

자신의 클래스

파생 클래스

클래스 외부

 private

O

X

X

protected

O

O

X

 public

O

O

O


파생 클래스 사용 방법


class 파생 클래스 : 접근 지정자 기반 클래스{

    멤버변수:

    멤버함수:

};


 

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
#include<iostream>
 
using namespace std;
 
class Calc {
protected:
    int a, b, c;
public:
    void Init(int new_a, int new_b);
    void Prn();
};
void Calc::Init(int new_a, int new_b) {
    a = new_a;
    b = new_b;
    c = 0;
}
void Calc::Prn() {
    cout << "a : "<< a << "\tb : " << b << "\tc : " << c << endl;
}
 
 
class Add : public Calc {
public:
    void Sum();
};
void Add::Sum() {
    c = a + b;
}
 
 
class Mul : public Calc {
public:
    void Gob();
};
void Mul::Gob() {
    c = a*b;
}
 
 
void main() {
    Add x;
    Mul y;
    x.Init(35);
    y.Init(27);
    x.Sum();
    x.Prn();
    y.Gob();
    y.Prn();
}
cs

 

▲부모 클래스와 파생 클래스 생성 예제




상속관계에서의 생성자와 소멸자

생성자와 소멸자는 맴버함수지만 상속이 불가능

파생 객체가 생성시 부모 클래스의 생성자 까지 연속적으로 자동 호출됨

즉, 위 예제의 Add 클래스를 생성할때 Calc 생성자도 호출되며 메모리 공간엔 Calc+Add 만큼 공간을 차지하게 됨


 

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
#include<iostream>
 
using namespace std;
 
class Base {
public:
    Base();
    ~Base();
};
 
Base::Base() {
    cout << "부모 클래스의 생성자 호출" << endl;
}
Base::~Base() {
    cout << "부모 클래스의 소멸자 호출" << endl;
}
 
class Dervied : public Base{
public:
    Dervied();
    ~Dervied();
};
Dervied::Dervied() {
    cout << "파생 클래스의 생성자 호출" << endl;
}
Dervied::~Dervied() {
    cout << "파생 클래스의 생성자 호출" << endl;
}
void main() {
    Dervied obj;
}
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
#include<iostream>
 
using namespace std;
 
class Calc {
protected:
    int a, b, c;
public:
    Calc();
    Calc(int new_a, int new_b);
    void Prn();
};
void Calc::Prn() {
    cout << "a : "<< a << "\tb : " << b << "\tc : " << c << endl;
}
Calc::Calc() {
    a = 0; b = 0; c = 0;
}
Calc::Calc(int new_a, int new_b) : a(new_a), b(new_b), c(0)
{
}
 
class Add : public Calc {
public:
    void Sum();
    Add(int new_a, int new_b);
    Add();
};
void Add::Sum() {
    c = a + b;
}
Add::Add(int new_a, int new_b) : Calc(new_a, new_b) 
{
}
Add::Add() : Calc() {
 
}
 
void main() {
    Calc x(35);
    x.Prn();
    Add y(35);
    y.Prn();
    Add z;
    z.Prn();
}
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
 
using namespace std;
 
class Calc {
protected:
    int a, b;
public:
    Calc();
    Calc(int new_a, int new_b);
    void Prn();
};
void Calc::Prn() {
    cout << "a : "<< a << "\tb : " << b << endl;
}
Calc::Calc() : a(0),b(0)
{
}
Calc::Calc(int new_a, int new_b) : a(new_a), b(new_b)
{
}
 
class Add : public Calc {
protected:
    int c;
public:
    Add(int new_a, int new_b);
    Add();
    void Sum();
    void Prn();
};
void Add::Sum() {
    c = a + b;
}
Add::Add(int new_a, int new_b) : Calc(new_a, new_b) ,c(0)
{
}
Add::Add() : Calc(), c(0) {
 
}
void Add::Prn() {
    cout << a << " + " << b << " = " << c << endl;
}
 
class Mul : public Calc {
protected:
    int c;
public:
    Mul(int new_a, int new_b);
    Mul();
    void Gob();
    void Prn();
};
void Mul::Gob() {
    c = a * b;
}
Mul::Mul(int new_a, int new_b) : Calc(new_a, new_b), c(0)
{
}
Mul::Mul() : Calc(), c(0) {
 
}
void Mul::Prn() {
    cout << a << " * " << b << " = " << c << endl;
}
 
void main() {
    Calc x(35);
    x.Prn();
    Add y(35);
    y.Sum();
    y.Prn();
    Mul z(35);
    z.Gob();
    z.Prn();
}
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
#include<iostream>
using namespace std;
 
class Complex {
private:
    int real;
    int image;
public:
    Complex(int r = 0int i = 0);
    void showComplex();
    Complex sum(Complex rightHand);
};
 
void Complex::showComplex() {
    cout << "( " << real << " + " << image << "i )" << endl;
}
 
Complex::Complex(int r, int i) {
    real = r;
    image = i;
}
 
Complex Complex::sum(Complex rightHand) {
    Complex res;
    res.real = this->real + rightHand.real;
    res.image = this->image + rightHand.image;
    return res;
}
 
 
void main() {
    Complex x(1020), y(2040);
    Complex z;
    z = x.sum(y);
    x.showComplex();
    y.showComplex();
    z.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
35
36
37
38
#include<iostream>
using namespace std;
 
class Complex {
private:
    int real;
    int image;
public:
    Complex(int r = 0int i = 0);
    void showComplex();
    friend Complex sum(Complex leftHand, Complex rightHand);
};
 
void Complex::showComplex() {
    cout << "( " << real << " + " << image << "i )" << endl;
}
 
Complex::Complex(int r, int i) {
    real = r;
    image = i;
}
 
Complex sum(Complex leftHand, Complex rightHand) {
    Complex res;
    res.real = leftHand.real + rightHand.real;
    res.image = leftHand.image + rightHand.image;
    return res;
}
 
 
void main() {
    Complex x(1020), y(2040);
    Complex z;
    z = sum(x, y);
    x.showComplex();
    y.showComplex();
    z.showComplex();
}
cs

 

▲프렌드 함수로 두 객체의 합을 구하는 메소드 예

 

friend : 클래스에 private된 멤버변수를 외부함수에서 사용할 수 있도록 하는 키워드

데이터 보안에 문제가 있으므로 남발하면 안됨



클래스에서 선행처리, 후행처리 하는 방법

real의 값을 1 더하는 행동

++this->real -> 선행처리

this->real++ -> 후행처리

복사된 값을 넘겨주느냐 더한값을 넘겨주느냐에 따라 선행과 후행이 결정됨


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
#include<iostream>
using namespace std;
 
class Complex {
private:
    int real;
    int image;
public:
    Complex(int r = 0int i = 0);
    void showComplex();
    Complex addOnePreFix();
    Complex addOnePostFix();
};
 
void Complex::showComplex() {
    cout << "( " << real << " + " << image << "i )" << endl;
}
 
Complex::Complex(int r, int i) {
    real = r;
    image = i;
}
//선행처리
Complex Complex::addOnePreFix() {
    ++this->real;
    ++this->image;
    return *this;
}
//후행처리
Complex Complex::addOnePostFix() {
    Complex temp;
    temp = *this;
    ++this->real;
    ++this->image;
    return temp;
}
void main() {
    Complex x(1020), y(2040);
    Complex z;
    cout << "---- 선행처리 ----"<<endl;
    z = x.addOnePreFix();
    x.showComplex();
    z.showComplex();
 
    cout << "---- 후행처리 ----" << endl;
    z = y.addOnePreFix();
    y.showComplex();
    z.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
using namespace std;
 
class Complex {
private:
    int real;
    int image;
public:
    Complex(int r = 0int i = 0);
    void showComplex();
    friend Complex &addOnePreFix(Complex &Operand);
    friend Complex addOnePostFix(Complex &Operand);
};
 
void Complex::showComplex() {
    cout << "( " << real << " + " << image << "i )" << endl;
}
 
Complex::Complex(int r, int i) {
    real = r;
    image = i;
}
//선행처리
Complex &addOnePreFix(Complex &Operand) {
    ++Operand.real;
    ++Operand.image;
    return Operand;
}
//후행처리
Complex addOnePostFix(Complex &Operand) {
    Complex temp;
    temp = Operand;
    ++Operand.real;
    ++Operand.image;
    return temp;
}
void main() {
    Complex x(1020), y(2040);
    Complex z;
    cout << "---- 선행처리 ----" << endl;
    z = addOnePreFix(x);
    x.showComplex();
    z.showComplex();
 
    cout << "---- 후행처리 ----" << endl;
    z = addOnePostFix(y);
    y.showComplex();
    z.showComplex();
}
cs

 


연산자 오버로딩


C++에서 기본 자료형으로 사용하고 있는 연산자를 재정의 하는 것

연산자도 함수로 취급하기 때문에 함수를 정의하는 방법과 동일 한 방법으로 연산자를 오버로딩할 수 있음

오버로딩 된 연산자를 연산자함수(Operator Function)이라고 함

연산에 참여하는 피연산자는 연산자를 정의할 때 매개변수로 구현하여 사용

매개변수 자료형에 의해 그 연산자를 사용할 수 있는 자료형이 결정됨


반환값 operator연산자(매개변수1,매개변수2,...)

{

함수의 본체

}


 

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
using namespace std;
 
class Complex {
private:
    int real;
    int image;
public:
    Complex(int r = 0int i = 0);
    void showComplex();
    Complex operator+(Complex rightHand);
    Complex operator-(const Complex &rightHand) const;
    Complex operator-() const;
};
 
void Complex::showComplex() {
    if (image > 0)
        cout << "(" << real << "+" << image << " i)" << endl;
    else if (image < 0)
        cout << "(" << real << image << " i)" << endl;
    else
        cout << real << endl;
}
 
Complex::Complex(int r, int i) {
    real = r;
    image = i;
}
 
//연산자 +를 오버로딩
Complex Complex::operator+(Complex rightHand) {
    Complex res;
    res.real = this->real + rightHand.real;
    res.image = this->image + rightHand.image;
    return res;
}
//연산자 -를 오버로딩
Complex Complex::operator-(const Complex &rightHand) const{
    Complex res;
    res.real = this->real - rightHand.real;
    res.image = this->image - rightHand.image;
    return res;
}
//부호 변경
Complex Complex::operator-() const 
{
    Complex res;
    res.real = -real;
    res.image = -image;
    return res;
}
void main() {
    Complex x(1020), y(2040);
    Complex z;
    cout << "---- 두 Complex 객체에 대한 덧셈 ----" << endl;
    z = x + y;
    x.showComplex();
    y.showComplex();
    z.showComplex();
 
    cout << "---- 두 Complex 객체에 대한 뺄셈 ----" << endl;
    z = x - y;
    x.showComplex();
    y.showComplex();
    z.showComplex();
 
    cout << "---- Complex 객체의 부호 변경 ----" << endl;
    z = -x;
    x.showComplex();
    z.showComplex();
}
cs

 


출력결과


 


cout에서 출력할 때 사용하는 << 연산자도 연산자오버로딩이 가능함.


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;
 
class Complex {
private:
    int real;
    int image;
public:
    Complex(int r = 0int i = 0);
    friend ostream &operator<<(ostream &os, const Complex &comObj);
};
 
Complex::Complex(int r, int i) {
    real = r;
    image = i;
}
ostream &operator<<(ostream &os, const Complex &comObj) {
    os << "( " << comObj.real << " + " << comObj.image << "i )" << endl;
    return os;
}
void main() {
    Complex x(1020);
    cout << x;
}
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
#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