Oops 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Object Oriented Programming

Using C++

Subject Teacher:- MANDEEP SINGH

OOPS Using
OOPS Using C++
C++ 1
1
Functions in C++
• A function declaration.
• All functions must be declared (prototyped)
(anywhere) before its use. e.g.
return_type function name (argument-list);
• Argument list has structure as
(data type1, datatype2, data type3)
• Names for arguments are not required while
declaring it (they don’t have to be matched with
names used for definition or call)

OOPS Using
OOPS Using C++
C++ 2
2
• All library functions used in a program has
prototypes and definitions in header files
included.
• Compiler uses prototype to check number
and types of arguments used, while calling
that function.

OOPS Using C++ 3


3
Function Definition
• The general form of a function definition in C++ is as
follows:
return-type function-name ( parameter-list )
{
local-definitions or declarations;
function-implementation;
}
• If the function returns a value then the type of that
value must be specified in return-type. For the
moment this could be int, float or char. If the function
does not return a value then the function-type must
be void.

OOPS Using C++ 4


4
• Prototype is not required if function is defined before
its use.
• The function-name follows the same rules of
composition as identifiers.
• The parameter-list lists the formal parameters of the
function together with their types.
• The local-definitions are definitions of variables that
are used in the function-implementation. These
variables have no meaning outside the function.
• The function-implementation consists of C++
executable statements that implement the effect of
the function.

OOPS Using C++ 5


5
Passing arguments in functions
1. Call by value (copy of actual parameter is
acted upon)
2. Call by address (address of actual
parameter is acted upon)
Void swap1(int a, int b)
{
int temp=a;
a=b;
b=temp;
}

OOPS Using C++ 6


6
• void swap2(int* a, int* b) • void swap3(int &a, int b)
{ {
int temp; int temp;
temp=*a; temp=a;
*a=*b; a=b;
*b=temp; b=temp;
} }

OOPS Using C++ 7


7
• int main()
{
int m=2;n=1;
swap1(m,n); //Call by Value
cout<<m<<n;
swap2(&m,&n); //Call by Address of variables
cout<<m<<n;
swap3(m,n); //Call by Reference Variables (Alias)
cout<<m<<n;
}

OOPS Using C++ 8


8
What is an inline function?
• Functions defined with “inline” directive
• At each call to such function , its function
body is inserted at that point in compiled
code.
• If function body is small and called at
several places then it should be made inline
as there is no overhead in CALL and
RETURN statements .
• Functions defined inside a class are by
default INLINE.

OOPS Using C++ 9


using inline function….
#include<iostream.h>
inline double prod (double a, double b)
{ return (a*b); }
inline double div (double a, double b)
{ return (a/b);}
int main()
{
double a=12.654,b=3.45;
cout<<prod(a,b)<<endl;
cout<<div(a,b)<<endl;
return 0;
}

OOPS Using C++ 10


10
• Inline keyword sends only a request (not a command)
to the compiler, the compiler may ignore the request
and treat the function as normal one if function
definition is too long or complicated.
• Situations where inline expansion may not work:
1 For functions returning values, if a loop, a switch, or a
goto exist.
2 For functions not returning values, if a return
statement exists.
3 If functions contain static variables.
4 If inline functions are recursive.

OOPS Using C++ 11


11
Default arguments
• The argument whose default value is mentioned at
function declaration can be skipped from actual
parameter list at the time of function call.
e.g. return-type function-name(type1,type2 n=6)
• If some value is passed to default argument then
default value is replaced by that value.
• Are useful where one or more parameter remain
same for each call.

OOPS Using C++ 12


12
#include<iostream.h>
void display (int , char name[12]="default");
int main (void)
{ char name[12];
int no;
cout <<"enter name and no";
cin >>name>>no;
display(no,name);
display(no); }
void display (int n,char name[12])
{ cout<<"name is“<<name;
cout<<endl<<"no is"<<n<<endl;
}

OOPS Using C++ 13


13
• Output of the program will be
enter name and no rahul 23
name is rahul
no is23

name is default
no is23

OOPS Using C++ 14


14
Const Arguments
• An argument to a function can be declared as const as
shown below:
int strlen ( const char *p);
int length ( const string &s);
• 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

OOPS Using C++ 15


Function Overloading
• A number of functions having same name, but
performing different tasks.
• Every function differs from others in argument
list.
• Argument lists may differ in either type of
arguments or number of arguments.
• At function call ,function definition with
corresponding arguments is invoked.

OOPS Using C++ 16


Example:
• // Declaration
//int add(int a, int b); // 1
int add(int a, int b, int c); //2
double add( double x, double y); //3
double add (int p , double q); //4
double add (double p, int q ); //5
• //Function calls
cout<< add(5,10); // uses 1
cout<< add(15,10.0); // uses 4
cout<< add(12.5,7.5); // uses 3
cout<< add(5,10,15); // uses 2
cout<< add(9.75,5); // uses 5

OOPS Using C++ 17


The Function Selection involves the
following steps:
1. The Compiler first tries to find an exact match
in which the types of actual arguments are
the same, and use that function
2. If compiler is unable to find an exact match
then it tries to use integral promotions to the
actual arguments and again match new
argument list with function definitions. e.g.
char --🡪int
float--🡪double
OOPS Using C++ 18
The Function Selection involves the following
steps: (Contd…)
3. When either of them fails, the compiler tries
to use the built-in conversions (the implicit
conversions) to the actual arguments and
then uses the function whose match is
unique. If the conversion is possible to have
multiple matches, then the compiler will
generate an error message. e.g.
long square (long n);
double square (double x);
Function call : square(10); // so Error

OOPS Using C++ 19


The Function Selection involves the following
steps: (Contd…)
4. If all of the steps fail, then the compiler will
try the user-defined conversions in
combination with integral promotions and
built-in conversions to find a unique match.
User-defined conversions are often used in
handling class objects.

OOPS Using C++ 20


#include<iostream.h>
#include<conio.h>
Float pie=3.14;
int volume (int s) //function defination
{
return (s*s*s);
}
double volume (double l, double b, double h)
{
return (l*b*h);
}
double volume (double r, double h)
{
return (3.14*r*r*h);
}
int main(void)
{
clrscr();
cout<<endl<<"volume of a cube is”<<volume(5);
cout<<endl<<"volume of a rectangular box is” << volume(2.6, 12, 21.25);
cout<<endl<<"volume of a cylinder "<<volume(2, 2.8);
return 0;
}
OOPS Using C++ 21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy