CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

Thread: Area and volume of cylinder

1. Member
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. 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?

3. Member
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. 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?

5. Member
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.
Thank you.

6. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,768

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?

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.

8. Elite Member
Join Date
May 2009
Posts
2,413

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.

9. Re: Area and volume of cylinder

??
Last edited by 2kaud; April 28th, 2013 at 10:45 AM.

10. Re: Area and volume of cylinder

??
Last edited by 2kaud; April 28th, 2013 at 10:45 AM.

11. Senior Member
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. Member
Join Date
Apr 2013
Posts
25

Re: Area and volume of cylinder

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. 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.

14. Member
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 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 <<"\n";
cout <<"1.Area of Cylinder\n:";
cout <<"2.Volume of Cylinder:\n";
cout <<"7.Exit\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. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,597

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
•