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

20150618 4번 김민정 C#(str 연산자, Str 연산자, 다형성)

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

20150618 C++

연산자

Str 연산자

다형성

- 멤버함수가 호출하는 함수

- 가상 파괴자

연산자

- Str 연산자

p235 예제

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <cstdlib>
#include <stdarg.h>
#include <stdio.h>
using namespace std;

class Str
{
    friend ostream &operator <<(ostream &c, const Str &S);
    friend const Str operator +(const char *ptr,Str &s);
    friend bool operator ==(const char *ptr,Str &s);
    friend bool operator !=(const char *ptr,Str &s);
    friend bool operator >(const char *ptr,Str &s);
    friend bool operator <(const char *ptr,Str &s);
    friend bool operator >=(const char *ptr,Str &s);
    friend bool operator <=(const char *ptr,Str &s);
    protected:
    char *buf;
    int size;

    public:
    Str();
    Str(const char *ptr);
    Str(const Str &Other);
    explicit Str(int num);
    virtual ~Str();

    int length() const { return strlen(buf); }
    Str& operator =(const Str &Other);
    Str& operator +=(Str &Other);
    Str& operator +=(const char *ptr);
    char& operator [](int idx) { return buf[idx]; }
    const char &operator [](int idx) const { return buf[idx]; }
    operator const char *() { return (const char *)buf; }
    operator int() { return atoi(buf); }
    const Str operator +(Str &Other) const;
    const Str operator +(const char *ptr) const { return *this+Str(ptr); }
    bool operator ==(Str &Other) { return strcmp(buf,Other.buf)==0; }
    bool operator ==(const char *ptr) { return strcmp(buf,ptr)==0; }
    bool operator !=(Str &Other) { return strcmp(buf,Other.buf)!=0; }
    bool operator !=(const char *ptr) { return strcmp(buf,ptr)!=0; }
    bool operator >(Str &Other) { return strcmp(buf,Other.buf)>0; }
    bool operator >(const char *ptr) { return strcmp(buf,ptr)>0; }
    bool operator <(Str &Other) { return strcmp(buf,Other.buf)<0; }
    bool operator <(const char *ptr) { return strcmp(buf,ptr)<0; }
    bool operator >=(Str &Other) { return strcmp(buf,Other.buf)>=0; }
    bool operator >=(const char *ptr) { return strcmp(buf,ptr)>=0; }
    bool operator <=(Str &Other) { return strcmp(buf,Other.buf)<=0; }
    bool operator <=(const char *ptr) { return strcmp(buf,ptr)<=0; }
    void Format(const char *fmt,...);
};

// 디폴트 생성자
Str::Str()
{
    size=1;
    buf=new char[size];
    buf[0]=0;
}

// 문자열로부터 생성하기
Str::Str(const char *ptr)
{
    size=strlen(ptr)+1;
    buf=new char[size];
    strcpy(buf,ptr);
}

// 복사 생성자
Str::Str(const Str &Other)
{
    size=Other.length()+1;
    buf=new char[size];
    strcpy(buf,Other.buf);
}

// 정수형 변환 생성자
Str::Str(int num)
{
    char temp[128];

    itoa(num,temp,10);
    size=strlen(temp)+1;
    buf=new char[size];
    strcpy(buf,temp);
}

// 파괴자
Str::~Str()
{
    delete [] buf;
}

// 대입 연산자
Str &Str::operator =(const Str &Other)
{
    if (this != &Other) {
        size=Other.length()+1;
        delete [] buf;
        buf=new char[size];
        strcpy(buf,Other.buf);
    }
    return *this;
}

// 복합 연결 연산자
Str &Str::operator +=(Str &Other)
{
    char *old;
    old=buf;
    size+=Other.length();
    buf=new char[size];
    strcpy(buf,old);
    strcat(buf,Other.buf);
    delete [] old;
    return *this;
}

Str &Str::operator +=(const char *ptr)
{
    return *this+=Str(ptr);
}

// 연결 연산자
const Str Str::operator +(Str &Other) const
{
    Str T;

    delete [] T.buf;
    T.size=length()+Other.length()+1;
    T.buf=new char[T.size];
    strcpy(T.buf,buf);
    strcat(T.buf,(const char *)Other);
    return T;
}

// 출력 연산자
ostream &operator <<(ostream &c, const Str &S)
{
    c<< S.buf;
    return c;
}

// 더하기 및 관계 연산자
const Str operator +(const char *ptr,Str &s) { return Str(ptr)+s;}
bool operator ==(const char *ptr,Str &s) { return strcmp(ptr,s.buf)==0;}
bool operator !=(const char *ptr,Str &s) { return strcmp(ptr,s.buf)!=0;}
bool operator >(const char *ptr,Str &s) { return strcmp(ptr,s.buf)>0;}
bool operator <(const char *ptr,Str &s) { return strcmp(ptr,s.buf)<0;}
bool operator >=(const char *ptr,Str &s) { return strcmp(ptr,s.buf)>=0;}
bool operator <=(const char *ptr,Str &s) { return strcmp(ptr,s.buf)<=0;}

// 서식 조립 함수
void Str::Format(const char *fmt,...)
{
    char temp[1024];
    va_list marker;

    va_start(marker, fmt);
    vsprintf(temp,fmt,marker);
    *this=Str(temp);
}


int main()
{
    Str s="125";
    int k;
    k=(int)s+123;

    Str s1("문자열");      // 문자열로 생성자
    Str s2(s1);                   // 복사 생성자
    Str s3;                    // 디폴트 생성자
    s3=s1;                    // 대입 연산자

    // 출력 연산자
    cout<< "s1=" << s1 << ",s2=" << s2<< ",s3=" << s3 << endl;
    cout<< "길이=" << s1 << endl;

    // 정수형 변환 생성자와 변환 연산자
    Str s4(1234);
    cout<< "s4=" << s4 << endl;
    int num=int(s4)+1;
    cout<< "num=" << num << endl;

    // 문자열 연결 테스트
    Str s5="First";
    Str s6="Second";
    cout<< s5+s6 << endl;
    cout<< s6+"Third" << endl;
    cout<< "Zero"+s5 << endl;
    cout<< "s1은 "+s1+"이고 s5는 "+s5+"이다." << endl;
    s5+=s6;
    cout<< "s5와 s6을 연결하면 " << s5 << "이다."<< endl;
    s5+="Concatination";
    cout<< "s5에 문자열을 덧붙이면 " << s5 << "이다."<< endl;

    // 비교 연산자 테스트
    if (s1 == s2) {
        cout<< "두 문자열은 같다." << endl;
    } else {
        cout<< "두 문자열은 다르다." << endl;
    }

    // char *형과의 연산 테스트
    Str s7;
    s7="상수 문자열";
    cout<< s7 << endl;
    char str[128];
    strcpy(str,s7);
    cout<< str << endl;

    // 첨자 연산자 테스트
    Str s8("Index");
    cout<< "s8[2]=" << s8[2<< endl;
    s8[2]='k';
    cout<< "s8[2]=" << s8[2<< endl;

    // 서식 조립 테스트
    Str sf;
    int i=9876;
    double d=1.234567;
    sf.Format("서식 조립 가능하다. 정수=%d, 실수=%.2f",i,d);
    cout<< sf << endl;

    return 0;
}

  

다형성

- 교재 p322 다형성 -> 멤버 함수가 호출하는 함수 예제,

p322 예제 MemCallMem

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

class Point
 {
     protected:
   int x,y;
   char ch;

     public:
   Point(int ax, int ay, char ach)
   {
       x=ax;
       y=ay;
       ch=ach;
   }
   virtual void Show()
   {
       gotoxy(x,y);
             putch(ch);
         }
         virtual void Hide()
         {
             gotoxy(x,y);
             putch(' ');
         }
         void Move(int nx, int ny)
         {
             Hide();
             x=nx;
             y=ny;
             Show();
         }
 };

class Circle : public Point
 {
     protected:
         int Rad;

     public:
         Circle(int ax, int ay, char ach, int aRad) 
             : Point(ax, ay, ach)
         {
             Rad = aRad;
         }

         virtual void Show()
         {
       for(double a = 0; a < 360; a+=15)
             {
                 gotoxy(int(x+sin(a*3.14/180)*Rad), int(y-cos(a*3.14/180)*Rad));
                 putch(' ');
       }
   }
 };

int main(void)
 {


     Point P(1,1,'P');
     Circle C(10,10,'C',5);

     P.Show();
     C.Show();

     getch();
     P.Move(40,1);
     getch();
     C.Move(40,10);
     getch();
 }




 


 예제 p329 다형성 -> 가상 파괴자 예제


#include <iostream>
#include <stdio.h>
using namespace std;

class Base
{
    private : 
        char *B_buf;
    public:
        Base()
        {
            B_buf = new char[10];
            puts("Base 생성");
        }
        virtual ~Base()
        {
            delete [] B_buf;
            puts("Base 파괴");
        }
};
class Derived : public Base
{
    private : 
        char *D_buf;
    public:
        Derived()
        {
            D_buf = new char[10];
            puts("Derived 생성");
        }
        virtual ~Derived()
        {
            delete [] D_buf;
            puts("Derived 파괴");
        }
};

        

int main(void)
{
    Base *pB;
    pB = new Derived;
    delete pB;
    return 0;
}




- 생성자는 virtual 키워드를 사용할 수 없다.

 

- 소멸자에 virtual을 붙혀주면 아래와 같은 결과가 나온다.





 

728x90