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?
Printable View
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?
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?
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?
Polymorphism is to do with derived classes from a base class. So what's your base class definition and what are your derived classes?
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.
Solve it without using inheritance and the associated polymorphism then. Since you say you know the formula involved, what's stopping you now?
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.
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.
??
??
Personally, I see two ways to do this: method overloading, or templates:
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);
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);
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.
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.
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
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.
But like abs() function name will be same but using will be different. This is rule as i know.
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?
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.
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.
"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 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.
Different overloads of a function can have different return types. So you can have
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.Code:int abs(int);
float abs(float);
double abs(double);
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.
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.
Where have you defined the variables r and h??Code:cout <<"Enter radius\n";
cin>>r;
cout <<"Enter height\n";
cin>>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!
#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.
#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;
}
2kaud i copied with Alt and # pasted it with nice style but when i send i saw that it doesnt change
Before posting, format your code properly with indentations etc. Then Go Advanced, paste in the code, select the code and then click '#'. To format the code you have already posted, on the bottom of your post #12 you should see 'edit post'. Click this, then click Go Advanced, select the code then click '#' then click save. Your code should then be posted correctly formatted.
In a class, by default all members are private - which basically means that they cannot be accessed outside of the class. This is fine for member variables like r and h, but functions such as area and volume need to accessed anywhere. Hence these need to be public. So you need public: before the start of these function definitions. Have a look atQuote:
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.
http://www.learncpp.com/cpp-tutorial...ss-specifiers/
In main function, you try to obtain the values of r and h, but r and h aren't declared in function main! You need to declare them in main. Then you need a method function of the class to set the class variables r and h from these values entered. The variables r and h used in function main are not the same variables r and h defined within the class. You should also initialise the class variables to 0 when the class is instantiated by using a default class constructor. Have a look at
http://www.learncpp.com/cpp-tutorial...class-members/
At least i show to the terminal but i am selecting for example to the 1 then asking radius and height but doesnt calculate.Code:#include<iostream>
#include<conio.h>
using namespace std;
const float PI=3.14;
class cylinder{
double r, h;
public:
double area();
double volume();
double volume(double radius,double height){
r=radius;
h=height;
return PI*r*r*h;
}
double area(double radius,double height){
r=radius;
h=height;
return 2*PI*r*r*+2*PI*r*h;
}
}c;
int main()
{
int ch;
double x,y;
cout<<"\tCALCULATION OF AREA AND VOLUME";
cout<<"\n1.Area of the cylinder";
cout<<"\n2. Volume of the cylinder";
cout<<"\n\tEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the radius";
cin>>x;
cout<<"\nEnter the height";
cin>>y;
break;
case 2:
cout<<"\nEnter the radius";
cin>>x;
cout<<"\nEnter the height";
cin>>y;
break;
default:
cout<<"\nThe choice entered is a wrong choice";
}
getch();
}
Again, you're guessing without really understanding what you're working with.
First, you need to create an instance of cylinder. In main, write a line of code like this.
Replace the c; from the end of the cylinder declaration with just a ;Code:cylinder c;
Access r and h through your c object, such as c.r.
You're not calculating area and volume because there's nothing in your code to call those functions.
Your code in case 1 and case 2 is identical, which should tell you you've done something wrong. Move the input out of the switch statement and use case 1 and case to call the appropriate function.
Actually i mixed.Code:#include<iostream>
#include<conio.h>
using namespace std;
const float PI=3.14;
class cylinder{
double r, h;
public:
double area();
double volume();
double volume(double radius,double height){
r=radius;
h=height;
return PI*r*r*h;
}
double area(double radius,double height){
r=radius;
h=height;
return 2*PI*r*r*+2*PI*r*h;
}
}c;
int main()
{
int ch;
cylinder c;
c.r;
c.h;
double area(x,y);
double volume(x,y);
cout<<"\tCALCULATION OF AREA AND VOLUME";
cout<<"\n1.Area of the cylinder";
cout<<"\n2. Volume of the cylinder";
cout<<"\n\tEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the radius";
cin>>x;
cout<<"\nEnter the height";
cin>>y;
break;
case 2:
cout<<"\nEnter the radius";
cin>>x;
cout<<"\nEnter the height";
cin>>y;
break;
default:
cout<<"\nThe choice entered is a wrong choice";
cout<< "Area =" << c.area() << "\n";
cout<< "Volume=" << c.volume() << "\n";
}
getch();
}
A lot of your code is nonsense. You need to take a step back and reread whatever book or whatever you've been learning from. We're telling you things that you're not understanding, and you need to have a grasp of the basics that you currently lack. I'm serious that you can't guess your way through C++. You really need to understand what every line you write does.
Well you're getting closer!
In your case statements, you haven't actually called the class functions! So in case 1 for example you need
and similar for case 2.Code:cin>>y;
cout << "Area is: " << c.area(x, y) << endl;
Your calculation for area is not quite right
As you are passing r and h via the functions area(..) and volume(..) you don't actually needCode:return 2*PI*r*r*+2*PI*r*h;
Code:double area();
double volume();
So you are only going to calculate the area and volume when the user enters a wrong choice???? Doh!!Code:default:
cout<<"\nThe choice entered is a wrong choice";
cout<< "Area =" << c.area() << "\n";
cout<< "Volume=" << c.volume() << "\n";
2kaud, i would like to thank you.
With patience you teach me.
Aim of this forum should be guidence.
GCDEF you said "read from book" so you shouldnt give responce.
Why you reply
I understand, but you dont want to help.And you dont know how to talk with a lady.
2kaud, Tomorrow i will try again.
Thank you.
GCDEF is quite correct. You cannot guess c++. You must understand what you are doing. You must have a grasp of the basics before you advance.
As GCDEF said, you really need to take a step back and revise the areas of c++ that have been covered so far so that you understand the basics. Your code indicates that you don't understand - even with the guidance and advice that this forum has provided.
What book(s) etc are you using from which to learn? If you are stuggling with a book, try looking at
http://www.learncpp.com/
which is an easy going tutorial for c++ starting with the basics.
We'll provide what guidance we can, but we are not teachers. This forum cannot teach you c++ - only explain for the code you post problems with guidance to fix. But if you don't understand the basics of c++ you don't understand the guidance we have provided - what is what has been demonstrated in your recent posts. We won't write the code for you - that is cheating. You have to put in the effort to lean properly the language.:)
Whether you like my response or not, it's the truth. I've been trying to help, but you there's only so much any of us can do given your current knowledge. "Learn the basics" is good advice. As to your comment about talking with a lady, that's completely inappropriate in this forum.
I am right, your talking style so rude.
And I am learning basics as doing applications with helpfull programmers.
You dont want my learning.
Anyway
How's that working out for you so far? I'm not seeing any progress. I do want you learning. My advice to you was to get a that would teach you the basics. With the fundamentals you'll get nowhere. Surely this thread is illustrating that. You can did your heals in if it makes you feel better, but in the long run, it won't help you any.
Hi again,
Today, i made some changes as your advices. This is working but i know some mistakes. I wanted to make program automatic can understand number types. I am waiting your comments.
Code:#include<iostream>
#include<conio.h>
using namespace std;
const float PI=3.14;
class calculate
{
public:
void cylinder_area(int r,int h);
void cylinder_area(float r1,float h1);
void cylinder_area(double r2, double h2);
void cylinder_volume(int r3,int h3);
void cylinder_volume(float r4,float h4);
void cylinder_volume(double r5, double h5);
};
void calculate::cylinder_area(int r, int h)
{
cout<<"\n Area of Cylinder: "<<(2 * PI * r)*( r + h);
}
void calculate::cylinder_area(float r1, float h1)
{
cout<<"\nArea of Cylinder "<<(2 * PI * r1)*( r1 + h1);
}
void calculate::cylinder_area(double r2, double h2)
{
cout<<"\nArea of Cylinder "<<(2 * PI * r2)*( r2 + h2);
}
void calculate::cylinder_volume(int r3, int h3)
{
cout<<"\nVolume of Cylinder "<<(2 * PI * r3 * h3);
}
void calculate::cylinder_volume(float r4, float h4)
{
cout<<"\nVolume of Cylinder "<<(2 * PI * r4 * h4);
}
void calculate::cylinder_volume(double r5, double h5)
{
cout<<"\nVolume of Cylinder "<<(2 * PI * r5 * h5);
}
int main()
{
int ch;
int r,h,r3,h3;
float r1,h1,r4,h4;
double r2,h2,r5,h5;
calculate obj;
cout<<"\tTHIS PROGRAM CALCULATES AREA AND VOLUME OF CYLINDER\n";
cout<<"\n1. Area of Cylinder\n";
cout<<"\n2. Volume of Cylinder\n";
cout<<"\n\tPlease select\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter radius of Cylinder\n";
cin>>r;
cout<<"\nEnter height of Cylinder\n";
cin>>h;
obj.cylinder_area(r,h);
break;
case 2:
cout<<"\nEnter radius of Cylinder:\n";
cin>>r3;
cout<<"\Enter height of Cylinder\n";
cin>>h3;
obj.cylinder_volume(r3,h3);
break;
default:
cout<<"\nThe choice entered is a wrong choice";
}
getch();
}
Much better. You still have redundancies in your switch statement. It could/should be written as
Typically, calculate function will return a value, not actually output it. So a better design would be for the cout statements to be in main, and just have your calculate functions return the value they calculate.Code:cout<<"\nEnter radius of Cylinder\n";
cin>>r;
cout<<"\nEnter height of Cylinder\n";
cin>>h;
switch(ch)
{
case 1:
obj.cylinder_area(r,h);
break;
case 2:
obj.cylinder_volume(r,h);
break;
default:
cout<<"\nThe choice entered is a wrong choice";
}
As GCDEF said, much improved:thumb:. Your class calculate now has various overloaded functions for cylinder_area and cylinder_volume. So from you main function, how are you going to allow the user to input data as either int, float or double? How are going to test all the overloaded class functions to make sure they all produce the correct results?