CPP Lecture03
CPP Lecture03
(type-name) expression // C notation type-name( expression) // C++ notation Ex. : average = sum/(float)i; // C notation average = sum/float(i); // C++ notation
A type-name behaves as if it is a function for converting
p = int *(q); // illegal In such case, use C notation p = (int *) q; OR, Use typedef, typedef int * int_pt; p = int_pt(q);
Integral Expn.
Float Expn.
Bitwise
Expn.
m x+y &m m*n-5 x+y/10 ptr m*x 5+(float)10 ptr+1 5+int(2.0) 10.75
x<=y a>b&&x=10 x<<3 a+b == c+d x==10||y>0 y>>1 m+n > 100
identical to
y = 50; x = y + 10;
Implicit conversion
m = 5 + 2.75;
For binary operator, if the operands type differ, the
compiler converts one of them to match with the other, using the rule that the Smaller type is converted to the Wider type. Whenever a char or short int appears in an expressions, it is converted to an int. This is called integral widening conversion. The implicit conversion is applied only after completing all integral widening conversion.
Control Structures
Control Structure
Selection
Sequence
Loop
If-else
Two way branch
switch
Multiple Branch
do-while
Exit-control
While, for
Entry-control
Control Structure
Entry
True
Entry
False
Conditi on
Entry
Loop
Action 1
Action 2
Action 1
Action 2
Cond ition
True
Action 1
(a) Sequence
(b) Selection
(C) loop
Control Structure
Form 1 If (expression is true) { action 1; } action 2; action 3; switch (expression) { case <value1>: { action 1; } case <value2>: { action 2; } case <value3>: { action 3; } default : { action 4; } } action-5;
Form 2
If (expression is true) { action 1; } Else { action 2; } action 3;
Control Structure
do { action 1; } while(condition is true); action 2; while(condition is true) { action 1; } action 2; for(initial value; test; inc/dec) { action 1; } action 2;
Function in C++
Function: Introduction
Group of statements that perform task Advantage :
To reduce the size of a program by calling and using them at different places in the program.
void show(); main() { . show(); . } void show() { } /* Function declaration*/
/* Function call*/
/* Function definition*/
/* Function body*/
any value main() { //main prog. Stmt. } OR void main() { //main prog. Stmt. }
C++ main() returns a value of type int to the operating system int main(); int main(int argc, char *argv[]);
zero ---- program ran successfully nonzero ---- there was a problem
Function Prototyping
Prototype describes the function interface to the compiler by giving details such as the number and type of arguments and the type of return values. Function prototype is a declaration statement in the calling program. Function prototype is of the following form:
type function-name(argument-list) ;
Where, argument-list contains types and names of arguments that must be passed to the function Ex., float volume(int x, float y, float z); float volume(int x, float y, z); // illegal
In function declaration, name of arguments are dummy variables and therefore they are optional. That is, the form
// valid
Compiler checks for type of arguments when function is called. Therefore, if names are used , they dont have to match the names used in the function call or function definition.
Function Prototyping
In Function definition, names are required because the arguments
must be referenced inside the function. Ex., float cube(float, float, float) int main() { float b,w,h,cube; cin>>b>>w>>h; cube=volume(b,w,h); // Function call & b,w,h = Actual argu. } float volume(float a, float b, float c) // a,b,c Formal Argument { float v=a*b*c; .. /* Function Definition*/ return v; }
Function Prototyping
We can also declare a function with an empty argument
Call by Reference
Function Call by value
In call by value method, the called function creates a new set of variables and copies the values of arguments
#include<iostream.h> #include<stdio.h> void swap(int, int); void main(){ int i=100, j=200; cout<<\n Before calling i=<<i; cout<<\n Before calling j=<<j; swap(i,j); cout<<\n After calling i=<<i; cout<<\n After calling j=<<j; } void swap(int m, int n){ int temp=m; m=n; n=temp; cout<<\n inside function m=<<m; cout<<\n inside function n=<<n; }
When arguments are passed by reference the formal arguments in the called function becomes aliases to the actual arguments in the calling function. When the function is working with its own arguments, it is actually working on the original data. #include<iostream.h> #include<iostream.h> void swap(int, int); void main(){ int i=100, j=200; cout<<\n Before calling i=<<i; cout<<\n Before calling j=<<j; swap(i,j); cout<<\n After calling i=<<i; cout<<\n After calling j=<<j; } void swap(int &m, int &n){ int temp=m; m=n; n=temp; }
Call by Reference
In C, Call by Reference is done using pointer.
void swap(int *m, int *n) { int temp; temp=*m; *m=*n; *n=temp; }
Function call: main() { int x=10,y=20; // print value of x and y before calling function swap(&x, &y); // print value of x and y after calling function }
Return by Reference
A function can also return a reference.
Ex.,
int &max(int &x, int &y) { if (x > y) return x; else return y; } Here, function call can appear on the left-hand side of an assignment statement. i.e., max(a,b) = -1; // assigns -1 to a if it is larger, otherwise // -1 to b
Inline Functions
Objective : to save memory space Every time, when function is called,
-- jumping to function -- saving registers -- pushing arguments into the stack, and -- returning to the calling function If function is small, a substantial percentage of execution time may be spent. One Solution : Use Macro With Macro, Drawback : usual error checking does not occur during compilation because, Macros are not really function
Inline Functions
An Inline Function is a function that is expanded in line
when it is invoked. That is, Compiler replaces the function call with the corresponding function code. inline double volume(double a, double b, Inline functions are defined double c) { as : return (a*b*c);
inline function-header {
function body }
Ex.
Inline Functions
Situations where inline expansion may not work are : For functions returning values, if a loop, a switch, or a goto exists. For functions not returning values, if a return statement exists. If functions contain static variables. If inline functions are recursive.
Default Arguments
C++ allows to call a function without specifying all its
arguments. A default argument is a value given in the function declaration that the compiler automatically inserts if the caller does not provide a value for that argument in the function call. Default values are specified when the function is declared. Syntax :
Default Arguments
Ex. float amount(float principal, int period, float rate=0.15);
Function call,
Default Arguments
Only the trailing arguments can have default values
Default Arguments
#include <iostream.h> float value(float p, int n, float r=0.15); // Prototype int main() { float amount; amount = value(5000.00,5); cout<<"\n Final value = "<< amount <<"\n\n"; return 0; } float value(float p, int n, float r) { int year=1; float sum=p; while(year <= n) { sum = sum * (1+r); year = year + 1; } return sum; }
Default Arguments
Useful in situations where some arguments always have
same value. Advantages : We can use default arguments to add new parameters to the existing functions. Default arguments can be used to combine similar functions into one.
Function Overloading
Overloading refers to use of the same thing for different
purpose. That means, Use same function name to create functions that perform a variety of different task is known as function overloading.
Same function name but with different argument list. A function call first matches the prototype having the same
number and type of arguments and then calls the appropriate function for execution.
Function Overloading
Ex. : // Declaration int add(int a, int b); // Prototype 1 int add(int a, int b, int c); // Prototype 2 double add(double x, double y); // Prototype 3 double add(int p, double q); // Prototype 4 double add(double p, int q); // Prototype 5
Function Overloading
A function call first matches the prototype having the same number and type 1. 2.
3.
4.
of arguments and then calls the appropriate function for execution. A best match must be unique. The function selection involves the following steps : Find an exact match in which types of actual arguments are the same, and use that function. If an exact match is not found, the compiler uses the integral promotion to the actual argument, such as char to int float to double When either of them fails, compile uses built-in conversion for unique match. If conversion is possible to have multiple matches, compiler will generate an error message. long square(long n) double square(double x) Function call, square(10) If all of the steps fail, then compiler will try the user-defined conversion + integral promotion + built-in conversion
Function Overloading
// Declaration int add(int a, int b); // Prototype 1 int add(int a, int b, int c); // Prototype 2 double add(double x, double y); // Prototype 3 double add(int p, double q); // Prototype 4 double add(double p, int q); // Prototype 5 // Function calls cout << add( 5, 10); cout << add( 15, 10.0); cout << add( 12.5, 7.5); cout << add( 5, 10, 15); cout << add( 0.75, 5);
Function Overloading
#include <iostream.h> int volume(int); double volume(double, int); long volume(long, int, int); int main() { cout<<"\n Volume of cube = "<< volume(10); cout<<"\n Volume of cylinder = "<< volume(2.5, 8); cout<<"\n Volume of rectangular box = "<< volume(100, 75, 15); return 0; }
Output : Volume of cube = 1000 Volume of cylinder = 157.26 Volume of rectangular box = 112500
// Function Definiton
int volume(int s) { return (s*s*s); } // Cube
double volume(double r, int h) // Cylinder { return (3.14519*r*r*h); } long volume(long h, int b, int h) // rectangular box { return (l*b*h); }