Inline Function in C++


 

 1.  /* Write a program to define an enumerated data type Month with name of 12 months.

 Assign first month as 1 and display the integer value assigned to the months.*/


#include<iostream>

#include<conio.h>

void main()

{

enum months

{

Janaurary=1,

February,

March,

May,

April,

June,

July,

August,

October,

September,

November,

December,

};

months month1,month2;

month1=Janaurary;

month2=November;

cout<<" Janaurary :"<<month1<<endl;

cout<<" November:"<<month2<<endl;

getch();

  }


Output:






2. Program using inline function to calculate the square of a number

#include <iostream>

#include <conio.h>

inline int square(int n)

{

return(n*n);

}

void main()

{

int number,result;

cout<<"Enter the value of number(n) to be entered:"<<endl;

cin>>number;

result=square(number);

cout<<"The square of given number is :"<<result<<endl;

getch();

}

Output:


3.Program to calculate multiplication and divison by inline function.

#include<iostream>
#include<conio.h>
inline float multiplication(float x,float y)
{
                 return(x*y);
}
                     inline float division(float x,float y)
{
return(x/y);
}
void main()
{
float a,b,result;
cout<<"Enter the value of a & b to perform multiplication:"<<endl;
cin>>a>>b;
result= multiplication(a,b);
cout<<"The Value after multiplication is :"<<result<<endl;
cout<<endl;

cout<<"Enter the value of a & b to perform division:"<<endl;
cin>>a>>b;
result= division(a,b);
cout<<"The Value after division is :"<<result<<endl;

getch();

}

Output: 





4   Program to calculate the volume of cube using inline function.

.#include<iostream>
#include<conio.h>

inline int volume(int S)
{
return(S*S*S);
}
void main()
{
int side,result;
cout<<"Enter the value of Side of Cube to be entered:"<<endl;
cin>>side;
result=volume(side);
cout<<"The Volume of Cube is :"<<result<<endl;
getch();
}
Output:






Comments