SlideShare a Scribd company logo
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1
Haresh Jaiswal
Rising Technologies, Jalna.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 2
Function Overview
 Function plays an important role in Program
development.
 Dividing program logic in functions/sub-programs is one
of the major principles of top-down, structured
programming.
 Another advantage of using function is that it is possible
to reduce the size of program by calling & using them at
multiple times in the program.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 3
Function Overview
void show(); // declaration/prototype
main()
{
...
show(); // function call
...
}
void show() // function definition
{
... // function body
...
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 4
Functions in C++
 C++ has added many new features to functions to make
them more reliable and flexible.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 5
What is new in C++ Functions
 Call by Reference using Reference Variable.
 Return by Reference.
 Inline Functions.
 Default Arguments.
 Const Arguments.
 Function Overloading.
 Friend & Virtual Functions.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 6
Call By Reference vs. Call By value
 In traditional C, a function call passes arguments by its
value. The called function creates new set of variables
and copies the value of arguments into them.
 The called function does not have access to the actual
variables in the calling program and can only work on
the copies of values.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 7
Example of Call By Value.
int sum(int, int);
main()
{
int a = 4, b = 3, c;
c = sum(a, b);
cout << “Add is : “ << c;
}
int sum(int x, int y)
{
int addition = x + y;
return addition;
}
Add is : 7
Output
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 8
Call By Reference vs. Call By value
 This mechanism is fine if the function does not need to
modify the values of the original variables in the calling
program.
 But there may arise some situations where we would
like to change the values of variables in the calling
program.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 9
Call By Reference.
 Provisions of the reference variable in c++ permits us to
pass parameters to the function by reference.
 When we pass arguments by reference, the ‘formal’
arguments in the called function became aliases of
‘actual’ arguments in the calling program.
 This means when the called function is working with its
own arguments, it is actually working on the original
variables of calling function.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 10
Call By Reference.
void swap(int&, int&);
main()
{
int a = 10, b = 20;
swap(a, b);
cout << “A : “ << a << endl << “B : “ << b;
}
// x and y are references
void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
A : 20
B : 10
Output
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 11
Call By Reference.
 When the function call like
swap(a, b);
executes, then the following initialization occurs.
int &x = a;
int &y = b;
 Any changes made to variable ‘x’ and ‘y’ in swap function
will reflect to variables ‘a’ and ‘b’ of main, because ‘x’ and
‘y’ are simply aliases of ‘a’ and ‘b’
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 12
Call By Reference using pointers.
void swap(int*, int*);
main()
{
int a = 10, b = 20;
swap(&a, &b);
cout << “A : “ << a << endl << “B : “ << b;
}
// x and y are now pointers
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
A : 20
B : 10
Output
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 13
Return By Reference.
 In C++, not only you can pass arguments by reference but
also you can return a reference from a function.
 When a function returns a reference, it returns an implicit
pointer to its return value. This way, a function can be
used on the left hand side of an assignment operator.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 14
Return By Reference.
int n; // a global variable
int& test(); // function prototype
main()
{
test() = 5;
cout << “N : “ << n;
}
int& test()
{
return n;
}
N : 5
Output
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 15
Return By Reference : Explanation
 In this example, the return type of function test() is int&.
Hence this function returns a reference.
 The return statement is return n; but unlike return by
value. This statement doesn't return value of n, instead it
returns variable n itself.
int& test()
{
return n;
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 16
Return By Reference : Explanation
 When the function is called from main the yield of the
function call is variable n itself, so when the following
statement in main executes it assigns 5 to variable n
test() = 5;
 In simple words, variable n is assigned at the left hand
side of statement test() = 5;
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 17
Points to keep in mind.
 You cannot return a local variable (which is non-static)
from a function which returns a reference.
 Following piece of code will generate a compile error.
int& test()
{
int n; // n is local variable
return n; // compile error
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 18
Points to keep in mind.
 But if I change int n; to static int n; then
 Following piece of code will be successfully compiled and
run perfectly.
int& test()
{
static int n; // n is now a static
// local variable
return n; // perfectly fine
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 19
Points to keep in mind.
 Ordinary function returns value but this function doesn't.
Hence, you can't return constant from this function.
 Following piece of code will generate a compile error.
int& test()
{
return 2; // compile error
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 20
Return By Reference.
 A function can also return a reference.
int& max(int &x, int &y)
{
if(x>y)
return x;
else
return y;
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 21
Return By Reference.
 Since the return type of max() is int &, the
function returns reference to x or y.
 The function call such as max(a, b) will return a
reference either to a or b depending on their
values. That means the function call can
appear on the left hand side of an assignment
operator.
 max(a, b) = 0;
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 22
Inline Functions
 Every time a function get called it takes a lot of extra
time in executing a series of instructions for tasks, such
as jumping to the function, saving registers, pushing
arguments into the stack, and returning to the calling
function.
 solution to solve this problem is to use inline functions.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 23
Inline Functions
 An Inline function is a function that is expanded in line
when it is invoked.
 The compiler replaces the function call with the
corresponding function code.
 Inline expansion makes program run faster because the
overhead of a function call and return is eliminated.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 24
Inline Functions
inline double cube(double n)
{
return (n*n*n);
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 25
Inline Functions
 Remember that inline keyword sends a request, not a
command, to the compiler.
 The compiler may ignore this request if the function
definition is too long or too complicated.
 In such cases compiler will compile the function as a
normal function.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 26
Inline Functions
 Some of the situations where inline expansion may not
work.
 If function contains a loop, switch or goto statement.
 If function contains static variables.
 If function is a recursive function.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 27
Default arguments
 C++ allows us to call a function without specifying all
its arguments. In such cases, the function assigns a
default value to the parameters.
 Default values are specified when the function is
declared.
 The compiler looks at the prototype to see how many
arguments a function uses and alerts the program for
default values.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 28
Default arguments
 Consider the following prototype.
float interest(float amt, int period, float irate=7.2);
 The default value is specified in a manner similar to a
variable initialization.
 Above prototype declares a default value of 7.2 to the
argument irate.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 29
Default arguments
 Consider the following prototype.
float interest(float amt, int period, float irate=7.2);
 A function call like
x = interest(5000, 8); // one argument missing
 Passes the value 5000 to amt, and 8 to period and let the
function use default value of 7.2 for irate.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 30
Default arguments
 Consider the following prototype.
float interest(float amt, int period, float irate=7.2);
 A function call like
x = interest(5000, 8, 6.3); //no missing argument
 Passes the value 5000 to amt, and 8 to period, and 6.3 for
irate.
 Passes an explicit value of 6.3 to irate
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 31
Default arguments
 One important point to note is that the only trailing
arguments can have default values.
 We must add defaults from right to left.
 We cannot provide a default value to a particular
argument in the middle of an argument list.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 32
Default arguments
 Some Examples :
int mul(int i, int j=5, int k=10); // legal
int mul(int i, int j=5, int k); // illegal
int mul(int i=2, int j, int k=2); // illegal
int mul(int i=5, int j=2, int k=4); // legal
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 33
Default arguments
 Default arguments are useful in situations where some
arguments always have the same value.
 For example bank interest may retain same for all
customers for a particular period of deposits.
 It also provides a greater flexibility to the programmers by
sending all arguments explicitly.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 34
const arguments
 In C++, an argument to a function can be declared as
const,
int length(const char p[]);
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 35
const arguments
 The qualifier const tells the compiler that the
function should not modify the argument.
 The compiler will generate an error when this
condition is violated. This type of declaration is
significant only when we pass arguments by
reference or pointers.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 36
Introduction to Class
 Like structures in C, class is a user defined data type in
C++.
 A Class is extension of the idea of structures used in C.
 It is a new way of creating and implementing a user
defined data type.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 37
Structures Revised
 Structures in C provides a method of packing different
type of data together.
 A structure is a convenient tool for handling a group of
logically related data items.
 Once the structure type has been defined, we can
create any number of variables of that type.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 38
Structures Revised
 Consider following structure.
struct student
{
int rollno;
char name[25];
int marks;
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 39
Structures Revised
 Consider following Declaration.
struct student a;
 ‘a’ is a variable of type ‘student’ and
contains 3 member variables, which
can be accessed by using ‘.’ operator.
a.rollno = 15;
strcpy(a.name, “Aditya”);
a.marks = 435;
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 40
Limitations of C Structures
 C structures do not provide the concept of data
hiding.
 Structure members can be directly accessed by the
structure variables by any function in their scope.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 41
Extensions to Structures in C++
 C++ supports all features of structures as defined in C,
in addition C++ has expanded its capabilities to suit its
OOP philosophy.
 It attempts to bring the user-defined types as close as
possible to built in data types, and also provides a
facility to hide the data which is the major principle of
OOP.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 42
Extensions to Structures in C++
 In C++ a structure can contain variables and functions
both as its member.
 It can also declare some of its members as ‘private’ so
that they cannot be accessed directly by the external
functions.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 43
Extensions to Structures in C++
 In C++, the structure names are stand alone and can
be used like any other type names.
 In other words the keyword ‘struct’ can be omitted
from the declaration of the structure variables.
 For example.
student a; // in c++
struct student a; // in c
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 44
Introduction to Class
 C++ incorporates all of these extensions in
another user defined data type ‘class’.
 There is a very little syntactical difference
between structure and classes in c++.
 The only difference is that by default the
members of a class are private, while, by default,
the members of a structure are public.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 45
Specifying a Class
 A class is a way of binding data and its associated
functions together, it allows the data (and
functions) to be hidden from external world, if
necessary.
 While defining a class we are creating a new
abstract data type, that can be treated like any
other built in data type.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 46
Specifying a Class
 Generally a class specifications has two parts.
 Class declaration.
 Class function definitions.
 The class declaration describes the type and the
scope of its members.
 The class function definitions describes how the
class functions are implemented.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 47
General form of a Class
class class_name
{
private: // optional
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 48
General form of a Class
 The functions and variables declared in a class
are called as class members.
 The class members declared as private can be
accessed only within the class, and the
members declared as public can be accessed
from outside of the class also.
 The variables declared inside a class are called
as data members, and the functions are called
as member functions.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 49
Public Area
Data
Functions
General form of a Class
Class
Entry not allowed for
outside world
X
Entry allowed for
outside world
Private Area
Data
Functions

More Related Content

PDF
03. oop concepts
PDF
06. operator overloading
PDF
05. inheritance
PDF
01. introduction to C++
PDF
Classes and objects
PPTX
#OOP_D_ITS - 4th - C++ Oop And Class Structure
PDF
Operator overloading
03. oop concepts
06. operator overloading
05. inheritance
01. introduction to C++
Classes and objects
#OOP_D_ITS - 4th - C++ Oop And Class Structure
Operator overloading

What's hot (20)

PPT
Operator overloading
PPTX
operator overloading & type conversion in cpp over view || c++
PPT
08 c++ Operator Overloading.ppt
PDF
Develop Embedded Software Module-Session 3
PPT
Bca 2nd sem u-2 classes & objects
PPT
Constructor and destructor in C++
PDF
VTU PCD Model Question Paper - Programming in C
PPT
14 operator overloading
PDF
Develop Embedded Software Module-Session 2
PPTX
Function in C program
PPT
Operator overloading
PDF
C++ book
PPT
Pointer and Object in C++
PDF
Functions
PPT
C++ Functions
PPTX
Unary operator overloading
PPT
C++ functions
PDF
Pragmatic functional refactoring with java 8
PPT
PPT
Operator overloading
Operator overloading
operator overloading & type conversion in cpp over view || c++
08 c++ Operator Overloading.ppt
Develop Embedded Software Module-Session 3
Bca 2nd sem u-2 classes & objects
Constructor and destructor in C++
VTU PCD Model Question Paper - Programming in C
14 operator overloading
Develop Embedded Software Module-Session 2
Function in C program
Operator overloading
C++ book
Pointer and Object in C++
Functions
C++ Functions
Unary operator overloading
C++ functions
Pragmatic functional refactoring with java 8
Operator overloading
Ad

Viewers also liked (6)

PPTX
Inline function in C++
PPT
16717 functions in C++
 
PPTX
C++ programming function
PPT
C++ Function
PPT
FUNCTIONS IN c++ PPT
PPTX
functions of C++
Inline function in C++
16717 functions in C++
 
C++ programming function
C++ Function
FUNCTIONS IN c++ PPT
functions of C++
Ad

Similar to 02. functions & introduction to class (20)

PPSX
Chapter-4_OOP aare temi Lena na petna chho loda
PDF
Functions in C++.pdf
PPT
Unit iv functions
PPTX
Inline function
PDF
Preprocessor directives
PDF
04. constructor & destructor
PPT
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
PPT
11 functions
PPTX
6. Functions in C ++ programming object oriented programming
PPTX
Functionincprogram
PPTX
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
PPTX
Functions in c++
PPTX
structured Programming Unit-7-Functions.pptx
PPT
C++ functions
PPTX
07. Virtual Functions
PPS
C programming session 08
DOCX
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
PPT
C++ functions presentation by DHEERAJ KATARIA
PPT
Functions and pointers_unit_4
PDF
Chapter 1. Functions in C++.pdf
Chapter-4_OOP aare temi Lena na petna chho loda
Functions in C++.pdf
Unit iv functions
Inline function
Preprocessor directives
04. constructor & destructor
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
11 functions
6. Functions in C ++ programming object oriented programming
Functionincprogram
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
Functions in c++
structured Programming Unit-7-Functions.pptx
C++ functions
07. Virtual Functions
C programming session 08
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
C++ functions presentation by DHEERAJ KATARIA
Functions and pointers_unit_4
Chapter 1. Functions in C++.pdf

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PDF
Sports Quiz easy sports quiz sports quiz
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Lesson notes of climatology university.
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Types and Its function , kingdom of life
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Cell Structure & Organelles in detailed.
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pharma ospi slides which help in ospi learning
Sports Quiz easy sports quiz sports quiz
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Lesson notes of climatology university.
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPH.pptx obstetrics and gynecology in nursing
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial diseases, their pathogenesis and prophylaxis
Cell Types and Its function , kingdom of life
O7-L3 Supply Chain Operations - ICLT Program
Cell Structure & Organelles in detailed.
Microbial disease of the cardiovascular and lymphatic systems
Anesthesia in Laparoscopic Surgery in India
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O5-L3 Freight Transport Ops (International) V1.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

02. functions & introduction to class

  • 1. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1 Haresh Jaiswal Rising Technologies, Jalna.
  • 2. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 2 Function Overview  Function plays an important role in Program development.  Dividing program logic in functions/sub-programs is one of the major principles of top-down, structured programming.  Another advantage of using function is that it is possible to reduce the size of program by calling & using them at multiple times in the program.
  • 3. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 3 Function Overview void show(); // declaration/prototype main() { ... show(); // function call ... } void show() // function definition { ... // function body ... }
  • 4. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 4 Functions in C++  C++ has added many new features to functions to make them more reliable and flexible.
  • 5. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 5 What is new in C++ Functions  Call by Reference using Reference Variable.  Return by Reference.  Inline Functions.  Default Arguments.  Const Arguments.  Function Overloading.  Friend & Virtual Functions.
  • 6. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 6 Call By Reference vs. Call By value  In traditional C, a function call passes arguments by its value. The called function creates new set of variables and copies the value of arguments into them.  The called function does not have access to the actual variables in the calling program and can only work on the copies of values.
  • 7. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 7 Example of Call By Value. int sum(int, int); main() { int a = 4, b = 3, c; c = sum(a, b); cout << “Add is : “ << c; } int sum(int x, int y) { int addition = x + y; return addition; } Add is : 7 Output
  • 8. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 8 Call By Reference vs. Call By value  This mechanism is fine if the function does not need to modify the values of the original variables in the calling program.  But there may arise some situations where we would like to change the values of variables in the calling program.
  • 9. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 9 Call By Reference.  Provisions of the reference variable in c++ permits us to pass parameters to the function by reference.  When we pass arguments by reference, the ‘formal’ arguments in the called function became aliases of ‘actual’ arguments in the calling program.  This means when the called function is working with its own arguments, it is actually working on the original variables of calling function.
  • 10. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 10 Call By Reference. void swap(int&, int&); main() { int a = 10, b = 20; swap(a, b); cout << “A : “ << a << endl << “B : “ << b; } // x and y are references void swap(int &x, int &y) { int temp = x; x = y; y = temp; } A : 20 B : 10 Output
  • 11. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 11 Call By Reference.  When the function call like swap(a, b); executes, then the following initialization occurs. int &x = a; int &y = b;  Any changes made to variable ‘x’ and ‘y’ in swap function will reflect to variables ‘a’ and ‘b’ of main, because ‘x’ and ‘y’ are simply aliases of ‘a’ and ‘b’
  • 12. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 12 Call By Reference using pointers. void swap(int*, int*); main() { int a = 10, b = 20; swap(&a, &b); cout << “A : “ << a << endl << “B : “ << b; } // x and y are now pointers void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } A : 20 B : 10 Output
  • 13. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 13 Return By Reference.  In C++, not only you can pass arguments by reference but also you can return a reference from a function.  When a function returns a reference, it returns an implicit pointer to its return value. This way, a function can be used on the left hand side of an assignment operator.
  • 14. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 14 Return By Reference. int n; // a global variable int& test(); // function prototype main() { test() = 5; cout << “N : “ << n; } int& test() { return n; } N : 5 Output
  • 15. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 15 Return By Reference : Explanation  In this example, the return type of function test() is int&. Hence this function returns a reference.  The return statement is return n; but unlike return by value. This statement doesn't return value of n, instead it returns variable n itself. int& test() { return n; }
  • 16. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 16 Return By Reference : Explanation  When the function is called from main the yield of the function call is variable n itself, so when the following statement in main executes it assigns 5 to variable n test() = 5;  In simple words, variable n is assigned at the left hand side of statement test() = 5;
  • 17. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 17 Points to keep in mind.  You cannot return a local variable (which is non-static) from a function which returns a reference.  Following piece of code will generate a compile error. int& test() { int n; // n is local variable return n; // compile error }
  • 18. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 18 Points to keep in mind.  But if I change int n; to static int n; then  Following piece of code will be successfully compiled and run perfectly. int& test() { static int n; // n is now a static // local variable return n; // perfectly fine }
  • 19. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 19 Points to keep in mind.  Ordinary function returns value but this function doesn't. Hence, you can't return constant from this function.  Following piece of code will generate a compile error. int& test() { return 2; // compile error }
  • 20. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 20 Return By Reference.  A function can also return a reference. int& max(int &x, int &y) { if(x>y) return x; else return y; }
  • 21. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 21 Return By Reference.  Since the return type of max() is int &, the function returns reference to x or y.  The function call such as max(a, b) will return a reference either to a or b depending on their values. That means the function call can appear on the left hand side of an assignment operator.  max(a, b) = 0;
  • 22. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 22 Inline Functions  Every time a function get called it takes a lot of extra time in executing a series of instructions for tasks, such as jumping to the function, saving registers, pushing arguments into the stack, and returning to the calling function.  solution to solve this problem is to use inline functions.
  • 23. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 23 Inline Functions  An Inline function is a function that is expanded in line when it is invoked.  The compiler replaces the function call with the corresponding function code.  Inline expansion makes program run faster because the overhead of a function call and return is eliminated.
  • 24. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 24 Inline Functions inline double cube(double n) { return (n*n*n); }
  • 25. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 25 Inline Functions  Remember that inline keyword sends a request, not a command, to the compiler.  The compiler may ignore this request if the function definition is too long or too complicated.  In such cases compiler will compile the function as a normal function.
  • 26. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 26 Inline Functions  Some of the situations where inline expansion may not work.  If function contains a loop, switch or goto statement.  If function contains static variables.  If function is a recursive function.
  • 27. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 27 Default arguments  C++ allows us to call a function without specifying all its arguments. In such cases, the function assigns a default value to the parameters.  Default values are specified when the function is declared.  The compiler looks at the prototype to see how many arguments a function uses and alerts the program for default values.
  • 28. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 28 Default arguments  Consider the following prototype. float interest(float amt, int period, float irate=7.2);  The default value is specified in a manner similar to a variable initialization.  Above prototype declares a default value of 7.2 to the argument irate.
  • 29. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 29 Default arguments  Consider the following prototype. float interest(float amt, int period, float irate=7.2);  A function call like x = interest(5000, 8); // one argument missing  Passes the value 5000 to amt, and 8 to period and let the function use default value of 7.2 for irate.
  • 30. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 30 Default arguments  Consider the following prototype. float interest(float amt, int period, float irate=7.2);  A function call like x = interest(5000, 8, 6.3); //no missing argument  Passes the value 5000 to amt, and 8 to period, and 6.3 for irate.  Passes an explicit value of 6.3 to irate
  • 31. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 31 Default arguments  One important point to note is that the only trailing arguments can have default values.  We must add defaults from right to left.  We cannot provide a default value to a particular argument in the middle of an argument list.
  • 32. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 32 Default arguments  Some Examples : int mul(int i, int j=5, int k=10); // legal int mul(int i, int j=5, int k); // illegal int mul(int i=2, int j, int k=2); // illegal int mul(int i=5, int j=2, int k=4); // legal
  • 33. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 33 Default arguments  Default arguments are useful in situations where some arguments always have the same value.  For example bank interest may retain same for all customers for a particular period of deposits.  It also provides a greater flexibility to the programmers by sending all arguments explicitly.
  • 34. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 34 const arguments  In C++, an argument to a function can be declared as const, int length(const char p[]);
  • 35. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 35 const arguments  The qualifier const tells the compiler that the function should not modify the argument.  The compiler will generate an error when this condition is violated. This type of declaration is significant only when we pass arguments by reference or pointers.
  • 36. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 36 Introduction to Class  Like structures in C, class is a user defined data type in C++.  A Class is extension of the idea of structures used in C.  It is a new way of creating and implementing a user defined data type.
  • 37. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 37 Structures Revised  Structures in C provides a method of packing different type of data together.  A structure is a convenient tool for handling a group of logically related data items.  Once the structure type has been defined, we can create any number of variables of that type.
  • 38. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 38 Structures Revised  Consider following structure. struct student { int rollno; char name[25]; int marks; };
  • 39. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 39 Structures Revised  Consider following Declaration. struct student a;  ‘a’ is a variable of type ‘student’ and contains 3 member variables, which can be accessed by using ‘.’ operator. a.rollno = 15; strcpy(a.name, “Aditya”); a.marks = 435;
  • 40. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 40 Limitations of C Structures  C structures do not provide the concept of data hiding.  Structure members can be directly accessed by the structure variables by any function in their scope.
  • 41. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 41 Extensions to Structures in C++  C++ supports all features of structures as defined in C, in addition C++ has expanded its capabilities to suit its OOP philosophy.  It attempts to bring the user-defined types as close as possible to built in data types, and also provides a facility to hide the data which is the major principle of OOP.
  • 42. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 42 Extensions to Structures in C++  In C++ a structure can contain variables and functions both as its member.  It can also declare some of its members as ‘private’ so that they cannot be accessed directly by the external functions.
  • 43. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 43 Extensions to Structures in C++  In C++, the structure names are stand alone and can be used like any other type names.  In other words the keyword ‘struct’ can be omitted from the declaration of the structure variables.  For example. student a; // in c++ struct student a; // in c
  • 44. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 44 Introduction to Class  C++ incorporates all of these extensions in another user defined data type ‘class’.  There is a very little syntactical difference between structure and classes in c++.  The only difference is that by default the members of a class are private, while, by default, the members of a structure are public.
  • 45. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 45 Specifying a Class  A class is a way of binding data and its associated functions together, it allows the data (and functions) to be hidden from external world, if necessary.  While defining a class we are creating a new abstract data type, that can be treated like any other built in data type.
  • 46. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 46 Specifying a Class  Generally a class specifications has two parts.  Class declaration.  Class function definitions.  The class declaration describes the type and the scope of its members.  The class function definitions describes how the class functions are implemented.
  • 47. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 47 General form of a Class class class_name { private: // optional variable declarations; function declarations; public: variable declarations; function declarations; };
  • 48. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 48 General form of a Class  The functions and variables declared in a class are called as class members.  The class members declared as private can be accessed only within the class, and the members declared as public can be accessed from outside of the class also.  The variables declared inside a class are called as data members, and the functions are called as member functions.
  • 49. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 49 Public Area Data Functions General form of a Class Class Entry not allowed for outside world X Entry allowed for outside world Private Area Data Functions