Title: Chapter 15 C Function
1Chapter 15 C Function
- By C. Shing
- ITEC Dept
- Radford University
2Objectives
- Understand how to create and free dynamic
- memory
- Understand the scope rule
- Understand pass by reference
- Understand member functions
- Know how to write inline functions
- Understand function overloading
- Understand static function
- Understand template function and virtual function
3Dynamic Memory
- Create a memory pointed by memPtr
- (calls constructor)
- Form type memPtrnew type
- Example int numberPtrnew int
- Destroy the dynamic memory memPtr
- (calls destructor)
- Form delete memPtr
- Example delete memPtr
4Dynamic Memory (Cont.)
- Create an array of memory
- pointed by arrayPtr
- Form type arrayPtrnew typeSIZE
- Example int numberarrayPtrnew intSIZE
- Destroy the dynamic array memory arrayPtr
- Form delete arrayPtr
- Example delete numberarrayPtr
5Scope Rule
- The variable is meaningful and unique
- in its defined block
- The local variable redefined scope precedes
- the global variable if use the same name.
- The global variable can be referred using
- local variable name
6Scope Rule (Cont.)
- Example
- int number 10
- Int main()
-
- int number 100
- cout ltlt local number ltltnumber
- cout ltlt global number ltltnumber
7Pass By Reference
- The function directly access the variables passed
- in using reference.
- (This is different from passing pointers)
-
8Pass By Reference (Cont.)
- Example
- Int main()
-
- int number10
- byRef(number)
- cout ltlt number // print 100
-
- void byRef(int number)
-
- number100
-
9Pass By Reference - Example
- Array of Pointers Store array of strings
- Assume that array w has 5 cells, each stores an
- address of strings as follows
- w0100address of string this
- w1200address of string is
- w2300address of string a
- w3400address of string snow
- w4500address of string day
10Pass By Reference Example (Cont.)
- Bubble sort for array w
- To call sort_strings sort_strings(w, size)
- To use bubble sort to sort the array w
- void sort_strings (char w, int n)
-
- int i, j
- for (i0 iltn i)
- for (ji1 jltnj)
- if (strcmp(wi,wj)gt0)
- swap (wi, wj)
11Pass By Reference Example (Cont.)
- Example (Cont.) You may also write it as
- void swap (char s, char t)
-
- char tmp
- tmps
- st
- ttmp
12Pass By Reference Example (Cont.)
- Without using class
- bubble.cpp
- bubble_data.txt
- Using class
- bubbleclass.cpp
- bubble_data.txt
13Pass By Pointer Example (Cont.)
- Bubble sort for array w
- To use bubble sort to sort the array w
- void sort_strings (char w, int n)
-
- int i, j
- for (i0 iltn i)
- for (ji1 jltnj)
- if (strcmp(wi,wj)gt0)
- swap (wi, wj)
14Pass By Pointer Example (Cont.)
- Example (Cont.) The swap function is
- void swap (char s, char t)
-
- char tmp
- tmps
- st
- ttmp
15Pass By Pointer Example (Cont.)
- Using class
- bubbleclassPtr.cpp
- bubble_data.txt
16Functions
- member functions
- only functions that can access member in the
class - Default constructor missing parameters will be
- initialized automatically by the default values
- inline function
- short function that can fit in one line
- constant function function that does not change
any - member data, not allowed for constructor or
destructor. - A non-constant function is not allowed to
access - constant object.
17Functions (Cont.)
- friend function
- not a member function but can access member
- data
- static function (no this pointer available)
- function that returns
- static variable (share among all objects
created) - without any object exists
18Functions (Cont.)
- Overloading
- Function overloading function with different
- argument list, each function performs different
- task
- Operator overloading rewrite rule for existing
- operator on object (not change operator
precedence - nor the operator characteristics)
19Functions (Cont.)
- Template
- Template function same task for different
- data types
- Class template specify template for entire class
member data - and member functions
20Functions (Cont.)
- Virtual function used to specify interface
- function and will be implemented in various
- inherited classes (discussed in Polymorphism)
21Member Function Default Constructor
- Form
- Class Name (type default value, )
- Example
- default constructor for 2D Point class
- Point (int0, int0)
22Member function - inline
- Example
- Class 3DPoint
-
- public
- 3DPoint(double 0.0, double 0.0,
- double 0.0)
- double getX() return x // this is inline
function -
- private
- double x, y, z
-
23Member function - constant
- Example
- Class 3DPoint
-
- public
- 3DPoint(double 0.0, double 0.0,
- double 0.0)
- double getX() const return x // constant
function -
- private
- double x, y, z
-
24Member function inline and constant function
Example
- Using class a 3D Point class
- point.cpp
- Use this pointer for cascading member
- function call
- pointThis.cpp
25Member function - friend
- Example
- Class Point
-
- friend Point setPoint(double, double, double)
- public
- Point(double 0.0, double 0.0,
- double 0.0)
- double getX() const return x // constant
function -
- private
- double x, y, z
-
26Member function friend (Cont.)
- Example (Cont.)
- Point setPoint(Point p,
- double sx, double sy, double sz)
-
- p.x sx
- p.y sy
- p.z sz
- return p
-
27Member function friend function Example
- Using class a 3D Point class
- pointFriend.cpp
-
28Member function - static
- Example
- Class Point
-
- friend Point setPoint(double, double, double)
- public
- Point(double 0.0, double 0.0,
- double 0.0)
-
- // static function to access static member data
- static int howmany ()
- private
- double x, y, z
- static int pointCount
-
29Member function static (Cont.)
- Example (Cont.)
- // initialize static member data
- int PointpointCount0
- // constructor
- PointPoint(double sx, double sy, double sz)
- // member initializer assign sx to x, sy to y
and sz to z - x(sx), y(sy), z(sz)
-
- pointCount
30Member function static (Cont.)
- Example (Cont.)
- // static function definition
- int Pointhowmany()
-
- return pointCount
-
- // main function
- int main()
-
-
- coutltlt "Total number of points created
"ltltPointhowmany()ltltendl
31Member function static function Example
- Using class a 3D Point class
- pointStatic.cpp
-
32Operator Overloading
- Example
- Class Point
-
- friend istream
- operatorgtgt (istream , Point )
- public
- Point(double 0.0, double 0.0,
- double 0.0)
-
- private
- double x, y, z
33Operator Overloading (Cont.)
- Example (Cont.) // input form (m,n,r)
- istream operatorgtgt (istream input, Point p)
-
- input.ignore()
- input gtgt p.x
- input.ignore()
- input gtgt p.y
- input.ignore()
- input gtgt p.z
- input.ignore(5,'\n')
- return input
34Operator Overloading (Cont.)
- Example (Cont.)
- int main()
-
- Point p, q
- p.output()
- coutltlt"Please enter in 2 points in the form
(m,n,r), - ltlt e.g. (1.2,3.45,67.891)"
- cin gtgt p gtgt q
- p.output()
- q.output()
- cout ltlt '\n'
35Operator Overloading (Cont.) Example
- Using class a 3D Point class
- overload.cpp
-
36Template Function
- Form
- template ltclass T1, class T2gt
- returntype functionname (T1 var1 , T2 var2)
-
-
-
37Template Function (Cont.)
- Example template function for swap
- template ltclass Tgt
- void Bubbleswap(T x , T y)
-
- T tmp
- tmp x
- x y
- y tmp
-
38Template Function (Cont.)
- bubbleclass_template.cpp
- bubble_data.txt
39Class template
- Form
- template ltclass Tgt
- class classname
- public
- classname()
- returntype memberfunction (T var1, )
- T memberfunction (T var1, )
- classname()
- private
- T var3
- int var4,
-
40Class template (Cont.)
- Form (Cont.)
- template ltclass Tgt
- classnameltTgtclassname ()
-
-
-
41Class template (Cont.)
- Example A Bubble sort class
- template ltclass Tgt
- class Bubble
- public
- Bubble()
- void PrintData()
- void sort()
- Bubble()
- private
- T studentMAX_CLASS_SIZE
- int numStudents
- void swap(T x , T y)
-
42Class template (Cont.)
- Example A Bubble sort class (Cont.)
- template ltclass Tgt
- BubbleltTgtBubble()
-
- numStudents0
- while (cingtgtstudentnumStudents)
- numStudents
-
-
43Class template (Cont.)
- Complete Example
- No template
- bubbleclass_string.cpp
- bubble_data.txt
44Class template (Cont.)
- Complete Example (Cont.)
- Use class template
- bubble_classtemplate1.cpp
- bubbleclasstemplate1_data.txt (string data)
- bubble_classtemplate2.cpp
- bubbleclasstemplate2_data.txt (int data)
45References
- Deitel Deitel C How to Program, 4th ed.,
- Chapter 15, 16 17, Prentice Hall