Chapter 15 C Function - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 15 C Function

Description:

Array of Pointers: Store array of strings. Assume that array w has 5 cells, each stores an ... References. Deitel & Deitel: C How to Program, 4th ed., Chapter ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 46
Provided by: radf7
Category:

less

Transcript and Presenter's Notes

Title: Chapter 15 C Function


1
Chapter 15 C Function
  • By C. Shing
  • ITEC Dept
  • Radford University

2
Objectives
  • 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

3
Dynamic 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

4
Dynamic 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

5
Scope 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

6
Scope Rule (Cont.)
  • Example
  • int number 10
  • Int main()
  • int number 100
  • cout ltlt local number ltltnumber
  • cout ltlt global number ltltnumber

7
Pass By Reference
  • The function directly access the variables passed
  • in using reference.
  • (This is different from passing pointers)

8
Pass By Reference (Cont.)
  • Example
  • Int main()
  • int number10
  • byRef(number)
  • cout ltlt number // print 100
  • void byRef(int number)
  • number100

9
Pass 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

10
Pass 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)

11
Pass By Reference Example (Cont.)
  • Example (Cont.) You may also write it as
  • void swap (char s, char t)
  • char tmp
  • tmps
  • st
  • ttmp

12
Pass By Reference Example (Cont.)
  • Without using class
  • bubble.cpp
  • bubble_data.txt
  • Using class
  • bubbleclass.cpp
  • bubble_data.txt

13
Pass 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)

14
Pass By Pointer Example (Cont.)
  • Example (Cont.) The swap function is
  • void swap (char s, char t)
  • char tmp
  • tmps
  • st
  • ttmp

15
Pass By Pointer Example (Cont.)
  • Using class
  • bubbleclassPtr.cpp
  • bubble_data.txt

16
Functions
  • 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.

17
Functions (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

18
Functions (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)

19
Functions (Cont.)
  • Template
  • Template function same task for different
  • data types
  • Class template specify template for entire class
    member data
  • and member functions

20
Functions (Cont.)
  • Virtual function used to specify interface
  • function and will be implemented in various
  • inherited classes (discussed in Polymorphism)

21
Member Function Default Constructor
  • Form
  • Class Name (type default value, )
  • Example
  • default constructor for 2D Point class
  • Point (int0, int0)

22
Member 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

23
Member 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

24
Member function inline and constant function
Example
  • Using class a 3D Point class
  • point.cpp
  • Use this pointer for cascading member
  • function call
  • pointThis.cpp

25
Member 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

26
Member 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

27
Member function friend function Example
  • Using class a 3D Point class
  • pointFriend.cpp

28
Member 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

29
Member 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

30
Member function static (Cont.)
  • Example (Cont.)
  • // static function definition
  • int Pointhowmany()
  • return pointCount
  • // main function
  • int main()
  • coutltlt "Total number of points created
    "ltltPointhowmany()ltltendl

31
Member function static function Example
  • Using class a 3D Point class
  • pointStatic.cpp

32
Operator 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

33
Operator 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

34
Operator 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'

35
Operator Overloading (Cont.) Example
  • Using class a 3D Point class
  • overload.cpp

36
Template Function
  • Form
  • template ltclass T1, class T2gt
  • returntype functionname (T1 var1 , T2 var2)

37
Template Function (Cont.)
  • Example template function for swap
  • template ltclass Tgt
  • void Bubbleswap(T x , T y)
  • T tmp
  • tmp x
  • x y
  • y tmp

38
Template Function (Cont.)
  • bubbleclass_template.cpp
  • bubble_data.txt

39
Class template
  • Form
  • template ltclass Tgt
  • class classname
  • public
  • classname()
  • returntype memberfunction (T var1, )
  • T memberfunction (T var1, )
  • classname()
  • private
  • T var3
  • int var4,

40
Class template (Cont.)
  • Form (Cont.)
  • template ltclass Tgt
  • classnameltTgtclassname ()

41
Class 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)

42
Class template (Cont.)
  • Example A Bubble sort class (Cont.)
  • template ltclass Tgt
  • BubbleltTgtBubble()
  • numStudents0
  • while (cingtgtstudentnumStudents)
  • numStudents

43
Class template (Cont.)
  • Complete Example
  • No template
  • bubbleclass_string.cpp
  • bubble_data.txt

44
Class template (Cont.)
  • Complete Example (Cont.)
  • Use class template
  • bubble_classtemplate1.cpp
  • bubbleclasstemplate1_data.txt (string data)
  • bubble_classtemplate2.cpp
  • bubbleclasstemplate2_data.txt (int data)

45
References
  • Deitel Deitel C How to Program, 4th ed.,
  • Chapter 15, 16 17, Prentice Hall
Write a Comment
User Comments (0)
About PowerShow.com