110 likes | 120 Views
C++ support for Object-Oriented Programming. 程式設計 潘仁義 CCU COMM. “Donut” Model of Standard Type int. “Donut” Model of an Abstract Data Type. C does not allow to define operators Complex + Complex ? C permits only one meaning for a name abs( ), labs( ), llabs( ), fabs( ) ?.
E N D
C++ support for Object-Oriented Programming 程式設計 潘仁義 CCU COMM
C does not allow to define operators Complex + Complex ? C permits only one meaning for a name abs( ), labs( ), llabs( ), fabs( ) ? C++ supports Class definition to group data structure and operations of an abstract data type supports operator overloading to include definitions of arithmetic and input/output operators supports function overloading Ex. Using only one “abs( )” Abstract Data Type supporting
Comparison of Models of Standard Type int and Abstract Data Type Complex
Header File for Class Complex • Complex comp1; • Complex comp2( 5.1 ); • Complex comp3( 9.1, -7.2);
Implementation File for Class Complex • “this” • Member functions can access private data member • Member operators have direct access to the members of their first operand • If member function/operator does not change data members • Prototype ends in “const” • Why?
Implementation File for Class Complex (cont’d) 有個bug, 猜猜在哪
Step-by-Step Evaluation of Multiple << Operations • ostream& operator<< (ostream& os, Complex c) {…os << fixed << showpoint << setprecision(2);…return os;}
Q & A • Shadow copy vs. Deep copy • A shallow copy of an object copies all of the member field values • The default copy constructor and assignment operator make shallow copies • May not be what you want for fields that point to dynamically allocated memory • Deep copies need ... • Destructor • Copy constructor • Overloaded assignment operator