i want to write a program can caulate the area of a circle and squar

using overloaded functions, i tried but it didnt work , what i missing here ?

#include<iostream>
using namespace std;

class math
{
      int r;
      double d;
      
public:
       void set_value(int t)
       {
            r = t;
            
            }
            void set_value2(double f)
            {
                 d=f;
                 }
            int area (int )
            {
                return r*r;
                }
            int area(double)
            {
                return d*d;
                }
                };
                


      
int main()
{
    int y,u,g,h;
    char c;
    math cir,squ;
    cout<<"Enter the redius:\n";// for the circle 
    cin>>y;
    cir.set_value(y);
    u=cir.area(int);
    cout<< "area of the circle = " << u;
    cout << "Enter the side size:\n";// for the square
    cin>> g;
    squ.set_value2(g);
    h=squ.area(double);
    cout<<" area of the square = " << h;
    cin>>c;
    return 0;
}

try this :

#include <iostream>


using namespace std;

class Cal
{
    private:
    int r;
    double d;
    public:
    //you dont need two separate methods to get values
    void SetValues(int,double);
    double Area(double);
    //i'm not sure about this one, can your area be an integer?
    int Area(int);
};

int Cal::Area(int i)
{
    return i*i;
}

double Cal::Area(double d)
{
    return d*d;
}

void Cal::SetValues(int rr,double dd)
{
    r = rr;
    d = dd;
}


int main()
{
    Cal cl;
   cl.SetValues(10,12.3);
   cout << cl.Area(1) << endl;


    return EXIT_SUCCESS;
}

please mark this thread solved if you found a solution

Edited 6 Years Ago by mshauny: n/a

I really don't know why your using overloaded functions when you really should be using overloaded constructors like below:

#include<iostream>

class math
{
  public:
    math(double side1, double side2):area(side1 * side2) {}
    math(double rad):area(3.14 * rad * rad) {}
    double getarea() const {return area;}
  private:
    double area;
};
                


      
int main()
{
  math cir(1.1);
  math rect(1.2, 4.5);
  
  std::cout<<"rect area->"<<rect.getarea()<<std::endl;
  std::cout<<"cir area->"<<cir.getarea()<<std::endl;
  return 0;
}

Edited 6 Years Ago by gerard4143: n/a

i want to write a program can caulate the area of a circle and squar

using overloaded functions, i tried but it didnt work , what i missing here ?

#include<iostream>
using namespace std;

class math
{
      int r;
      double d;
      
public:
       void set_value(int t)
       {
            r = t;
            
            }
            void set_value2(double f)
            {
                 d=f;
                 }
            int area (int )
            {
                return r*r;
                }
            int area(double)
            {
                return d*d;
                }
                };
                


      
int main()
{
    int y,u,g,h;
    char c;
    math cir,squ;
    cout<<"Enter the redius:\n";// for the circle 
    cin>>y;
    cir.set_value(y);
    u=cir.area(int);
    cout<< "area of the circle = " << u;
    cout << "Enter the side size:\n";// for the square
    cin>> g;
    squ.set_value2(g);
    h=squ.area(double);
    cout<<" area of the square = " << h;
    cin>>c;
    return 0;
}

I think I see what you were going for.Unfortunately, C++ does not allow this type of overloading.

There was a mistake or two in your syntax...
In this line: int area(double)
You are saying that you will pass the function a double value.
But in this line: h=squ.area(double);
This is not a valid function call. The function in your class is properly overloaded, but it is expecting you to either pass it an integer or double value. You are not passing any value--it seems to me you are just trying to tell it here to use the "double" version of the function, right?

This is not possible within the confines of a single class in C++. There is no way to specify that one function is a "double version" and one is an "integer version", without actually passing it a value. It is possible with virtual functions however. Try learning about inheritance and virtual functions. Let me know if you have any questions.
-Greywolf

I think I see what you were going for.Unfortunately, C++ does not allow this type of overloading.

There was a mistake or two in your syntax...
In this line: int area(double)
You are saying that you will pass the function a double value.
But in this line: h=squ.area(double);
This is not a valid function call. The function in your class is properly overloaded, but it is expecting you to either pass it an integer or double value. You are not passing any value--it seems to me you are just trying to tell it here to use the "double" version of the function, right?

This is not possible within the confines of a single class in C++. There is no way to specify that one function is a "double version" and one is an "integer version", without actually passing it a value. It is possible with virtual functions however. Try learning about inheritance and virtual functions. Let me know if you have any questions.
-Greywolf

Here we Go !

#include<iostream>
using namespace std;

class math 
{
      int q;
      double w;
      
    
      
public:
       void set_value(int e)
       {
            q=e ;
            
            }
       void set_value1(double r)
       {
            w = r;
            }
            int area(int q)
            {
                return (q*q)*3.14;
                }
                double area(double w )
                { 
                       return w*w;
                       }
                };
int main()
{
    int t,y;
    double u,i;
    char o;
    math cir,squ;
    cout<<"Enter the redius:\n";// for the circle 
    cin>>t;
    cir.set_value(t);
    y=cir.area(t);
    cout<< "area of the circle = " << y ;
    cout << "\n Enter the side size:\n";// for the square
    cin>> u;
    squ.set_value1(u);
    i=squ.area(u);
    cout<<" area of the square = " << i;
    cin>>o;
    return 0;
}

Thanks guys

Not really how I would do it but I might not be understanding the question...I would lean towards a solutionss like this:

#include<iostream>

class math
{
	public:
		enum shape {Circle, Rectangle};

		math(double side1, double side2, shape theshape = Rectangle):area(side1 * side2), itsshape(theshape) {}
		math(double rad, shape theshape = Circle):area(3.14 * rad * rad), itsshape(theshape) {}

		double getarea() const {return area;}
		shape getitsshape() const {return itsshape;}
	private:
		double area;
		shape itsshape;
};
                


      
int main()
{
	math shape1(1.1, math::Circle);
	math shape2(1.2, 4.5, math::Rectangle);

	if (shape1.getitsshape() == math::Circle)
	{
		std::cout<<"shape is a circle->"<<shape1.getarea()<<std::endl;
	}
	else
	{
		std::cout<<"shape is a rectangle->"<<shape1.getarea()<<std::endl;
	}

	if (shape2.getitsshape() == math::Circle)
	{
		std::cout<<"shape is a circle->"<<shape2.getarea()<<std::endl;
	}
	else
	{
		std::cout<<"shape is a rectangle->"<<shape2.getarea()<<std::endl;
	}  
	return 0;
}
This article has been dead for over six months. Start a new discussion instead.