Thread: Operator Overloading more than once?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question 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;
    	double radius;
    
    
    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)
    {
    	this->radius = radius;
    }
    
    
    Circle::~Circle()
    {
    }
    
    
    double Circle::area() const
    {
    	return this->PI * this->radius * this->radius;
    }
    
    
    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) +
    		   "\nRadius: " + to_string(this->radius);
    }
    
    
    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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:
    Look at your own code:
    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!
    Last edited by laserlight; 02-10-2018 at 10:31 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    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. #4
    Registered User
    Join Date
    Feb 2018
    Posts
    6
    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 subscribe to a feed

Similar Threads

  1. C++ Operator overloading
    By Aslaville in forum C++ Programming
    Replies: 7
    Last Post: 12-16-2014, 09:40 AM
  2. Overloading + operator
    By juice in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2011, 11:03 AM
  3. Operator Overloading
    By xiaolim in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2009, 03:51 AM
  4. Operator overloading
    By karb0noxyde in forum C++ Programming
    Replies: 3
    Last Post: 11-08-2004, 10:23 AM
  5. Operator overloading
    By Trauts in forum C++ Programming
    Replies: 7
    Last Post: 12-10-2002, 10:03 AM

Tags for this Thread