블로그 이미지
송시혁

calendar

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

Notice

Tag

Recent Post

Recent Comment

Recent Trackback

Archive

2013. 11. 13. 19:05 c++


#include <iostream>
#include <string>
using namespace std;

class car
{
  public:
    string Color;
    string Vender;
    string Model;

    car()//생성자 
    {
      Color="없음";                                    
      Vender="없음";
      Model="없음";
      cout << "car class 생성자 구동\n";
    }

    void print()
    {
      cout <<"Color  = "  << Color <<endl;
      cout <<"Vender = "  << Vender <<endl;
      cout <<"Model  = "  << Model <<endl;
    }

    ~car()//소멸자 생성.
    {
      cout << "car class 소멸자 구동 시작\n";
      print();
      
      cout << "car class 소멸자 구동 끝\n";
    }
};


class bmw_745
{
  public:
    string Color;
    string Vender;
    string Model;

    bmw_745()
    {
      Color="white";
      Vender="bmw";
      Model="745Li";
      cout << "bmw class 생성자 구동\n";
    }
  
    ~bmw_745()
    {
      cout << "bmw_745 class 소멸자 구동 시작\n";
      print();
      
      cout << "bmw_745 class 소멸자 구동 끝\n";
    }
    void print()
    {
      cout <<"Color  = "  << Color <<endl;
      cout <<"Vender = "  << Vender <<endl;
      cout <<"Model  = "  << Model <<endl;
    }
  
    

};

//class Mornig:car

class Mornig:public car  //class car를 상속받는다.
{
  public:
    
    Mornig()
    {
      Color="red";
      Vender="kia";
      Model="morning";
      cout << "Mornig class 생성자 구동\n";      
    }

    ~Mornig()
    {
      cout << "Mornig class 소멸자 구동 시작\n";
      print();
      
      cout << "Mornig class 소멸자 구동 끝\n";
    }
};




int main()
{
  car aCar;
  aCar.Color="blue";
  aCar.Vender="bmw";
  aCar.Model="sonata";
  
//  aCar.print();

  bmw_745 Mycar;
//  Mycar.print();

  Mornig Momcar;
//  Momcar.print();

  return 0;
}

/*
 상속받은 클래스는 부모생성자와 자식생성자 2개를 생성한다.
 (실행결과 3,4행은 Mornig class의 의해서 생성된것.)  
 소멸자는  역순으로 자식생성자, 부모생성자순으로 소멸된다.

*/






'c++' 카테고리의 다른 글

11월 25일 연산자 오버로딩 (정리 중)  (0) 2013.11.25
11월 21일 추상클래스  (0) 2013.11.21
11월 13일 상속 클래스 기초1.  (0) 2013.11.13
11월 12일 파일 임의 접근  (0) 2013.11.13
11월 11일 fstream 클래스  (0) 2013.11.11
posted by 송시혁