-
April 27th, 2013, 08:00 AM
#1
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?
-
April 27th, 2013, 08:11 AM
#2
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. 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)
-
April 27th, 2013, 08:17 AM
#3
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?
-
April 27th, 2013, 08:49 AM
#4
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. 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)
-
April 27th, 2013, 08:56 AM
#5
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.
-
April 27th, 2013, 09:02 AM
#6
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?
-
April 27th, 2013, 09:04 AM
#7
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. 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)
-
April 27th, 2013, 10:38 AM
#8
Re: Area and volume of cylinder
Originally Posted by melissax
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.
-
April 28th, 2013, 10:06 AM
#9
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. 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)
-
April 28th, 2013, 10:28 AM
#10
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. 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)
-
April 30th, 2013, 03:39 PM
#11
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);
-
May 1st, 2013, 12:15 AM
#12
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.
-
May 1st, 2013, 06:49 AM
#13
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. 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)
-
May 22nd, 2013, 12:19 PM
#14
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
-
May 22nd, 2013, 12:23 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|