tailieunhanh - Absolute C++ (4th Edition) part 16

Absolute C++ (4th Edition) part 16. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | 152 Parameters and Overloading Now suppose your program also requires a function to compute the average of three numbers. You might define a new function called ave3 as follows double ave3 double n1 double n2 double n3 return n1 n2 n3 This will work and in many programming languages you have no choice but to do something like this. However C overloading allows for a more elegant solution. In C you can simply use the same function name ave for both functions. In C you can use the following function definition in place of the function definition ave3 double ave double n1 double n2 double n3 return n1 n2 n3 so that the function name ave then has two definitions. This is an example of overloading. In this case we have overloaded the function name ave. Display embeds these two function definitions for ave into a complete sample program. Be sure to notice that each function definition has its own declaration prototype . The compiler can tell which function definition to use by checking the number and types of the arguments in a function call. In the program in Display one of the functions called ave has two arguments and the other has three arguments. When there are two arguments in a function call the first definition applies. When there are three arguments in a function call the second definition applies. Overloading a Function Name If you have two or more function definitions for the same function name that is called overloading. When you overload a function name the function definitions must have different numbers of formal parameters or some formal parameters of different types. When there is a function call the compiler uses the function definition whose number of formal parameters and types of formal parameters match the arguments in the function call. determining which definition applies Whenever you give two or more definitions to the same function name the various function definitions must have different specifications for their arguments that .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.