437 33 powerpoint-slides chapter-4-functions ch4
TRANSCRIPT
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
1/23
Programmingwith ANSI C++,2nd Edition
Prof. Bhushan Trivedi
Director
GLS Institute of Computer Technology
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
2/23
Chapter 4
Functions
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
3/23
Similarity and difference withC functions
The function calling, execution and return of aC++ function is technically similar to that of a
C function. C++ has inline functions when programmer
would like to eliminate the overhead ofcontext switching
Also, the object code of the C++ function
contains some additional information then aC function. Main is different here
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
4/23
Inline fucntionsprovide the flexibility of normal functions and provide efficiency of macrosprecede the function name with keywordinline at the time of definitionmust be defined before usageEliminate the context switching overheadIncrease the program sizeIt is a request
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
5/23
Default argumentsbool CheckPass(int TotalMarks, intPassingMarks = 50);CheckPass(int Sub1Marks, int Sub2Marks,int PassingMarks = 50)CheckPass(int Sub1Marks, int Sub2Marks,int PassingMarks1 = 50, int PassingMark2 =35);
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
6/23
Default arguments// following is wrong!CheckPass(int Sub1Marks, intPassingMarks1 = 50, int Sub2Marks, intPassingMark2 = 35);
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
7/23
Objects as parametersclass Time { }Time Difference(Time , Time);Temporary object creation and NRVoptimization
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
8/23
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
9/23
Call by and returnreference
Time AddTimes(Time & , Time & );StudList.GetStudent(OriginalName) =NewNode;Returning a temporary object as an aliasof existing variable
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
10/23
Prototype and overloadingA function prototypeMyFunction(int First, char Second)Overloading a funtionint Add(int, int);double Add(double, double);
string Add(string, string);Overloading does not involve return type
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
11/23
Other issuesFunction overloading and polymorphismUse and differences between defaultargument and function overloadingsolutionsProgram Readability and Defaultarguments
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
12/23
Member and non member We need to pass only one argument in place
of two arguments while using a member
function. In the non member case, Difference (Time1,
Time2) or Difference (Time2, Time1) can bepossible.
Non member need either public members or
be friends We do not need an explicit return statement
while using a member function.
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
13/23
Non friend non member function
The class is not cluttered with unnecessarymember functions and remainsmanageable.The function code is not affected ifprivate variables (the implementation ofthe class) changes, as the function are
not using any private variables
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
14/23
A friend functionfriend SomeFunction(list of arguments).friend void ListDeptWise(employee[]);A function is not truly a member by naturebut needs access to private membersshould not be made member but a friendFriends violate information hiding principle
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
15/23
Other issuesConst and Volatile functionsvoid PrintDetails() const { .} void CheckValuesForNIC() volatile {} Mutable data members
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
16/23
Static functionsstatic int TotalStudents;static void DispTotal();// a declarationvoid student :: DispTotal() // definition {cout
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
17/23
Static and normal functionsThey do not possess this pointer.They can not access other data membersof the class.They can not be virtual .They can not be declared either const or volatile .
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
18/23
Returning an objectemployee GetManager(employee EmpArray[])
{
return EmpArray[i]; }}employee GetEmp(string EmpName, employee
EmpArray[]){
for (int i=0;i
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
19/23
Public and Private functionsclass employee{
bool IsPermanent() /* a private function */{
return (EmpNo > 2000);}
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
20/23
Non member functionpointers
int (*PointFun)(int,int);// defining pointer to a function which hastwo arguments, both of them are integersTwo ways to call the functioncout
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
21/23
Member function pointersint PointFunEx :: PointAccess()int (PointFunEx::*PA)(); //a function pointer
PA = PointFunEx :: PointAccess;cout
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
22/23
Operators :: for defining pointer tofunction and making a call.::* operator to define pointer to amember function.* operator for accessing a member pointer using an object->* operator for accessing a member pointer using pointer to an object.
-
7/28/2019 437 33 Powerpoint-slides Chapter-4-Functions Ch4
23/23
Adding C function to aC++ program
C++ decorates the compiled function torepresent its arguments and types of them.
at run time, there is no overhead extern "C" void TestCLinkage(); extern "C" { void TestCLinkage(); void TestCLinkage2(); }