function overloading.c ++

9

Click here to load reader

Upload: navya-s-radhakrishnan

Post on 12-May-2017

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Function Overloading.C ++

#include<iostream.h>

#include<conio.h>

#include<math.h>

#define pi 3.14

class shapes

{

float result;

public:

shapes()

{

result=0.0;

}

float area(float);

float area(float,float);

float area(float,float,float);

float volume(float);

float volume(float,float);

float volume(float,float,float);

};

float shapes::area(float r)

{

result=4*pi*pow(r,2);

return result;

}

Page 2: Function Overloading.C ++

float shapes::area(float r,float s)

{

result=pi*pow(r,2) +pi*(r*s);

return result;

}

float shapes::area(float l,float h,float w)

{

result=2*(l*h)+2*(l*w)+2*(w*h);

return result;

}

float shapes::volume(float r)

{

result=4/3*(pi*pow(r,3));

return result;

}

float shapes::volume(float r,float h)

{

result=(pi*pow(r,2)*h)*1/3;

return result;

}

float shapes::volume(float l,float h,float w)

{

result=l*h*w;

return result;

}

Page 3: Function Overloading.C ++

void main()

{

int ch;

char con;

float r,l,h,w,s;

shapes sh;

enum choice{sphere=1,cone,rectangular_solid};

choice c;

do

{

clrscr();

cout<<"\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";

cout<<"\n\t\t@@@FUNCTION OVERLOADING@@@\n";

cout<<"\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";

cout<<"\n\t1.Sphere\n\n\t2.Cone\n\n\t3.Rectangular Solid\n\n";

cout<<"\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";

cout<<"\nEnter your choice:\t";

cin>>ch;

c=choice(ch);

switch(c)

{

case sphere:cout<<"\nEnter the radius:\t";

cin>>r;

cout<<"\nArea of sphere is:\t"<<sh.area(r);

cout<<"\n\nVolume of sphere:\t"<<sh.volume(r);

Page 4: Function Overloading.C ++

break;

case cone: cout<<"\nEnter the radius:\t";

cin>>r;

cout<<"\nEnter the height:\t";

cin>>h;

cout<<"\nEnter the side:\t";

cin>>s;

cout<<"\nArea of cone is:\t"<<sh.area(r,s);

cout<<"\n\nVolume of cone:\t"<<sh.volume(r,h);

break;

case rectangular_solid:cout<<"\nEnter the length:\t";

cin>>l;

cout<<"\nEnter the height:\t";

cin>>h;

cout<<"\nEnter the width:\t";

cin>>w;

cout<<"\nSurface area of rectangle is:\t"<<sh.area(l,h,w);

cout<<"\n\nVolume of rectangle:\t"<<sh.volume(l,h,w);

break;

default: cout<<"Invalid choice\n";

break;

}

cout<<"\n\nPress y to Continue:\t";

cin>>con;

Page 5: Function Overloading.C ++

}while(con=='y');

cout<<"\n \n......THANK YOU......\n";

getch();

}

Page 6: Function Overloading.C ++

OUTPUT:

@@@FUNCTION OVERLOADING@@@

1.Sphere

2.Cone

3.Rectangular Solid

*********************************

Enter your choice: 1

Enter the radius: 2.2

Area of sphere is: 60.790401

Volume of sphere: 33.434723

Press y to Continue: y

Enter your choice: 2

Enter the radius: 2.3

Enter the height: 2

Enter the side: 4

Area of cone is: 45.4986

Volume of cone: 11.073733

Press y to Continue: y

Enter your choice: 3

Enter the length: 2

Enter the height: 3

Enter the width: 4

Surface area of rectangle is: 52

Volume of rectangular solid: 24

Press y to Continue: n

Page 7: Function Overloading.C ++
Page 8: Function Overloading.C ++