Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ support for Object-Oriented Programming

Similar presentations


Presentation on theme: "C++ support for Object-Oriented Programming"— Presentation transcript:

1 C++ support for Object-Oriented Programming
程式設計 潘仁義 CCU COMM

2 “Donut” Model of Standard Type int

3 “Donut” Model of an Abstract Data Type

4 Abstract Data Type supporting
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( )”

5 Comparison of Models of Standard Type int and Abstract Data Type Complex

6 Header File for Class Complex
Complex comp1; Complex comp2( 5.1 ); Complex comp3( 9.1, -7.2);

7 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?

8 Implementation File for Class Complex (cont’d)
有個bug, 猜猜在哪

9 Driver Function to Test Class Complex

10 Step-by-Step Evaluation of Multiple << Operations
ostream& operator<< (ostream& os, Complex c) { … os << fixed << showpoint << setprecision(2); … return os; }

11 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


Download ppt "C++ support for Object-Oriented Programming"

Similar presentations


Ads by Google