Chapter 5 Review of C++
Chapter 5 Review of C++
Review of C++
Introduction:
OOP characteristics:
- Abstraction
- Data encapsulation
- modularity
- Inheritance
- Polymorphism
- Dynamic binding
- Message passing
Chapter 5
Review of C++
OOP characteristics:
- Abstraction: abstraction represents the essential features
of an entity without including background details about it.
Outputting a string:
Syntax: cin.write(string.size);
Example, cin.write(str,20);
Chapter 5
Review of C++
strlen() function: this function is used to return length of the
string and terminated by a null (‘\0’) character.
Syntax: variable=strlen(string);
Example, int l=strlen(“Program”); returns 7
Example:
float cube(int a);
OR
float cube(int);
Chapter 5
Review of C++
Types of arguments:
- Actual arguments
In the function call big=biggest(a,b,c);
- Formal arguments
In the function header int biggest(int a, int b, int c);
Local variables:
Global variables:
Chapter 5
Review of C++
Type of functions or categories of functions:
- Function with no arguments and no return values.
- Function with arguments and no return values.
- Function with no arguments and with return values.
- Function with arguments and with return values.
- Recursive functions.
Chapter 5
Review of C++
Type of functions or categories of functions:
- Function with no arguments and no return values.
Example: void natural()
{
for(int i=0;i<n;i++)
cout<<i;
}
Chapter 5
Review of C++
Type of functions or categories of functions:
- Function with arguments and no return values.
Example: void average(int x, int y, int z)
{
float sum,average;
sum=a+b+c;
average=sum/3.0;
cout<<“Average=”<<average;
}
Chapter 5
Review of C++
Type of functions or categories of functions:
- Function with no arguments and with return values.
Example: int greatest()
{
if(a>b)
return(a);
else
return(b);
}
Chapter 5
Review of C++
Type of functions or categories of functions:
- Function with arguments and with return values.
Example: float interest(float p, float t,float r)
{
si=p*t*r/100.0;
return(si);
}
- Recursive functions: The function that calls by itself is called
recursive function.
Recursion function must have one or more terminating conditions
to terminate recursion. Otherwise, recursion will become infinite.
Chapter 5
Review of C++
Passing default arguments to functions:
Example: consider the prototype,
float interest(float p, int t, float r=0.10);
0.10 is a default value assigned to the argument rate
The function call statement must be,
si=interest(1000.0,2);
Defining a structure:
struct structure_name
{
Data_type1 member_name1;
Data_type2 member_name2;
…
Data_type-n member_name-n;
};
Chapter 5
Review of C++
Example:
struct employee
{
int empid;
char name[10];
char designation[10];
float salary;
};