SlideShare a Scribd company logo
Object Oriented Programming Using C++
Prepared by
Mr. Nagesh Pratap Singh
JMS GROUP OF INSTITUTIONS, HAPUR
DEPARTMENT OF COMPUTER APPLICATION
C++ FUNCTIONS
A function is a block of code which only runs when it is
called.
You can pass data, known as parameters, into a
function.
Functions are used to perform certain actions, and they
are important for reusing code: Define the code once, and
use it many times
• The main( )Functon ;
ANSI does not specify any return type for the main ( ) function which is the starting point for the execution of a
program .
The definition of main( ) is :-
main()
{
//main program statements
}
This is property valid because the main () in ANSI C does not return any value. In C++, the main () returns a
value of type int to the operating system.
The functions that have a return value should use the return statement for terminating.
The main () function in C++ is therefore defined as follows.
int main( )
{
--------------
--------------
return(0)
}
Since the return type of functions is int by default, the key word int in the main( ) header is optional
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 inline when it is invoked .That is the compiler replaces the function
call with the corresponding function code.
The inline functions are defined as follows:-
inline function-header
{
function body;
}
Example:
inline double cube (double a)
{
return(a*a*a);
}
The above inline function can be invoked by statements like c=cube(3.0);
d=cube(2.5+1.5);
remember that the inline keyword merely sends a request, not a command to the compliler. The compiler may
ignore this request if the function definition is too long or too complicated and compile the function as a normal
function
SOME OF THE SITUATIONS WHERE
INLINE EXPANSION MAY NOT WORK ARE:
1. For functions returning values if a loop, a switch or a go
to exists.
2. for function s not returning values, if a return statement
exists.
3. if functions contain static variables.
4. if inline functions are recursive,.
Object Oriented Programming using C++ Unit 1
DEFAULT ARGUMENTS IN C++
• A default argument is a value provided in a function
declaration that is automatically assigned by the compiler
if the calling function doesn’t provide a value for the
argument. In case any value is passed, the default value
is overridden.
• 1) The following is a simple C++ example to demonstrate
the use of default arguments. Here, we don’t have to write
3 sum functions; only one function works by using the
default values for 3rd and 4th arguments.
DEFAULT ARGUMENTS IN C++
CPP PROGRAM TO DEMONSTRATE DEFAULT
ARGUMENTS
#include <iostream>
using namespace std;
int sum(int x, int y, int z = 0, int w = 0)
{
return (x + y + z + w);
}
int main()
{
cout << sum(10, 15) << endl;
cout << sum(10, 15, 25) << endl;
cout << sum(10, 15, 25, 30) << endl;
return 0;
}
FUNCTION OVERLOADING:
Function overloading is a feature of object-oriented
programming where two or more functions can have the
same name but different parameters. When a function
name is overloaded with different jobs it is called Function
Overloading. In Function Overloading “Function” name
should be the same and the arguments should be
different. Function overloading can be considered as an
example of a polymorphism feature in C++.
If multiple functions having same name but parameters of
the functions should be different is known as Function
Overloading.
Object Oriented Programming using C++ Unit 1
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(double a, double b)
{
cout << endl << "sum = " << (a + b);
}
int main()
{
add(10, 2);
add(5.3, 6.2);
return 0;
}
Object Oriented Programming using C++ Unit 1
WHY USE CLASSES INSTEAD OF STRUCTURES
• Classes and structures are somewhat the same but
still, they have some differences. For example, we
cannot hide data in structures which means that
everything is public and can be accessed easily which
is a major drawback of the structure because structures
cannot be used where data security is a major concern.
Another drawback of structures is that we cannot add
functions in it.
CLASSES IN C++
• Class is a group of objects that share common
properties and relationships .In C++, a class is a
new data type that contains member variables
and member functions that operates on the
variables. A class is defined with the keyword
class. It allows the data to be hidden, if
necessary from external use.
CLASS DECLARATION
Generally a class specification has two parts:-
a) Class declaration
b) Class function definition
Syntax:-
class class-name
{
private:
variable declarations;
function declaration ;
public:
variable declarations;
function declaration;
};
CREATING OBJECTS:
Once a class has been declared we can create variables of that type by using the
class name.
Example: item x;
creates a variables x of type item.
In C++, the class variables are known as objects. Therefore x is called an object of
type item.
item x, y ,z also possible.
class item
{ -----------
-----------
----------- }x ,y ,z;
would create the objects x ,y ,z of type item.
ACCESSING CLASS MEMBER:
The private data of a class can be accessed only through the member functions of that class.
The main() cannot contains statements that the access number and cost directly.
Syntax: object name.function-name(actual arguments);
Example:- x. getdata(100,75.5);
It assigns value 100 to number, and 75.5 to cost of the object x by implementing the getdata() function .
Example: class xyz {
Int x;
Int y;
public:
int z;
};
xyz p;
p. x =0; error , x is private
p. z=10; ok ,z is public
DEFINING MEMBER FUNCTION:
Member can be defined in two places
• Outside the class definition
• Inside the class function
OUTSIDE THE CLASS DEFlNATION :-
Member function that are declared inside
a class have to be defined separately
outside the class.Their definition are very
much like the normal functions.
Syntax:
return type class-name::function-name(argument
declaration )
{
function-body
}
Example:
void item :: getdata (int a , float b )
{
number=a; cost=b;
}
void item :: putdata ( void)
{
cout<<”number=:”<<number<<endl;
cout<<”cost=”<<cost<<endl;
}
INSIDE THE CLASS DEFINATION:
Another method of defining a member function is to replace the function declaration by the
actual function definition inside the class .
Example:
class item
{
public:
Intnumber; float cost;
void getdata (int a ,float b);
void putdata(void)
{
cout<<number<<endl; cout<<cost<<endl;
}
};
A C++ PROGRAM WITH CLASS:
# include< iostream. h>
class item
{
public:
int number; float cost;
void getdata ( int a , float b);
void putdala ( void)
{
cout<<“number:”<<number<<endl;
cout<<”cost :”<<cost<<endl;
}
};
void item :: getdata (int a , float b)
{
number=a; cost=b;
}
main ( )
{
item x;
cout<<”nobjectx”<<endl;
x. getdata(100,299.95);
x .putdata();
item y;
cout<<”n object y”<<endl;
y. getdata(200,175.5);
y. putdata();
}
Ad

More Related Content

Similar to Object Oriented Programming using C++ Unit 1 (20)

C++ tutorial assignment - 23MTS5730.pptx
C++ tutorial assignment  - 23MTS5730.pptxC++ tutorial assignment  - 23MTS5730.pptx
C++ tutorial assignment - 23MTS5730.pptx
sp1312004
 
Chapter 6 - Modular Programming- in C++.pptx
Chapter 6 - Modular Programming- in C++.pptxChapter 6 - Modular Programming- in C++.pptx
Chapter 6 - Modular Programming- in C++.pptx
ChereLemma2
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
nikshaikh786
 
Functions1
Functions1Functions1
Functions1
DrUjwala1
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
Divyanshu Dubey
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
Venkateswarlu Vuggam
 
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
Venkateswarlu Vuggam
 
Chapter 5 - Modular Programming.pdf
Chapter 5 - Modular Programming.pdfChapter 5 - Modular Programming.pdf
Chapter 5 - Modular Programming.pdf
KirubelWondwoson1
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxconcepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptx
urvashipundir04
 
Function
Function Function
Function
Kathmandu University
 
functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptxfunctIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
nandemprasanna
 
UNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxUNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptx
urvashipundir04
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Srikanth Mylapalli
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
Function
FunctionFunction
Function
Rajat Patel
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
Object Oriented Programming (OOP) using C++ - Lecture 1
Object Oriented Programming (OOP) using C++ - Lecture 1Object Oriented Programming (OOP) using C++ - Lecture 1
Object Oriented Programming (OOP) using C++ - Lecture 1
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Function Overloading Call by value and call by reference
Function Overloading Call by value and call by referenceFunction Overloading Call by value and call by reference
Function Overloading Call by value and call by reference
TusharAneyrao1
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
C++ tutorial assignment - 23MTS5730.pptx
C++ tutorial assignment  - 23MTS5730.pptxC++ tutorial assignment  - 23MTS5730.pptx
C++ tutorial assignment - 23MTS5730.pptx
sp1312004
 
Chapter 6 - Modular Programming- in C++.pptx
Chapter 6 - Modular Programming- in C++.pptxChapter 6 - Modular Programming- in C++.pptx
Chapter 6 - Modular Programming- in C++.pptx
ChereLemma2
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
nikshaikh786
 
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
Chapter 5 - Modular Programming.pdf
Chapter 5 - Modular Programming.pdfChapter 5 - Modular Programming.pdf
Chapter 5 - Modular Programming.pdf
KirubelWondwoson1
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxconcepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptx
urvashipundir04
 
functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptxfunctIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
nandemprasanna
 
UNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxUNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptx
urvashipundir04
 
Function Overloading Call by value and call by reference
Function Overloading Call by value and call by referenceFunction Overloading Call by value and call by reference
Function Overloading Call by value and call by reference
TusharAneyrao1
 

Recently uploaded (20)

SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdfAre Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Telecoms Supermarket
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Top 10 IT Help Desk Outsourcing Services
Top 10 IT Help Desk Outsourcing ServicesTop 10 IT Help Desk Outsourcing Services
Top 10 IT Help Desk Outsourcing Services
Infrassist Technologies Pvt. Ltd.
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Mastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdfMastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdf
Spiral Mantra
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Web and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in RajpuraWeb and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in Rajpura
Erginous Technology
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdfAre Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Are Cloud PBX Providers in India Reliable for Small Businesses (1).pdf
Telecoms Supermarket
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Mastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdfMastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdf
Spiral Mantra
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Web and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in RajpuraWeb and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in Rajpura
Erginous Technology
 
Ad

Object Oriented Programming using C++ Unit 1

  • 1. Object Oriented Programming Using C++ Prepared by Mr. Nagesh Pratap Singh JMS GROUP OF INSTITUTIONS, HAPUR DEPARTMENT OF COMPUTER APPLICATION
  • 2. C++ FUNCTIONS A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times
  • 3. • The main( )Functon ; ANSI does not specify any return type for the main ( ) function which is the starting point for the execution of a program . The definition of main( ) is :- main() { //main program statements } This is property valid because the main () in ANSI C does not return any value. In C++, the main () returns a value of type int to the operating system. The functions that have a return value should use the return statement for terminating. The main () function in C++ is therefore defined as follows. int main( ) { -------------- -------------- return(0) } Since the return type of functions is int by default, the key word int in the main( ) header is optional
  • 4. 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 inline when it is invoked .That is the compiler replaces the function call with the corresponding function code. The inline functions are defined as follows:- inline function-header { function body; } Example: inline double cube (double a) { return(a*a*a); } The above inline function can be invoked by statements like c=cube(3.0); d=cube(2.5+1.5); remember that the inline keyword merely sends a request, not a command to the compliler. The compiler may ignore this request if the function definition is too long or too complicated and compile the function as a normal function
  • 5. SOME OF THE SITUATIONS WHERE INLINE EXPANSION MAY NOT WORK ARE: 1. For functions returning values if a loop, a switch or a go to exists. 2. for function s not returning values, if a return statement exists. 3. if functions contain static variables. 4. if inline functions are recursive,.
  • 7. DEFAULT ARGUMENTS IN C++ • A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn’t provide a value for the argument. In case any value is passed, the default value is overridden. • 1) The following is a simple C++ example to demonstrate the use of default arguments. Here, we don’t have to write 3 sum functions; only one function works by using the default values for 3rd and 4th arguments.
  • 9. CPP PROGRAM TO DEMONSTRATE DEFAULT ARGUMENTS #include <iostream> using namespace std; int sum(int x, int y, int z = 0, int w = 0) { return (x + y + z + w); } int main() { cout << sum(10, 15) << endl; cout << sum(10, 15, 25) << endl; cout << sum(10, 15, 25, 30) << endl; return 0; }
  • 10. FUNCTION OVERLOADING: Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading can be considered as an example of a polymorphism feature in C++. If multiple functions having same name but parameters of the functions should be different is known as Function Overloading.
  • 12. #include <iostream> using namespace std; void add(int a, int b) { cout << "sum = " << (a + b); } void add(double a, double b) { cout << endl << "sum = " << (a + b); } int main() { add(10, 2); add(5.3, 6.2); return 0; }
  • 14. WHY USE CLASSES INSTEAD OF STRUCTURES • Classes and structures are somewhat the same but still, they have some differences. For example, we cannot hide data in structures which means that everything is public and can be accessed easily which is a major drawback of the structure because structures cannot be used where data security is a major concern. Another drawback of structures is that we cannot add functions in it.
  • 15. CLASSES IN C++ • Class is a group of objects that share common properties and relationships .In C++, a class is a new data type that contains member variables and member functions that operates on the variables. A class is defined with the keyword class. It allows the data to be hidden, if necessary from external use.
  • 16. CLASS DECLARATION Generally a class specification has two parts:- a) Class declaration b) Class function definition Syntax:- class class-name { private: variable declarations; function declaration ; public: variable declarations; function declaration; };
  • 17. CREATING OBJECTS: Once a class has been declared we can create variables of that type by using the class name. Example: item x; creates a variables x of type item. In C++, the class variables are known as objects. Therefore x is called an object of type item. item x, y ,z also possible. class item { ----------- ----------- ----------- }x ,y ,z; would create the objects x ,y ,z of type item.
  • 18. ACCESSING CLASS MEMBER: The private data of a class can be accessed only through the member functions of that class. The main() cannot contains statements that the access number and cost directly. Syntax: object name.function-name(actual arguments); Example:- x. getdata(100,75.5); It assigns value 100 to number, and 75.5 to cost of the object x by implementing the getdata() function . Example: class xyz { Int x; Int y; public: int z; }; xyz p; p. x =0; error , x is private p. z=10; ok ,z is public
  • 19. DEFINING MEMBER FUNCTION: Member can be defined in two places • Outside the class definition • Inside the class function OUTSIDE THE CLASS DEFlNATION :- Member function that are declared inside a class have to be defined separately outside the class.Their definition are very much like the normal functions. Syntax: return type class-name::function-name(argument declaration ) { function-body } Example: void item :: getdata (int a , float b ) { number=a; cost=b; } void item :: putdata ( void) { cout<<”number=:”<<number<<endl; cout<<”cost=”<<cost<<endl; }
  • 20. INSIDE THE CLASS DEFINATION: Another method of defining a member function is to replace the function declaration by the actual function definition inside the class . Example: class item { public: Intnumber; float cost; void getdata (int a ,float b); void putdata(void) { cout<<number<<endl; cout<<cost<<endl; } };
  • 21. A C++ PROGRAM WITH CLASS: # include< iostream. h> class item { public: int number; float cost; void getdata ( int a , float b); void putdala ( void) { cout<<“number:”<<number<<endl; cout<<”cost :”<<cost<<endl; } }; void item :: getdata (int a , float b) { number=a; cost=b; } main ( ) { item x; cout<<”nobjectx”<<endl; x. getdata(100,299.95); x .putdata(); item y; cout<<”n object y”<<endl; y. getdata(200,175.5); y. putdata(); }