Articles by "Functions"
Showing posts with label Functions. Show all posts
no image
Write a C++ program to illustrate the concept of towers of hanoi problem.

Program Algorithm:
Step 1: Include the header files
Step 2: If n=1, move the disk from A to C
Step 3: If n=2, move the 1st disk from A to B then move the 2nd disk from A to C and finally move the 1st disk from B to C
Step 4: If n=3, repeat step 3 to move the first 2 disks from A to B using C as intermediate. Then the 3rd disk is moved from A to C. Then repeat step 3 to move 2 disks from B to C using A as intermediate.


Program Code in C++:
#include <iostream.h>
#include <conio.h>
void tower(int a,char from,char aux,char to)
{
if(a==1)
{
cout<<"\t\tMove disc 1 from "<<from<<" to "<<to<<"\n";
return;
}
else
{
tower(a-1,from,to,aux);
cout<<"\t\tMove disc "<<a<<" from "<<from<<" to "<<to<<"\n";
tower(a-1,aux,from,to);
}
}
void main()
{
clrscr();
int n;
cout<<"\n\t\t*****Tower of Hanoi*****\n";
cout<<"\t\tEnter number of discs : ";
cin>>n;
cout<<"\n\n";
tower(n,'A','B','C');
getch();
}


Program Output:
****Towers of Hanoi****
Enter number of disks: 2
Move disc 1 from A to B
Move disc 2 from A to C
Move disc 1 from B to C
no image
Write a C++ program to calculate the area of circle, rectangle and triangle using function overloading.

Program Algorithm:
Step 1: Include the header files
Step 2: Declare the class name as fn with data members and member functions.
Step 3: Read the choice from the user.
Step 4: Choice=1 then go to the step 5.
Step 5: The function area() to find area of circle with one integer argument.
Step 6: Choice=2 then go to the step 7.
Step 7: The function area() to find area of rectangle with two integer argument.
Step 8: Choice=3 then go to the step 9.
Step 9: The function area() to find area of triangle with three arguments, two as Integer and one as float.
Step 10: Choice=4 then exit


Program Code in C++:
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
{
public:
void area(int); //circle
void area(int,int); //rectangle
void area(float ,int,int); //triangle
};
void fn::area(int a)
{
cout<<"Area of Circle:"<<pi*a*a;
}
void fn::area(int a,int b)
{
cout<<"Area of rectangle:"<<a*b;
}
void fn::area(float t,int a,int b)
{
cout<<"Area of triangle:"<<t*a*b;
}
void main()
{
int ch;
int a,b,r;
clrscr();
fn obj;
cout<<"\n\t\tFunction Overloading";
cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:”;
cout<<”Enter your Choice:";
cin>>ch;
switch(ch)
{
}
getch();
}
case 1:
cout<<"Enter Radious of the Circle:";
cin>>r;
obj.area(r);
break;
case 2:
cout<<"Enter Sides of the Rectangle:";
cin>>a>>b;
obj.area(a,b);
break;
case 3:
cout<<"Enter Sides of the Triangle:";
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4:
exit(0);
}
getch();
}


Program Output:
Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 2
Enter the Sides of the Rectangle: 5 5
Area of Rectangle is: 25
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 4
no image
Write a C++ program for implementing the function overloading.

Function overloading in C++ You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload
function declarations that differ only by return type. C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.
An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation). When you call an overloaded function or operator, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.

Function overloading in C++
You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type.


Algorithm:
1. Define a class test.
2. Define the function sum with different arguments with different data types.
3. Define the object for the class test in main function.
4. Call the function using the argument type to perform different operations.
5. Print the output.


Program Code:
#include<iostream.h>
#include<conio.h>
class test
{
public:
int sum(int,int);
float sum(float,float);
double sum(double,double);
};
int test::sum(int a, int b)
{
return(a+b);
}
float test::sum(float a ,float b)
{
return(a+b);
}
double test::sum(double a, double b)
{
return(a+b);
}
void main()
{
test obj;
int choice,ans;
int a,b;
float x,y;
double m,n;
double result=0;
clrscr();
cout<<"\n\t\t main menu";
cout<<"\n\t1. Addition of two integer numbers";
cout<<"\n\t2. Addition of two float numbers";
cout<<"\n\t3. Addition of two double numbers"<<endl;
do
{
cout<<"\n enter your choice:";
cin>>choice;
switch(choice)
{
case 1: cout<<"\n enter 2 numbers";
cin>>a>>b;
result=obj.sum(a,b);
break;
case 2:
cout<<"\n enter 2 number";
cin>>x>>y;
result=obj.sum(x,y);
break;
case 3:
cout<<"\n enter 2 number";
cin>>m>>n;
result=obj.sum(m,n);
break;
default:
cout<<"wrong choice";
break;
}
cout<<"\n\n result"<<result<<endl;
cout<<"\ndo you want to continue:";
ans=getch();
}
while(ans=='y'||ans=='Y');
getch();
}


Thanks
Mukesh Rajput
Write a C++ program to implement function overloading.

Algorithm:
Step 1: Create a class.
Step 2: Declare the data members.
Step 3: Declare multiple member functions with the same name.
Step 4: Create an object for the class inside the main function.
Step 5: Call the overloaded functions by comparing the arguments list.
Step 6: Print the output.
Step 7: Stop the program.


Program Code:
//Function Overloading
#include<iostream.h>
#include<conio.h>
class volume
{
int l,b,h,a1,b1,c1;
int vol1,vol2,vol3;
public:
void get()
{
cout<<"Enter the value of l,b,h:";
cin>>l>>b>>h;
vol1=l*b*h;
}
void get(int x,int y,int z)
{
l=x;b=y;h=z;
vol2=l*b*h;
}
void get(int x,int y=12)//Default Argument
{
a1=x;b1=y;
vol3=a1*b1;
}
void display()
{
cout<<"The volumes are:";
cout<<"\nVol1="<<vol1<<"\nVol2="<<vol2<<"\nVol3="<<vol3;
}
};
void main()
{
clrscr();
volume v;
v.get();
v.get(5,6,7);
v.get(5);
v.display();
getch();
}




Output:
Enter the value of l,b,h:10
20
30
The volume is:
Vol1=6000
Vol2=210

Vol3=60
no image
To write a C++ program to swap the given numbers by call by value & address.




ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables and create the function with normal and pointer variables.
Step 3: Call the appropriate functions.
Step 4: Swap the values and print it.
Step 5: Stop the program.

PROGRAM:
#include<iostream.h>
#include<conio.h>
void swap(int x,int y);
void swap(int*x,int*y);
void main()
{
int a,b;
clrscr();
a=10;
b=20;
cout<<"In main function:\n";
cout<<"a: "<<a<<endl;
cout<<"b: "<<b<<endl;
swap(a,b); //swapping with default arguments
cout<<"In main function:\n";
cout<<"a: "<<a<<endl;
cout<<"b: "<<b<<endl;
swap(&a,&b);//swapping with default arguments
cout<<"In main function:\n";
cout<<"a: "<<a<<endl;
cout<<"b: "<<b<<endl;
getch();
}
void swap(int x ,int y)
{
x=x+y;
y=x-y;
x=x-y;
cout<<endl<<"\t"<<"CALLING FUNCTION 1 "<<endl;
cout<<endl<<"after swapping a is: "<<x<<endl;
cout<<endl<<"after swapping b is: "<<y<<endl;
}
void swap(int *x ,int *y)
{
*x=*x+*y;
*y=*x-*y;
*x=*x-*y;
cout<<endl<<"\t"<<"CALLING FUNCTION 2 "<<endl;
cout<<endl<<" after swapping a is: "<<*x<<endl;
cout<<endl<<" after swapping b is: "<<*y<<endl;
}





OUTPUT:
In main function:
a: 10
b: 20
CALLING FUNCTION 1
after swapping a is: 20
after swapping b is: 10
In main function:
a: 10
b: 20
CALLING FUNCTION 2
after swapping a is: 20
after swapping b is: 10
To write a C++ program to implement functions with default arguments.

Algorithm:
Step 1: Start the program.
Step 2: Declare the function add with default arguments.
Step 3: Call the function with defaults arguments inside the main function.
Step 4: stop the program.


Program Code:
#include<iostream.h>
#include<conio.h>
void add(int=10, int=20)
void main()
{
clrscr();
add();
add(30);
getch();
}
void add(int a, int b)
{
int c;
c=a+b;
cout<<”The value is”<<c<<endl;
}




Output:
The value is 30
The value is 50
no image
What is function overloading? 

Function overloading means we can use the same function name to create functions that perform a variety of different tasks. 
For example, we overloaded add( ) function, which further handles different data types as shown below. 
Different Function Declatations
1. int add( int a, int b); //add function with 2 arguments of same type 
2. int add( int a, int b, int c); //add function with 3 arguments of same type 
3. float add( int a, float d); //add function with 2 arguments of different type 
Function calls for defferent functions declarations above: 
1. add (3 , 4); // this function call uses prototype of function declaration 1 
2. add (3, 4, 5); //this function call uses prototype of function declaration 2  
3. add (3 , 10.0); //this function call uses prototype of function declaration 3 


C++ program which implement the Function overloading feature.
Program Code:
#include<iostream>
using namespace std;
int add( int a, int b);
int add( int a, int b, int c);
float add( int a, float d);
int main()
{
int a, b, c;
float d;
cout<<"Enter a and b and c: ";
cin>>a>>b>>c;
cout<<endl;
cout<<"Enter d : ";
cin>>d;
cout<<endl;
add (a , b);
cout<<endl;
add (a, b, c);
cout<<endl;
add (a , d);
return 0;
}
int add( int a, int b)
{
cout<<"Addition of a and b :"<<a+b;
}
int add( int a, int b, int c)
{
cout<<"Addition of a, b and c :"<<a+b+c;
}
float add( int a, float d)
{
cout<<"Addition of a and d :"<<a+d;
}


The program output is tested on www.jdoodle.com
Output:
Enter a and b and c: 1 2 3 
Enter d : 12.5
Addition of a and b :3
Addition of a, b and c :6
Addition of a and d :13.5


Thanks
Mukesh Rajput
no image
Raising a number n to a power p is the same as multiplying n by itself p times. Write a function called power ( ) that takes a double value for n and an int value for p, and returns the result as the double value. Use a default argument of 2 for p, so that if this argument is omitted, the number will be squared. Write a main ( ) function that gets values from the user to test this function.


Implementation of the above problem:
#include<iostream.h>
#include<conio.h>
double power(double n,int p=2);
int main()
{
double n,r;
int p;
char c;
clrscr();
cout<<"enter the number:";
cin>>n;
do
{
cout<<"do you want to enter power(y/n)?:";
cin>>c;
if(c=='y')
{
cout<<"enter the power to be raised:";
cin>>p;
r=power(n,p);
}
else
{
if(c=='n')
{
p=2;
r=power(n);
}
else

cout<<"invalid choice\n";
}
}
while(c!='y'&&c!='n');
cout<<n<<"^"<<p<<"("<<n<<" raised to the power "<<p<<")="<<r;
getch();
return 0;
}
double power(double n,int p)
{
double r=1;
int i;
if(p<0)
r=1/power(n,-p);
else
for(i=1;i<=p;i++)
r=r*n;

return(r);
}


Thanks
Mukesh Rajput
no image
Write a C++ program to count the number of persons inside a bank, by increasing count whenever a person enters a bank, using an increment(++) operator overloading function, and decrease the count whenever a person leaves the bank using a decrement(--) operator overloading function inside a class.


Implementation of the above problem:
#include<iostream.h>
#include<conio.h>
class counter
{
int count;
public:
counter()
{
count=0;
}
counter operator++(int)
{
count++;
}
int get_count()
{
return count;
}

counter operator--(int)
{
count--;
}
};
void main()
{
clrscr();
counter c1;
cout<<"Initial No Of People "<<c1.get_count()<<endl;
c1++;
c1++;
c1++;
cout<<"Present No Of People "<<c1.get_count()<<endl;
c1--;
c1--;
cout<<"Present No Of People "<<c1.get_count()<<endl;
getch();

}


Thanks
Mukesh Rajput
no image
Write a C++ program to create two objects of a class called company and add their data members using an operator overloaded function for ‘+’ operator and '-'operator


Implementation of the above problem:
#include<iostream.h>
#include<conio.h>
class company
{
int sal;
int nop;
public:
friend void print(company &);
company()
{
sal=0;
nop=0;
}
company operator+(company);
company company:: operator-(company c)
{
company temp;
temp.sal=sal-c.sal;
temp.nop=nop-c.nop;
return temp;
}
void set_data(int s, int n)
{
sal=s;
nop=n;
}
};
void print(company &sum)

{
cout<<"Total Salary is :"<<sum.sal<<endl;
cout<<"Total No: of Parts is:"<<sum.nop<<endl;
}
company company:: operator+(company c)
{
company temp;
temp.sal=sal+c.sal;
temp.nop=nop+c.nop;
return temp;
}
void main()
{
clrscr();
int s,n;
company sum,ram,clif,sub;
cout<<"Enter the salary for Ram";
cin>>s;
cout<<"Enter No:Of Parts for Ram:";
cin>>n;
ram.set_data(s,n);
cout<<"Enter the salary for clif";
cin>>s;
cout<<"Enter No:Of Parts for clif:";
cin>>n;
clif.set_data(s,n);
sum=ram+clif;
print(sum);
sub=sum-ram;
print(sub);
getch();
}



Thanks
Mukesh Rajput