1. Trang chủ
  2. » Công Nghệ Thông Tin

Object oriented programming with C++ - Session 3 Function Overloading and References ppt

35 688 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 35
Dung lượng 101 KB

Nội dung

Object Oriented Programming with C++/ Session 3 / 1 of 35 Function Overloading and References Session 3 Object Oriented Programming with C++/ Session 3/ 2 of 35 Session Objectives  Understand the concept of functions with default arguments  Define and use Friend functions • advantages • disadvantage • friend classes  Describe function overloading • various data types • different number of arguments Object Oriented Programming with C++/ Session 3/ 3 of 35 Session Objectives (Contd.) • describe the scope for function overloading  Explain the use of reference arguments • passing references to functions • returning references from functions  Define and use Inline functions Object Oriented Programming with C++/ Session 3/ 4 of 35 Functions  A function declaration gives: • the name of the function • the type of the value returned (if any) by the function • the number and types of the arguments that must be supplied in a call of the function  A function declaration may or may not contain argument names.  Possible to call a function without specifying all its arguments. Object Oriented Programming with C++/ Session 3/ 5 of 35 Functions with default arguments  The function declaration must provide default values for those arguments that are not specified. • Whenever a call is made to a function without specifying an argument, the program will automatically assign values to the parameters from the default declaration. void func(int = 1, int = 3, char = '*'); //prototype declaration or void func(int num1,int num2 = 3,char ch = '*'); Object Oriented Programming with C++/ Session 3/ 6 of 35 Default values for arguments  Once an argument is given a default value in the list of formal arguments, all of the remaining must have default values also.  Only the trailing values can be defaulted. void func(int num1=2,int num2, char ch='+'); //error • Default values must be of the correct types or the compiler will issue an error. • Default values can be given in either the prototype or the function definition header, but not in both. • Highly recommended that the default values be given in the prototype declaration rather than in the function definition. Object Oriented Programming with C++/ Session 3/ 7 of 35 Default values for arguments (cont.)  The following calls to the function func declared above func(2,13,'+'); func(1); //default values for second and third arguments func(2,25); //default value for third argument func(); //default values for all three args func(2,,'+'); //invalid • If you leave out any arguments in the middle the compiler would not know what you are referring to and will indicate an error. Object Oriented Programming with C++/ Session 3/ 8 of 35 Advantages  Default arguments are useful if you want to use arguments, which will almost always have the same value in a function.  Also useful when, after a program is written, the programmer decides to increase the capability of a function by adding an argument. • Existing function calls can continue to use the old number of arguments, while new function calls can use more. Object Oriented Programming with C++/ Session 3/ 9 of 35 Friend Functions  Private data values cannot be read or written to by non-member functions.  We need a means to allow a function access to the private part of a class without requiring membership.  A non-member function that is allowed access to the private part of a class is called a friend of the class. Object Oriented Programming with C++/ Session 3/ 10 of 35 Friend Functions (Contd.) Class Friend Function Private! Keep out! Except members and friends [...]... display(); }; Object Oriented Scope rules (Contd.) void main() { first object1 ; second object2 ; object1 .display(); //no function overloading takes place object2 .display(); } • The scope is strictly confined to the classes in which they are declared Object Oriented Passing arguments by value Called function creates a new variable of the same type as the argument and copies the argument's value into it Function. .. many overloadings as desired provided all of the parameter patterns are unique • Many programming languages have overloaded output functions so that you can output any data with the same function name Object Oriented Overloading with different number of arguments int int int int square(int); / /function declarations square(int,int,int); asq = square(a) / /function calls bsq = square(x,y,z) Invoke the function. .. the call Only those functions that basically do the same task, on different sets of data, should be overloaded Object Oriented Advantages Eliminates the use of different function names for the same operation Helps to understand and debug code easily Maintaining code is easier Object Oriented Overloading with various data types Compiler can distinguish between overloaded functions with the same number... fn(n2); } • Function header contains an ampersand before the function name to make the function return a reference variable Object Oriented Inline Functions When a compiler sees a function call, it usually jumps to the function At the end of the function it goes back to the instruction following the function call • May save memory space but it takes some extra time To save execution time in short functions... Advantages Friend functions provide a degree of freedom in the interface design options Member functions and friend functions are equally privileged • Major difference is that a friend function is called like func(xobject), while a member function is called like xobject.func() • Designer can select the syntax that is considered most readable Object Oriented Friend classes Declare a single member function, ... else the compiler gives an error if no function produces the best match • Note that the way the compiler resolves the overloading is independent of the order in which the functions are declared • Return types of the functions are not considered Object Oriented Function overloading: Scope rules Overloading mechanism is acceptable only within the same scope of the function declaration class first{ public:... to the calling program Object Oriented Passing references to functions A reference provides an alias or an alternate name for an object void swap(int& i, int& j) { int tmp = i; i = j; j = tmp; } main() { int x, y; swap(x,y); } The ampersand (&) tells the compiler to treat the variable as a reference to the actual variable passed from the calling function Object Oriented Passing references (Contd.) Do... an object A reference is the object It is not a pointer to the object, nor a copy of the object It is the object Passing a large structure can be done very efficiently if it is passed by reference This is a form of information hiding Object Oriented Functions: Returning references Returning a reference does not return back a copy of the variable, instead an alias is returned Useful for returning objects...Friend Functions (Contd.) A function is made a friend of a class by a friend declaration in that class class person{ public: void getdata(); friend void display(person abc); }; void display(person abc) //friend function //without :: operator {//… some code…} • The keyword friend is not repeated in the function definition Object Oriented Friend Functions (Contd.) If the same function needed to access objects... having many forms Therefore functional polymorphism means one function having several forms void void void void display(); // Display functions display(const char*); display(int one, int two); display(float number); Object Oriented Function Overloading (Contd.) Compiler uses the context to determine which definition of an overloaded function is to be invoked: depends on the number and type of arguments supplied . Object Oriented Programming with C++/ Session 3 / 1 of 35 Function Overloading and References Session 3 Object Oriented Programming with C++/ Session.  Describe function overloading • various data types • different number of arguments Object Oriented Programming with C++/ Session 3/ 3 of 35 Session Objectives

Ngày đăng: 23/03/2014, 04:21

TỪ KHÓA LIÊN QUAN