1. ## Operator Overloading more than once?

Hi! I thought if I have overloaded the + operator so that it calculates the area of n number circles that are added, is there a way to make it calculate the circumference also?

Can I tell C++ and somehow choose if I want to use the + operator to calculate area or the circumference of the circles? Or is this absolutely impossible to achieve?

Like this:
Add area of two circles: totArea = circle1 + circle2
Add circumference of two circles: totCircum = circle1 + circle2

Shape.h
Code:
```#ifndef SHAPE_H
#define SHAPE_H
#include <string>
using namespace std;

class Shape {
protected:
int x, y;

public:
Shape(int x, int y);
Shape();
virtual ~Shape();

virtual double area() const = 0;
virtual double circumference() const = 0;
virtual string toString() const = 0;
};

#endif```
Shape.cpp
Code:
```#include "Shape.h"

Shape::Shape(int x, int y)
{
this->x = x;
this->y = y;
}

Shape::Shape()
{
}

Shape::~Shape()
{
}```
Circle.h
Code:
```#ifndef CIRCLE_H
#define CIRCLE_H
#include "Shape.h"

class Circle : public Shape {
public:
enum CALC {AREA, CIRCUMFERENCE};
private:
static const double PI;

public:
Circle(int x, int y, double radius);
virtual ~Circle();

double area() const;
double circumference() const;
virtual string toString() const;

double operator+(const Circle& circle);
};

#endif```
Circle.cpp
Code:
```#include "Circle.h"

const double Circle::PI = 3.14159;

Circle::Circle(int x, int y, double radius)
:Shape(x, y)
{
}

Circle::~Circle()
{
}

double Circle::area() const
{
}

double Circle::circumference() const
{
return 2 * this->PI * this->radius;
}

string Circle::toString() const
{
return "X: " + to_string(this->x) +
"\nY: " + to_string(this->y) +
}

double Circle::operator+(const Circle& circle)
{
return (this->area() + circle.area());
}```
Testing.cpp
Code:
```#include <iostream>
#include "Shape.h"
#include "Circle.h"

using namespace std;

int main() {

Shape* circle = new Circle(10, 10, 5.2);

Circle c1(2, 2, 10);
Circle c2(3, 3, 4);

double sum = c1 + c2;

cout << circle->toString() << endl;
cout << "\nArea of two circles: " << sum << endl;

delete circle;

getchar();

return 0;
}``` 2. Originally Posted by DecoratorFawn82
I thought if I have overloaded the + operator so that it calculates the area of n number circles that are added, is there a way to make it calculate the circumference also?
No, do not do this. Given objects x and y, x + y is expected to have something to do with adding x and y, whatever that might mean for the types of x and y. It has nothing to do with calculating area or circumference, so don't overload it to calculate area or circumference.

EDIT:
Code:
```double sum = c1 + c2;

cout << circle->toString() << endl;
cout << "\nArea of two circles: " << sum << endl;```
Can't you see the cognitive dissonance? Why did you name the variable sum when it is computing area? Is it because you are mentally thinking of sum instead of area when you see the binary operator + in that line? Consider this code snippet:
Code:
```double foo = c1 + c2;
double bar = c1 - c2;
cout << "FOO of two circles: " << foo << endl;
cout << "BAR of two circles: " << bar << endl;```
Now, not shown is the code that I used to overload operator+ and operator- to compute the area and circumference of two circles. Your job is to replace the FOO and BAR in the output strings to reflect what I overloaded. Note that I may have overloaded operator+ to compute area, or I may have overloaded it to compute circumference. Or maybe I overloaded it to compute something else altogether, like the sum of the volume of two spheres projected from the circles? Good luck guessing! 3. Now, not shown is the code that I used to overload operator+ and operator- to compute the area and circumference of two circles. Your job is to replace the FOO and BAR in the output strings to reflect what I overloaded. Note that I may have overloaded operator+ to compute area, or I may have overloaded it to compute circumference. Or maybe I overloaded it to compute something else altogether, like the sum of the volume of two spheres projected from the circles? Good luck guessing!
Okay, yeah xD. I get your point.

Can't you see the cognitive dissonance? Why did you name the variable sum when it is computing area? Is it because you are mentally thinking of sum instead of area when you see the binary operator + in that line? Consider this code snippet:
No. I thought of it as sum of two circles areas, but the variable sum should be named more specifically. 4. In my opinion, overloading the + for calculating both the area and the circumference would get really confusing. You might just get on with it for now but trust me when you come back to your code after a couple of months that "+" operator may just make you pull your hair off your head.
So, it's better that you use the standard practice of using the good old nice "camelCase" methods to keep it nice and handy even for the long run. Hope this helps.
Cheers.
(Oops.... Sorry didn't see how old this thread was.) Popular pages Recent additions #include, area, circle, const, double 