SlideShare a Scribd company logo
1 of 25
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
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
Prepared By: Asst. Prof. Sejal Jadav
•Function saves memory
•Function is time consuming
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.
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.
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.
Prepared By: Asst. Prof. Sejal Jadav
Syntax:
inline<function header>
{
Function body
}
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
#include<iostream>
using namespace std;
inline void address();
main()
{
address();
}
void address()
{
cout<<endl<<"CCSIT";
cout<<endl<<"Junagadh";
}
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)
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.
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
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:
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.
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);
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);
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);
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);
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.
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
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.”
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.
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.
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.
Prepared By: Asst. Prof. Sejal Jadav
• Let’s look at the example: Overloading_fun.cpp

More Related Content

What's hot

2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and OverridingMichael Heron
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introductionsandeep54552
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts246paa
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++sandeep54552
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++gourav kottawar
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetHDR1001
 
C++ unit-1-part-12
C++ unit-1-part-12C++ unit-1-part-12
C++ unit-1-part-12Jadavsejal
 
OOPS features using Objective C
OOPS features using Objective COOPS features using Objective C
OOPS features using Objective CTiyasi Acharya
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 
#6- Arrays and Collections Framework
#6- Arrays and Collections Framework#6- Arrays and Collections Framework
#6- Arrays and Collections FrameworkGhadeer AlHasan
 
Practice Session
Practice Session Practice Session
Practice Session Hitesh-Java
 
Concepts of OOPs
Concepts of OOPsConcepts of OOPs
Concepts of OOPsEssay Corp
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++Learn By Watch
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2Abbas Ajmal
 

What's hot (20)

2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat Sheet
 
C++ unit-1-part-12
C++ unit-1-part-12C++ unit-1-part-12
C++ unit-1-part-12
 
OOPS features using Objective C
OOPS features using Objective COOPS features using Objective C
OOPS features using Objective C
 
Basics of oops concept
Basics of oops conceptBasics of oops concept
Basics of oops concept
 
Lecture02
Lecture02Lecture02
Lecture02
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
#6- Arrays and Collections Framework
#6- Arrays and Collections Framework#6- Arrays and Collections Framework
#6- Arrays and Collections Framework
 
Practice Session
Practice Session Practice Session
Practice Session
 
Concepts of OOPs
Concepts of OOPsConcepts of OOPs
Concepts of OOPs
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
 

Similar to C++ unit-1-part-13 (20)

C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
JS Responsibilities
JS ResponsibilitiesJS Responsibilities
JS Responsibilities
 
Function in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programmingFunction in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programming
 
Thinking in Functions
Thinking in FunctionsThinking in Functions
Thinking in Functions
 
Java 8 - functional features
Java 8 - functional featuresJava 8 - functional features
Java 8 - functional features
 
EST 102 Programming in C-MODULE 4
EST 102 Programming in C-MODULE 4EST 102 Programming in C-MODULE 4
EST 102 Programming in C-MODULE 4
 
Unit - 2.ppt
Unit - 2.pptUnit - 2.ppt
Unit - 2.ppt
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
 
Replace OutputIterator and Extend Range
Replace OutputIterator and Extend RangeReplace OutputIterator and Extend Range
Replace OutputIterator and Extend Range
 
10.ppt
10.ppt10.ppt
10.ppt
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
 
B.sc CSIT 2nd semester C++ Unit4
B.sc CSIT  2nd semester C++ Unit4B.sc CSIT  2nd semester C++ Unit4
B.sc CSIT 2nd semester C++ Unit4
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
Lecture05
Lecture05Lecture05
Lecture05
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 

More from Jadavsejal

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassJadavsejal
 
Programming Java Concept of Event Handling
Programming Java Concept of Event HandlingProgramming Java Concept of Event Handling
Programming Java Concept of Event HandlingJadavsejal
 
Programming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingProgramming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingJadavsejal
 
concept of applet and concept of Layout Managers
concept of applet and concept of Layout Managersconcept of applet and concept of Layout Managers
concept of applet and concept of Layout ManagersJadavsejal
 
C++ unit-1-part-15
C++ unit-1-part-15C++ unit-1-part-15
C++ unit-1-part-15Jadavsejal
 
C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14Jadavsejal
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8Jadavsejal
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7Jadavsejal
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5Jadavsejal
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4Jadavsejal
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2Jadavsejal
 
C++ unit-1-part-3
C++ unit-1-part-3C++ unit-1-part-3
C++ unit-1-part-3Jadavsejal
 
C++-Unit-1-Part-1
C++-Unit-1-Part-1C++-Unit-1-Part-1
C++-Unit-1-Part-1Jadavsejal
 
05_system architecture
05_system architecture05_system architecture
05_system architectureJadavsejal
 
04 data flow architecture
04 data flow architecture 04 data flow architecture
04 data flow architecture Jadavsejal
 
04 authentication
04 authentication04 authentication
04 authenticationJadavsejal
 

More from Jadavsejal (20)

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone Class
 
Programming Java Concept of Event Handling
Programming Java Concept of Event HandlingProgramming Java Concept of Event Handling
Programming Java Concept of Event Handling
 
Programming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingProgramming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event Handling
 
concept of applet and concept of Layout Managers
concept of applet and concept of Layout Managersconcept of applet and concept of Layout Managers
concept of applet and concept of Layout Managers
 
C++ unit-1-part-15
C++ unit-1-part-15C++ unit-1-part-15
C++ unit-1-part-15
 
C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2
 
C++ unit-1-part-3
C++ unit-1-part-3C++ unit-1-part-3
C++ unit-1-part-3
 
C++-Unit-1-Part-1
C++-Unit-1-Part-1C++-Unit-1-Part-1
C++-Unit-1-Part-1
 
05_system architecture
05_system architecture05_system architecture
05_system architecture
 
04 data flow architecture
04 data flow architecture 04 data flow architecture
04 data flow architecture
 
Vpn protocols
Vpn protocolsVpn protocols
Vpn protocols
 
04 authentication
04 authentication04 authentication
04 authentication
 
03 cia
03 cia03 cia
03 cia
 
01
01 01
01
 
4 ipv6
4 ipv64 ipv6
4 ipv6
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

C++ unit-1-part-13

  • 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 }
  • 8. Prepared By: Asst. Prof. Sejal Jadav
  • 9. Prepared By: Asst. Prof. Sejal Jadav #include<iostream> using namespace std; inline void address(); main() { address(); } void address() { cout<<endl<<"CCSIT"; cout<<endl<<"Junagadh"; }
  • 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