Busan IT/제어 UI(C++)

클래스, 생성자

newind2000 2015. 5. 28. 17:23

 

================================ Outline ====================================

scope 연산자를 활용한 전역변수 출력

접근 연산자

bool 변수

생성자, 소멸자, 임시객체

----------------------------------------------------------------------------

 

<scope 연산자를 활용한 전역변수 출력>

 

 

<접근 연산자>

 

 

<bool 변수>

 

<생성자, 소멸자, 임시객체>


#include <iostream>

using namespace std;

class smart2
{
    public:
        int iNum;

        smart2(int A)
        {
            cout << "smart2\n";
        }
};

class smart
{
    public :
        int iNum;

        void test(void)
        {
            cout << "test\n";
            return;
        }
        //    private:          //접근 제한(private)를 만들면 생성자를 호출할 수 없다.

        smart(int A)                 
        {
            test();
            iNum = A;
        }
        /*smart(int A, char const * cString)                 
          {
          test(cString);
          iNum = A;
          }*/


        smart(int A, int B)                 
        {
            test();
            iNum = A + B;
        }
 //   private : 
        smart()                 //클래스 smart를 초기화 하기 위한 생성자, 인자가 없는 생성자 default 생성자
        {
            test();
            iNum = 0;
        }
        //소멸자를 만들기 위해 '~'를 넣어준다.  삽입 메인 바로 직전에 소멸된다. stack 구조의 특성
        //소멸자는 인자가 필요 없다.
        ~ smart()              
        {
            cout <<iNum<< "소멸자 호출됨\n";
        }

};

int main(void)
{
    smart(300);                 // 접근 불가의 '임시 객체'. 그 자리에서 생성 되었다가 바로 사라진다.
    smart obj1;
    smart obj2(100);
    smart obj3(100,200);

    cout << obj1.iNum << endl;
    cout << obj2.iNum << endl;
    cout << obj3.iNum << endl;


    smart2 obj4(100);
  //  smart2 obj5;
    smart(700);                 // 접근 불가의 임시 객체. 그 자리에서 생성 되었다가 바로 사라진다.
    cout << "=======================================================" <<endl;
    return 0;
}


반응형