CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 5 FirstFirst 12345 LastLast
Results 16 to 30 of 68
  1. #16
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    But like abs() function name will be same but using will be different. This is rule as i know.

  2. #17
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Area and volume of cylinder

    Your compiler is right. If you're going to overload a function, you need to change the arguments. You can't change the return type by itself, but what's the point of having functions like Volume have different return types anyway?

  3. #18
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    I tried to use same function with different tasks like abc() functions. This is rule as i know.
    Can you show me which part i have to change.

  4. #19
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Area and volume of cylinder

    Please when you post code format your code properly first and use code tags. Go Advanced, select code and click '#'. Your code is pretty unreadable without.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #20
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Area and volume of cylinder

    Quote Originally Posted by melissax View Post
    I tried to use same function with different tasks like abc() functions. This is rule as i know.
    Can you show me which part i have to change.
    Forget abs and whatever rules you think you know. Obviously, you're wrong, and I've already told you why.

    For the third time, tell me why you need different return types from your functions.

  6. #21
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    "but what's the point of having functions like Volume have different return types anyway?

    you are right as my opinion this is not logical but question is so.

  7. #22
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    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.

  8. #23
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Area and volume of cylinder

    Quote Originally Posted by melissax View Post
    "but what's the point of having functions like Volume have different return types anyway?

    you are right as my opinion this is not logical but question is so.
    I don't understand what you're saying there.

  9. #24
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Area and volume of cylinder

    Quote Originally Posted by melissax View Post
    I tried to use same function with different tasks like abc() functions. This is rule as i know.
    Different overloads of a function can have different return types. So you can have
    Code:
    int abs(int);
    float abs(float);
    double abs(double);
    The reason these are different overloads is because their argument lists differ. As GCDEF explained, you cannot overload a function only on its return type. Therefore, what you have is wrong.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  10. #25
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Area and volume of cylinder

    Quote Originally Posted by GCDEF View Post
    Your compiler is right. If you're going to overload a function, you need to change the arguments. You can't change the return type by itself, but what's the point of having functions like Volume have different return types anyway?
    If you are going to overload a function, the compiler must be able to differentiate between the various overloaded functions to determine the one to use. Simplified, it matches the number and type of the arguments used when you call the function to one of the defined functions with the same name. The type of the function return is not used. Thus for a class function such as area, the formal arguments of each of the various versions of the overloaded function must be different. If more than one of the functions with the same name have the same number of parameters and the same type of parameters then the compiler correctly reports errors.
    Last edited by 2kaud; May 22nd, 2013 at 01:26 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #26
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Area and volume of cylinder

    Quote Originally Posted by melissax View Post
    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.
    b)Just because the radius and height input from the keyboard can be either an integer, float or double does not mean that you require 3 different functions to calculate the area and 3 different functions to calculate the volume. Irrespective of whether your radius/height are entered as integer, float or double - the calculation uses pi so the answer is real (float or double). So it makes no sense whatsoever to have a function that calcluates the area and volume of a cylinder returning a value of type int. A float is a real number. So is a double. If I input say the number 3.45 is this a float or a double? There is no way of knowing.

    As the function needs to return a real, then this is probably best to be a double. So the class variables r and h should be double as well. The class functions will then use the values of the class variables to calculate area, volume etc and return the result as double - so no function parameters are used for area amd volume so there is only one version of each function.

    This means that there also needs to be a method of setting these class variables. They should be private and not public. You can have a class function to set them (with parameters of double) and/or a specific class constructor.

    This is actually a very simple class to calculate area and volume of a cylinder - but you are drastically overcomplicating it.
    Last edited by 2kaud; May 22nd, 2013 at 01:26 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #27
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Area and volume of cylinder

    Code:
    cout <<"Enter radius\n";
    cin>>r;
    cout <<"Enter height\n";
    cin>>h;
    Where have you defined the variables r and h??

    Also note that 3.14 * r * r is not the formula for the surface area of a cylinder. This is the formula for the area of a circle!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #28
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Area and volume of cylinder

    Quote Originally Posted by melissax View Post
    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.
    Re b). Please re-read Nuzzle's post #8 re polymorphism and casting. This is probably to what your teacher is referring (albeit badly) - ie int into a double and float into a double. Hence only one version of area and volume for double.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #29
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    #include<iostream>
    using namespace std;
    const float PI=3.14;

    class cylinder{
    double r, h;
    // Volume of cylinder
    double volume(){
    return PI*r*r*h;
    }// Area of cylinder
    double area(){
    return 2*PI*r*r*+2*PI*r*h;
    }
    }c;
    int main(){
    cout <<"\n";
    cout <<"*************** MENU ******************\n";
    cout <<"\n";
    cout <<"OPTIONS:\n";
    cout <<"1.Area of cylinder\n";
    cout <<"2.Volume of cylinder\n";
    cout <<"3.Exit\n";
    cout <<"Enter radius\n";
    cin>>r;
    cout <<"Enter height\n";
    cin>>h;
    cout<< "Area =" << c.area() << "\n";
    cout<< "Volume=" << c.volume() << "\n";
    getch();
    return 0;
    }
    I tried to do as your opinions still i am getting error. If you help me i will be learn how to do. Thank you.

  15. #30
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    #include<iostream>
    using namespace std;
    const float PI=3.14;

    class cylinder{
    double r, h;
    // Volume of cylinder
    double volume(){
    return PI*r*r*h;
    }// Area of cylinder
    double area(){
    return 2*PI*r*r*+2*PI*r*h;
    }
    }c;
    int main(){
    cout <<"\n";
    cout <<"*************** MENU ******************\n";
    cout <<"\n";
    cout <<"OPTIONS:\n";
    cout <<"1.Area of cylinder\n";
    cout <<"2.Volume of cylinder\n";
    cout <<"3.Exit\n";
    cout <<"Enter radius\n";
    cin>>r;
    cout <<"Enter height\n";
    cin>>h;
    cout<< "Area =" << c.area() << "\n";
    cout<< "Volume=" << c.volume() << "\n";
    getch();
    return 0;
    }

Page 2 of 5 FirstFirst 12345 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
  •  





Click Here to Expand Forum to Full Width

Featured