Area and volume of cylinder
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 5 1234 ... LastLast
Results 1 to 15 of 68

Thread: Area and volume of cylinder

  1. #1
    Join Date
    Apr 2013
    Posts
    25

    Area and volume of cylinder

    Hello friends,
    I would like to calculate area and volume of cylinder but radious and height should be integer, float and double. How can i do?
    May you help me?

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Area and volume of cylinder

    First, do you know the formula for calculating the surface area and volume in terms of radius and height? Why do you want to do it for integer, float and double? Why not just double?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    2kaud,

    First of all thank for respond.
    I know formula of them but i am trying to do polymorphism.
    For instance with a uniq calculate(); function i am trying to do tree options.
    But i dont know how to do?

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Area and volume of cylinder

    Polymorphism is to do with derived classes from a base class. So what's your base class definition and what are your derived classes?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    You are right should be with inheritance
    Actually,
    My teacher didnt teach inheritance yet.
    I think question can be without inheritance.
    What is your advise?
    Thank you.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,250

    Re: Area and volume of cylinder

    Solve it without using inheritance and the associated polymorphism then. Since you say you know the formula involved, what's stopping you now?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Area and volume of cylinder

    It might be useful if you said exactly what your assignment is because if you are trying to use polymorphism then you use base and derived classes with inheritance. So if your teacher hasn't covered this yet it's unlikely that you are expected to use polymorphism.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    May 2009
    Posts
    2,413

    Re: Area and volume of cylinder

    Quote Originally Posted by melissax View Post
    I think question can be without inheritance.
    There are at least four different kinds of polymorphisms available in the C++ type system. Only one is associated with inheritenace and overriding.

    In your case the most likely kind of polymorphism is coersion, also called casting. It's when different types "slide" into each other depending on context. For example an int into a double or the other way around. There are rules for what's allowed and not and sometimes the programmer need to be explicit about it by using a cast statement. Otherwise you may get a warning and even an error. This usually happens when the target type is "narrower" than the source type so information may get lost.
    Last edited by nuzzle; April 27th, 2013 at 10:54 AM.

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Area and volume of cylinder

    ??
    Last edited by 2kaud; April 28th, 2013 at 10:45 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Area and volume of cylinder

    ??
    Last edited by 2kaud; April 28th, 2013 at 10:45 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  11. #11
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Area and volume of cylinder

    Personally, I see two ways to do this: method overloading, or templates:

    Code:
    class cylinder_calc {
    public:
         static int get_volume(int r, int h){ ... }
         static float get_volume(float r, float h){ ... }
         static double get_volume(double r, double h){ ... }
    };
    
    double r = 1.0;
    double h = 1.0;
    double v = cylinder_calc::get_volume(r, h);
    or templates:

    Code:
    template <typename T>
    class cylinder_calc {
    public:
        static T get_volume(T r, T h){ ... }
    };
    
    double r = 1.0;
    double h = 1.0;
    double v = cylinder_calc<double>::get_volume(r, h);

  12. #12
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    Ninja Thank you for answer,
    I think i couldnt explain question clearly;
    As a question;
    1. r and h have to input from keyboard but you gave value them and as double.
    2. r and h can input from keyboard and may be integer, double or float so that calculation of volume() and area() can give permission to the polymorphism.
    3.Area() and Volume() functions must be declared as a class of member functions.
    I would like to thank to the nuzzle and 2kaud too.

  13. #13
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Area and volume of cylinder

    I might be useful if you posted what I asked in post #7.

    If you need to input r and h from the keyboard as either int, float or double, then you have three choices. a) Ask the user whether they are to input values as int, float or double and then get input into variables of the appropriate type. b) Assume input is double and get the input as double and then inspect the value entered to determine whether it is actually integer or float. c) Get the input as a string and then inspect it to determine whether it is int, float, double or a bad number and convert to the approriate type.

    Once you have the input entered into variables of the appropriate type, then you can use the class that ninja9578 suggested in post #11 which overloads the get_volume function depending upon the type of parameters used.

    I would suggest that you post your code here for further guidance.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    2kaud i come back i had middterms so i couldnt come.And i want to start subject again with my study.

    I have a question related with function overloading.

    I will calculate area and volume of cylinder with respect to this rules:
    a)Radius and height will be input from keyboard.
    b)Radius and height can be real numbers,float or integer for that reason data input will give permission different style of numbers so that will be use poliymorphism.
    c)Calculation of area and volume functions must belong to the class they must be member functions.
    #include<iostream>
    #include<conio.h>
    using namespace std;

    class cylinder{
    public:
    int r,h,x;
    int area();
    double area();
    float area();
    int volume();
    float volume();
    double volume();
    }s;

    int cylinder::area(){
    return 3.14*r*r;
    }
    int cylinder::volume(){
    return 3.14*x*x*h;
    }
    float cylinder::area(){
    return 3.14*r*r;
    }
    float cylinder::volume(){
    return 3.14*x*x*h;
    }
    double cylinder::area(){
    return 3.14*r*r;
    }
    double cylinder::volume(){
    return 3.14*x*x*h;
    }
    int main(){
    cout <<"\n";
    cout <<"*************** MENU ******************\n";
    cout <<"\n";
    cout <<"Please select option:\n";
    cout <<"1.Area of Cylinder\n:";
    cout <<"2.Volume of Cylinder:\n";
    cout <<"7.Exit\n";
    cout <<"Enter radius\n";
    cin>>r;
    cout <<"Enter height\n";
    cin>>h;
    cout<< "Area =" << s.area() << "\n"
    cout<< "Volume=" << s.volume() << "\n";
    getch();
    return 0;
    }
    I am not sure my way is true i am getting error. May you help me.Thank you

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,055

    Re: Area and volume of cylinder

    You're guessing without understanding and that will never work.

    Why do you have functions with the same name but different return types? C++ doesn't support that.

Page 1 of 5 1234 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center