본문 바로가기
코스웨어/15년 스마트컨트롤러

Car 클래스 수업자료

by 알 수 없는 사용자 2015. 6. 11.
728x90
반응형

#include <iostream>
#include <string.h>


using namespace std;

class CAR
{
    private :
        int iColor;
        int iSpeed;
        char *cName;
        void CarName(const char *);
    public :
        CAR();
        CAR(const char *);
        CAR(CAR &);
        ~CAR();
        void SetColor(int);
        void SetSpeed(int);
        void SetName(const char *);
        void Print(ostream *IN);
        const CAR operator=(const CAR &);
        void operator+=(CAR &IN);
};

CAR::CAR()
{
    CarName("car");
}

CAR::CAR(const char *T)
{
    CarName(T);
}

CAR::CAR(CAR &T)
{
    iColor = T.iColor;
    iSpeed = T.iSpeed;
    cName = new char[strlen(T.cName) + 1];
    strcpy(cName, T.cName);
#ifdef DEBUG
    cout << cName << "가 복사되었습니다\n";
#endif //DEBUG
}

CAR::~CAR()
{
#ifdef DEBUG
    cout << cName << "가 소멸되었습니다\n";
#endif //DEBUG
    delete[] cName;
}

void CAR::CarName(const char *T)
{
    iColor = 0;
    iSpeed = 0;
    cName = new char[strlen(T) + 1];
    strcpy(cName, T);
#ifdef DEBUG
    cout << cName << "가 생성되었습니다\n";
#endif //DEBUG
}

void CAR::SetColor(int color)
{
#ifdef DEBUG
    cout << cName <<  ":: iColor가 " << iColor << "에서 ";
#endif //DEBUG
    iColor = color;
#ifdef DEBUG
    cout << iColor << "로 변경되었습니다\n";
#endif //DEBUG
}

void CAR::SetSpeed(int speed)
{
#ifdef DEBUG
    cout << cName <<  ":: iSpeed가 " << iSpeed << "에서 ";
#endif //DEBUG
    iSpeed = speed;
#ifdef DEBUG
    cout << iSpeed << "로 변경되었습니다\n";
#endif //DEBUG
}

void CAR::SetName(const char *name)
{
#ifdef DEBUG
    cout << cName <<  ":: cName가 " << cName << "에서 ";
#endif //DEBUG

    delete[] cName;
    cName = new char[strlen(name) +1];
    strcpy(cName, name);

#ifdef DEBUG
    cout << cName << "로 변경되었습니다\n";
#endif //DEBUG
}

const CAR CAR::operator=(const CAR &T)
{
    SetColor(T.iColor);
    SetSpeed(T.iSpeed);
    SetName(T.cName);
    return *this;
}

int main(void)
{
    CAR car1;
    CAR car2("BMW");
    CAR car3("내꺼");
    car3 = car2 = car1;

    cout << "----------------------------------\n";
    return 0;
}


728x90