But like abs() function name will be same but using will be different. This is rule as i know.
Printable View
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;
}