• Tidak ada hasil yang ditemukan

1 1 장 강 사 – 강 대 기

N/A
N/A
Protected

Academic year: 2023

Membagikan "1 1 장 강 사 – 강 대 기"

Copied!
16
0
0

Teks penuh

(1)

객체객체객체객체지향지향지향지향프로그래밍프로그래밍프로그래밍프로그래밍 (Object Oriented Programming)(Object Oriented Programming)(Object Oriented Programming)(Object Oriented Programming)

1 1 장 강 사 – 강 대 기

(2)

차례차례차례차례(Agenda)(Agenda)(Agenda)(Agenda) <<

(3)

연산자연산자연산자연산자오버로딩오버로딩오버로딩오버로딩 operator

o p

(

a rg u m e n t lis t

) Time Time::operator+(constTime& t) const; // 640쪽 coding+fixingcoding.operator+(fixing) // 643쪽 t1+t2+t3t1.operator+(t2+t3) t1.operator+(t2.operator+(t3)) // 644쪽

(4)

연산자연산자연산자연산자오버로딩의오버로딩의오버로딩의오버로딩의제약제약제약제약(644(644(644(644쪽쪽쪽쪽)))) 피연산자중하나가사용자정의데이터형 원래연산자의문법규칙을따라야한다–예를들어 이항연산자를단항연산자로쓰지못하며, 연산자 우선순위도못바꿈 연산자기호를새로만들수없음–예. “**” 오버로딩할수없는연산자들이있음(645쪽, 646쪽 표11.1) 멤버함수로만오버로딩가능한연산자들(646쪽)

(5)

프렌드프렌드프렌드프렌드 외부에서클래스객체의private 부분에접근하는다 른통로 프렌드함수, 프렌드클래스(15장1000쪽), 프렌드 멤버함수(15장1006쪽) 왜? Time A = B*2.75; A = B.operator+(2.75); Time A = 2.75*B; ??? Time operator*(double m, const time& t); Time tprivate?

(6)

프렌드프렌드프렌드프렌드함수함수함수함수생성생성생성생성 friend Time operator*(double m, const time& t); 멤버함수가아님, 그러나동등한접근권한을가짐 (friend ) OOP? (653 )

(7)

연산자를연산자를연산자를연산자를오버로딩하는오버로딩하는오버로딩하는오버로딩하는두가지두가지두가지두가지방법방법방법방법 (Point ) Point Point::operator+(constPoint& p) const; (Point ) Point Point::operator+(doublem, const Point& p);

(8)

<<<<<<<< 연산자연산자연산자연산자오버로딩오버로딩오버로딩오버로딩 ostream Time 클래스에대한프렌드함수 (655)

, void cout<< x << y (cout<<x) << y 이기때문 (657, 659)

ostream

(9)

멤버멤버멤버멤버함수함수함수함수vs. vs. vs. vs. 프렌드프렌드프렌드프렌드함수함수함수함수 T1 = T2 + T3; T1 = T2.operator+(T3); T1 = operator+(T2,T3); ,

(10)

예예예예: : : : 벡터벡터벡터벡터클래스클래스클래스클래스(664(664(664(664쪽쪽쪽쪽)))) (667) getter / setter shove.set(100,3000); shove = Vector (100,3000); , (극, ) (675) : (676) : double, : (678) Random Walk (680)

(11)

내장내장내장내장데이터데이터데이터데이터형형형형변환변환변환변환복습복습복습복습 long count = 8; // 8L double time = 11; // 11.0 intside = 3.33; // 3 (경고) int* p = 10; // 에러 int* p = (int*)10;

(12)

암시적암시적암시적암시적데이터형데이터형데이터형데이터형변환변환변환변환 (115) Stonewt(doublelbs); StonewtmyCat; myCat= 19.6; // explicit Stonewt(double lbs) // StonewtmyCat; myCat= 19.6; //explicit myCat= Stonewt(19.6); myCat= (Stonewt)19.6; Stonewt(double) (int) StonewtJumbo(7000); // intdoubleStonewt(double) Jumbo = 7300; // intdoubleStonewt(double)

(13)

변환변환변환변환함수함수함수함수 ? Stonewtwolfe(285.7); double host = wolfe; , double host = double (wolfe); double host = (double) wolfe; operator typename(); operator int();, operator double(); , (696) cout<< poppins; (에) double p_wt= poppins; () long gone = poppins; () long gone = (double) poppins; (강) long gone = int(poppins); ()

(14)

암시적암시적암시적암시적변환변환변환변환자동자동자동자동수행의수행의수행의수행의문제점문제점문제점문제점 intar[20]; Stonewttemp(14,4); intTemp = 1; cout<< ar[temp] << endl; Stonewt::operatorint() {return int(pounds+5);} intStonewt:: Stone_to_Int() {return int(pounds+5);}

698~699

(15)

변환과변환과변환과변환과프렌드프렌드프렌드프렌드 덧셈추가–덧셈연산자오버로딩 (699) Stonewt= Stonewt+ Stonewt : jennySt.operator+(bennySt) : operator+(jennySt, bennySt) Stonewt(double) Stonewt= Stonewt+ double jennySt.operator+(kennyD) // operator+(jennySt, kennyD) // kennyD operator double Stonewt= double+ Stonewt pennyD operator+(pennyD, jennySt)

(16)

d o u b le d o u b le d o u b le d o u b le 과과과과 객 체 의 객 체 의 객 체 의 객 체 의 덧 셈 덧 셈 덧 셈 덧 셈 구 현 을 구 현 을 구 현 을 구 현 을 위 한 위 한 위 한 위 한 선 택 선 택 선 택 선 택 (7 0 1 (7 0 1 (7 0 1 (7 0 1 쪽쪽쪽쪽 ))))

operator+(constStonewt&, const Stonewt&) Stonewt(double) 생성자 프로그램이짧아지나시간/메모리부담 Stonewtoperator+(double); operator+(double, const Stonewt&) 프로그램이길지만, 속도가빠름

Referensi

Dokumen terkait

Depending on the hydrological analysis for the factory location that has been analyzed based on land sate DEM and special techniques as identifying the study area, through the contour

Percentage of cytoplasmic membrane integrity of the sperms Table 3 showed that percentage of cytoplasmic membrane integrity of epididymal sperms was higher than that of ejaculated