1 / 23

This

carolos
Download Presentation

This

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. 1 this ???/friend ?? ? class/operator overloading C++?? 9?? ? ? ?

    2. 2

    3. 3

    4. 4

    5. 5

    6. 6

    7. 7 ???(friend)??? ?? friend ??? ?? - ?? ???? this???? ?? ?? ??. - ???? ??(private) ??? ??? ? ??. - ? ???? ????? ?? ???? ???? ? ? ??. friend ??? ??? ? - ??? ??(operator overloading) - ?? ??? ??? ?? ??? ?? - ??? ??? ? ? ??? ?? ?? ???? ??? ?? ??? ?? friend ??? ?? - ???? ???? ? ??? ?????? ??. - friend ???? ?? friend ??? ???? ???.

    8. 8 ??????(friend??? ??)-1 #include<iostream.h> class truck; // ?? ??(forward reference) class car { int passengers; int speed; public: car(int p, int s) { passengers = p; speed = s; } friend int sp_greater(car c, truck t); // friend ??? ??: sp_greater()?? car? ???? ?? ?? };

    9. 9 ??????(friend??? ??)-2 class truck { int weight; int speed; public: truck(int w, int s) { weight = w, speed = s; } friend int sp_greater(car c, truck t); // friend ??? ??: sp_greater()?? truck? ???? ?? ?? }; int sp_greater(car c, truck t) { return c.speed – t.speed; // private? ? ???? ?? }

    10. 10 ??????(friend??? ??)-3 main() { int t; car c1(6,55), c2(2,120); truck t1(10000,55), t2(20000,72); cout << “Comparing c1 and t1 :\n”; t = sp_greater(c1,t1); if(t<0) cout << “Truck is faster.\n”; else if(t==0) cout << “Car and truck speed is the same.\n”; else cout << “Car is faster.\n”;

    11. 11 ??????(friend??? ??)-4 cout << “\n Comparing c2 and t2 :\n”; t = sp_greater(c2,t2); if(t<0) cout << “Truck is faster.\n”; else if(t==0) cout << “Car and truck speed is the same.\n”; else cout << “Car is faster.\n”; return 0; }

    12. 12

    13. 13 friend class(1) ??? ????? ?? ???? friend class ? ?? friend class Other

    14. 14 ??(friend) ??? ??? ?????. Myclass ? Other Myclass ? Other Myclass? ????? Other ???? private?? protected ????? ?? ?????? ??? ? ??. friend class(2)

    15. 15 ??? ??(operator overloading) ????? ?? ???? ?? ???? ???? ?? - ???? ??? ?? ???? ?? ??? ?? ??, ??? ??? ???. ??? ?? - ??? ??(Operator function)? ?? - ??? ??? ?? ????

    16. 16

    17. 17

    18. 18

    19. 19 ??? ??? ??(3) ??? ??? ? ? ?? ???? ?? ??? ??? ?????? ??? ?? ????? ??? ??? ? ??. w ‘+’ ???? ????? ??? ????? ????? ??. ‘=’ ???? ???? ??? ??? ?? ???? ????. ??? ??? ??? ??? ??? ????? ??. w ??? + ? ???? + ??? ??? ??? ??? ?? ? ??. ?? ?? ??? + - *(??) / % & | ~ ! , = < > <= >= ++ -- << >> == != && || += -= *= /= %= &= |= <<= >>= [] ( ) -> new delete ???? ? ?? ??? w .(???????) ::(?????) *(??? ???) ? :(??? ??) sizeof

    20. 20 ?? ???(binary operator) ? ??(1) ?? ? ?? ?????? ??

    21. 21 ?? ???(binary operator) ? ??(2) coord coord :: operator + (coord ob2) { coord temp; temp.x = x + ob2.x; temp.y = y + ob2.y; return temp; } main() { coord o1(10,10), o2(5,3), o3; int x,y; o3 = o1 + o2; // o3 = o1 + o2 + o1; o3.get_xy(x,y); // (o1+o2).get_xy(x, y); cout << “(o1+o2) X:” << x << “,Y:” << y << “\n”; return 0; }

    22. 22 ?? ???? ??(+)(3) #include<iostream.h> class myclass { int x; public : myclass() { x = 0; } myclass(int n) { x = n; } int getx() { return x; } };

    23. 23 ?? ???? ??(+)(4)

More Related