Document Details

Uploaded by Deleted User

Shree Swaminarayan College of Computer Science

Gaurang Bhatt

Tags

C++ programming OOP concepts inline functions computer science

Summary

This document provides lecture notes on C++ programming, specifically focusing on Unit 2: Functions, Constructors, and Destructors. The content details inline functions, including their characteristics and use cases. Includes examples and explanations related to this topic

Full Transcript

Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-2 Unit-2: Functions, Constructors and Destructor 1. Inline function 2. Friend Function 3. Static member function with static data member 4...

Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-2 Unit-2: Functions, Constructors and Destructor 1. Inline function 2. Friend Function 3. Static member function with static data member 4. Constructor – Types of constructor, characteristics of constructor 5. Constructor overloading 6. Constructors V/S Member functions 7. Destructor 8. Compare Constructor with Destructor Prepared By: Gaurang Bhatt B.Sc IT-Sem-3 Page 1 of 33 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-2 Q.1 Explain inline function in details. Inline function in C++ is an enhancement feature that improves the execution time and speed of the program. The main advantage of inline functions is that you can use them with C++ classes as well. When an instruction of a function call is encountered during the compilation of a program, its memory address is stored by the compiler. The function arguments are copied on the stack, jumping to the function body and after the execution of the code, the control is transferred to the calling instruction. This process can sometimes cause overhead in function calls, especially if the function is small and its execution time is less than the switching time. This issue is resolved by using the inline functions. These functions overcome the overhead and also make the program faster by reducing the execution time of the program. In case of inline functions, the compiler does not go through the above process of switching between the stack and calling function. Instead, it copies the definition of the inline function and the calling instruction with that definition. A function defined in the body of a class declaration is implicitly an inline function. Characteristics of inline functions: Inline a function is not mandatory. It is considered just a request, and its execution totally depends on the compiler. The compiler decides whether to accept or decline the request of inline the function based on some circumstances as discussed below: 1. The compiler denies the request to make a function an inline function if it contains a return statement but does not return anything. 2. All those functions that contain a loop statement (for loop, while loop, or do-while loop) cannot be considered as an inline function by the compiler. 3. The compiler will not consider a function to be an inline function if it is recursive. 4. If the function contains one or more static variables, the compiler will deny the request to make it an inline function. 5. If the function contains a switch or a go-to statement, the compiler will deny the suggestion to make it an inline function. The following demonstration illustrates syntax and the working of inline function in C++: Prepared By: Gaurang Bhatt B.Sc IT-Sem-3 Page 2 of 33 Shree Swaminarayan College of Computer Science, Sardarnagar, Bhavnagar 305. Object Oriented Programming (OOP) USING C++ I Unit-2 Syntax: inline function-header { function-body } For example; class number { public: inline void square(float x1) { cout

Use Quizgecko on...
Browser
Browser