Showing posts with label fuction. Show all posts
Showing posts with label fuction. Show all posts

Friday, 29 April 2016

Friend Function Example.

#include<iostream.h>
#include<conio.h>
class square
{
int no;
public:
void accept()
{
cout<<"Enter no for square:";
cin>>no;
}
friend void sq(square s);
};
void sq(square s)
{
int ans;
ans=s.no*s.no;
cout<<"Square="<<ans;
}
void main()
{
clrscr();
square s;
s.accept();
sq(s);
getch();
}
/*Output:
Enter no for square:4
Square=16 */

WAP for finding out the weight on any planet using static data member

#include<iostream.h>
#include<conio.h>
class planets
{
static float g;
float w,m;
int ch;
public:
void accept()
{
cout<<"Find weight on:"<<endl;
cout<<"1:MERCURY\n2:VENUS\n3:EARTH\n4:MARS\n5:JUPITER\n6:SATURN\n7:URANUS\n8:NEPTUNE"<<endl;
cout<<"Enter your choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
g=3.61;
break;
case 2:
g=8.83;
break;
case 3:
g=9.8;
break;
case 4:
g=3.75;
break;
case 5:
g=26.0;
break;
case 6:
g=11.2;
break;
case 7:
g=10.5;
break;
case 8:
g=13.3;
break;
}
cout<<"Enter mass:";
cin>>m;
}
void display()
{
w=m*g;
cout<<"Weight="<<w;
}
};
float planets::g;
void main()
{
clrscr();
planets p;
p.accept();
p.display();
getch();
}
/*Output:
Find weight on:
1:MERCURY                                                                      
2:VENUS                                                                        
3:EARTH                                                                        
4:MARS                                                                        
5:JUPITER                                                                      
6:SATURN                                                                      
7:URANUS                                                                      
8:NEPTUNE                                                                      
Enter your choice:                                                            
3
Enter mass:10
Weight=98  */

Thursday, 28 April 2016

Write a program showing Friend Function

 #include<iostream.h>
 #include<conio.h>
 class b;
 class a
 {
int no1;
public:
void accept()
{
cout<<"Enter no 1:";
cin>>no1;
}
void display()
{
cout<<"No 1 is greater";
}
friend void great(a,b);
 };
 class b
 {
int no2;
public:
void accept()
{
cout<<"Enter no 2:";
cin>>no2;
}
void display()
{
cout<<"No 2 is greater";
}
friend void great(a,b);
 };
 void great(a m,b n)
 {
if(m.no1>n.no2)
{
m.display();
}
else
{
n.display();
}
 }
 void main()
 {
clrscr();
a m;
b n;
m.accept();
n.accept();
great(m,n);
getch();
}
/*Output:
Enter no 1:23
Enter no 2:21                                                                  
No 1 is greater */