1. Prepared By: Asst. Prof. Sejal Jadav
Unit-1
Principles of Object Oriented Programming Tokens,
expressions & Control Statements
(14 MARKS)
B.C.A & B.Sc.(IT) – 3
CS-13 C++ and Object Oriented
Programming
2. Prepared By: Asst. Prof. Sejal Jadav
Inline function
Benefits of Function:
• Easy to read
• Easy to modify
• Avoids rewriting of same code
• Easy to debug
• Better memory utilization
3. Prepared By: Asst. Prof. Sejal Jadav
•Function saves memory
•Function is time consuming
4. Prepared By: Asst. Prof. Sejal Jadav
Inline function
•To eliminate the cost of calls to small functions,
C++ proposes a new feature called inline
function.
•An inline function is a function that is expanded
in line when it is invoked.
5. Prepared By: Asst. Prof. Sejal Jadav
•Inline is a request not a command.
•The benefit of speed of inline functions reduces
as the function grows in size.
6. Prepared By: Asst. Prof. Sejal Jadav
•So the compiler may ignore the request in some
situations. Few of them:
• Function containing loops, switch, goto.
• Functions with recursion.
•Containing static variable.
7. Prepared By: Asst. Prof. Sejal Jadav
Syntax:
inline<function header>
{
Function body
}
10. Prepared By: Asst. Prof. Sejal Jadav
Default Arguments in C++ Functions
• The default arguments are used when you provide no
arguments or only few arguments while calling a
function.
• The default arguments are used during compilation of
program.
• For example, lets say you have a user-defined function
sum declared like this: int sum(int a=10, int b=20)
11. Prepared By: Asst. Prof. Sejal Jadav
• For example: int sum(int a=10, int b=20)
• Now while calling this function you do not provide
any arguments, simply called sum(); then in this case
the result would be 30, compiler used the default
values 10 and 20 declared in function signature.
12. Prepared By: Asst. Prof. Sejal Jadav
•For example: int sum(int a=10, int b=20)
•If you pass only one argument like this:
sum(80) then the result would be 100, using
the passed argument 80 as first value and 20
taken from the default argument.
•Let’s look at example:01_defaultArg.Cpp
13. Prepared By: Asst. Prof. Sejal Jadav
Rules of default arguments
• As you have seen in the above example: int sum(int a, int
b=10, int c=20); that I have assigned the default values
for only two arguments b and c during function
declaration.
• It is up to you to assign default values to all arguments or
only selected arguments but remember the following rule
while assigning default values to only some of the
arguments:
14. Prepared By: Asst. Prof. Sejal Jadav
•If you assign default value to an argument, the
subsequent arguments must have default
values assigned to them, else you will get
compilation error.
15. Prepared By: Asst. Prof. Sejal Jadav
• For example: Lets see some valid and invalid cases.
• Valid: Following function declarations are valid –
int sum(int a=10, int b=20, int c=30);
int sum(int a, int b=20, int c=30);
int sum(int a, int b, int c=30);
16. Prepared By: Asst. Prof. Sejal Jadav
• Invalid: Following function declarations are invalid –
• Since a(a variable) has default value assigned, all the
arguments after a (in this case b and c) must have
default values assigned
int sum(int a=10, int b, int c=30);
17. Prepared By: Asst. Prof. Sejal Jadav
• Since b has default value assigned, all the arguments
after b (in this case c) must have default values
assigned.
int sum(int a, int b=20, int c);
18. Prepared By: Asst. Prof. Sejal Jadav
• Since a has default value assigned, all the arguments
after a (in this case b and c) must have default values
assigned, b has default value but c doesn't have, that’s
why this is also invalid
int sum(int a=10, int b=20, int c);
19. Prepared By: Asst. Prof. Sejal Jadav
Const arguments
• The constant variable can be declared using
const keyword.
• The const keyword makes variable value stable.
• The constant variable should be initialized
while declaring.
20. Prepared By: Asst. Prof. Sejal Jadav
•Syntax :
<function name> (const <type> <variable name>);
•The qualifier ‘const’ tells the compiler that the
function should not modify the argument.
•Let’s Look at example: DefaultArg.cpp
21. Prepared By: Asst. Prof. Sejal Jadav
void add(const int a, int b)
{
a=a+b; //error
b=a+b;
cout<<“Sum = “<<b;
}
main()
{
add(2,5);
}
• In the above program, the prototype
of function add declared two
arguments.
• Out of two arguments the first
argument is a constant. The second
argument is not constant.
• When function add is invoked, the
first argument a is a const according
to prototype of function.
• Any attempt made to change the
value of variable a will cause an
error message “cannot modify a
constant variable.”
22. Prepared By: Asst. Prof. Sejal Jadav
Function overloading
•Overloading refers to the use of the same
thing for different purposes.
•C++ permits overloading of functions as it
supports the concept of polymorphism.
That is also known as function
polymorphism in OOP.
23. Prepared By: Asst. Prof. Sejal Jadav
•It means that we can use the same function
name to create different functions that
perform a variety of different tasks.
•Function Overloading is the process of
using a single function name to perform
different types of tasks.
24. Prepared By: Asst. Prof. Sejal Jadav
• Using this concept we can design family of functions
with one function name but with different argument
list.
• The function would perform different operations
depending on the argument list in the function call.
• The correct function is invoked by checking the
number and type of arguments but not the function
type.
25. Prepared By: Asst. Prof. Sejal Jadav
• Let’s look at the example: Overloading_fun.cpp