
May 22nd, 2013, 01:32 PM
#16
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.

May 22nd, 2013, 01:40 PM
#17
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?

May 22nd, 2013, 01:49 PM
#18
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.

May 22nd, 2013, 01:51 PM
#19
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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

May 22nd, 2013, 01:53 PM
#20
Re: Area and volume of cylinder
Originally Posted by melissax
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.

May 22nd, 2013, 01:54 PM
#21
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.

May 22nd, 2013, 01:56 PM
#22
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.

May 22nd, 2013, 01:58 PM
#23
Re: Area and volume of cylinder
Originally Posted by melissax
"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.

May 22nd, 2013, 02:04 PM
#24
Re: Area and volume of cylinder
Originally Posted by melissax
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

May 22nd, 2013, 02:04 PM
#25
Re: Area and volume of cylinder
Originally Posted by GCDEF
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 02:26 PM.
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.

May 22nd, 2013, 02:22 PM
#26
Re: Area and volume of cylinder
Originally Posted by melissax
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 02:26 PM.
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.

May 22nd, 2013, 02:39 PM
#27
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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

May 22nd, 2013, 03:27 PM
#28
Re: Area and volume of cylinder
Originally Posted by melissax
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 reread 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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

May 23rd, 2013, 02:30 PM
#29
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.

May 23rd, 2013, 02:31 PM
#30
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;
}
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
This is a CodeGuru survey question.
Featured
