Search This Blog

Showing posts with label functionoverloading. Show all posts
Showing posts with label functionoverloading. Show all posts

Monday, 25 January 2016

Write a program which calculates volume of cube, cylinder, and rectangular box. (Use function overloading).

Practical 8


#include<iostream.h>
using namespace std;

int volume(int s)
{
    return s*s*s;
}
double volume(double r,int h)
{
    return 3.14*r*r*h;
}
float volume(float l,int b,int h)
{
    return l*b*h;
}
int main()
{
    int x,s,h,b;
    double y,r;
    float z,l;
    cout<<"\nEnter the value of s for cube:";
    cin>>s;
    x=volume(s);
    cout<<"\nVolume of cube is:"<<x<<endl;
    cout<<"\nEnter the value of r & h for cylinder:";
    cin>>r>>h;
    y=volume(r,h);
    cout<<"Volume of cylender is"<<y<<endl;
    cout<<"\nEnter valu of l,b & h:";
    cin>>l>>b>>h;
    z=volume(l,b,h);
    cout<<"\nVolume of rectangle:"<<z;
    return 0;
}



output:

enter the value f s for cube:2
volume of cube is 8

enter the value of r & h for cylinder
10
10
volume of cylinder is 314

enter the value of l b h
2
4
2
volume of rectangle is 16

Tuesday, 19 January 2016

Simple Function overloading in C++

 In C++ we are allowed to make only one function in class and we can use it with different ways.
just look over an example below I have created class name gajjar and in main its object is k I've used function karan() to overload in different requirements!

#include<iostream.h>
#include<conio.h>
class gajjar
{
    public:
    void karan(int i,int j)
    {
        cout<<"Addition is:"<<i+j<<endl;
    }
    void karan(float k)
    {
        cout<<"Division by 2 is:"<<k/2<<endl;
    }
    void karan(char k[])
    {
        cout<<"Hi "<<k<<endl;
    }
};
void main()
{
    gajjar k;
    cout<<"\nEnter two values to add:";
    int a,b;
    cin>>a;
    cin>>b;

    k.karan(a,b);

    cout<<"\nEnter float value to divide by 2:";

    float z;
    cin>>z;
    k.karan(z);

    char str[10];
    cout<<"\nEnter your first name:\n";
    cin>>str;
    k.karan(str);
    getch();
}