Friday, 19 January 2018

CPP-Slip13



Slip13

Q.1)  Write a C++ program to calculate area of cone, sphere and circle by using function overloading.

   #include<iostream.h>
#include<conio.h>
int area(int,int);
int area(int);
float area(float);
int main()
{
int radius,side;
float r;
clrscr();
cout<<"\n***********Calculating Area of Cone,Sphere and Circle:";
cout<<"\nEnter the radius:";
cin>>radius;
cout<<"\nEnter the side of the cone:";
cin>>side;
area(radius,side);
cout<<"\n1.Area of Cone:"<<area(radius,side);
area(radius);
cout<<"\n2.Area of Sphere:"<<area(radius);
cout<<"\nEnter the radius of circle:";
cin>>r;
area(r);
cout<<"\n3.Area of Circle:"<<area(r);
getch();
return 0;
}
int area(int radius,int side)
{
return(3.14*radius*side);
}
int area(int radius)
{
return(4*3.14*radius);
}
float area(float r)
{
return(3.14*r*r);
}  


METHOD -2

#include<iostream.h>
#include<conio.h>
class calculate
{
float r_cn,r_s,side;
int r_c;
public:

void area(float r, float s)
{
r_c=r;
side=s;
cout<<"\nArea of cone =\t\t"<<3.14*r_c*side;
}

void area(float r)
{
r_s=r;
cout<<"\nArea of sphere =\t"<<4*3.14*r_s*r_s;
}

void area(int r)
{
r_c=r;
cout<<"\nArea of circle =\t"<<3.14*r_c*r_c;
}
};
void main()
{
float r_cone,side;
int r_circle;
float sp;
clrscr();
calculate c;
cout<<"\nEnter radius of cone\t";
cin>>r_cone;
cout<<"\nEnter side of cone\t";
cin>>side;
cout<<"\nEnter radius of sphere\t";
cin>>sp;
cout<<"\nEnter radius  of circle\t";
cin>>r_circle;
c.area(r_cone,side);
c.area(sp);
c.area(r_circle);
getch();
}


          
Q.2)   Create a class Matrix and Write a C++ program with following functions:

                                i.            To accept a Matrix

                              ii.            To display a Matrix

                            iii.            Overload unary minus ‘–‘ operator to calculate transpose of a Matrix

                            iv.            Overload binary multiplication '*’ operator to calculate multiplication of two matrices                                                                                                 
#include<conio.h>
#include<iostream.h>
#include<process.h>
class  matrix
{  int i,j;
int r;
int c;
int a[10][10];
int b[10][10];
public:
void accept()
{
cout<<"\nEnter how many rowss do you want: ";
cin>>r;
cout<<"\nEnter how many columns do you want: ";
cin>>c;
cout<<"\nENTER ELEMENTS FOR MATRIX1";
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
void disp()
{
cout<<"\nMATRIX 1";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n";
}
}
void operator -()
{
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{
a[i][j]=-a[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\t"<<a[j][i];
}
cout<<"\n";
}
} //End of unary operator
};
void main()
{
int i,n,ch;
matrix mm;
clrscr();
while(1)
{ cout<<"\n\nMenu";
cout<<"\n1.To accept a Matrix";
cout <<"\n2.To display a Matrix  ";
cout <<"\n3.Overload unary minus to calculate transpose ";
cout <<"\n4Overload binary multiplication ' ";
cout<<"\n5.EXIT";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
mm.accept();
break;

case 2:
mm.disp();
break;

case 3:
-mm;
break;

case 4:exit(0);
break;
default: cout<<"\nWrong choice!!!";
}
}
getch();
 }