Function Template Using C++ Programming
#include
#include
template
void swap(t &x,t
&y)
{
t temp=x;
x=y;
y=temp;
}
void fun(int a,int
b,float c,float d)
{
swap(a,b);
cout<<"\n\nc and d before swaping
:"<
swap(c,d);
cout<<"\nc and d after swaping
:"<
}
void main()
{
int
a,b;
float c,d;
clrscr();
cout<<"Enter A,B values(integer):";
cin>>a>>b;
cout<<"Enter C,D values(float):";
cin>>c>>d;
fun(a,b,c,d);
getch();
}
Output
Enter A, B values
(integer): 10 20
Enter C, D values
(float): 2.50 10.80
A and B before
swapping: 10 20
A and B after
swapping: 20 10
C and D before
swapping: 2.50 10.80
C and D after
swapping: 10.80 2.50
Function Overloading Using C++ Programming
#include
#include
#include
#define pi 3.14
class fn
{
public:
void
area(int);
void
area(int,int);
void
area(float ,int,int);
};
void fn::area(int a)
{
{
cout<<"Area of
Circle:"<
}
void fn::area(int a,int b
{
}
void fn::area(float t,int a,int b)
{
cout<<"Area of
triangle:"<
}
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)
{
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();
}
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
Friend Function Using C++ Programming
#include
#include
class base
{
int
val1,val2;
public:
void get()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return
float(ob.val1+ob.val2)/2;
}
void main()
{
clrscr();
base obj;
obj.get();
cout<<"\n Mean value is : "<
getch();
}
Output
Enter two values: 10,
20
Mean Value is: 15
No comments:
Post a Comment