This and Operator Overloading in C++

Member functions can directly access the class’s member variables for a given object,  including a hidden pointer to itself, called this.

#include 
#include 

class Circle {
private:
	double radius;
	const double cPi = 3.141593;
public:
	double diameter();
	double area();
	Circle& setRadius(double r);
};

Circle& Circle::setRadius(double r) {
	this->radius = r;
	return *this;
}

double Circle::diameter() {
	return this->radius * 2;
}

double Circle::area() {
	return cPi * (radius * radius);
}


int main()
{

	Circle myCircle;

	std::cout << std::fixed << std::showpoint << std::setprecision(2);

	myCircle.setRadius(5);

	std::cout << "Circle's area is " << myCircle.area() << std::endl;

	//associativity of the dot operator is left or right
	std::cout << "Circle's area now is " << myCircle.setRadius(11).area() << std::endl;


    return 0;
}

A friend function of a class is a nonmember function of the class that still has access to the private members of the class. We can make a function be a friend to a class by preceding the function prototype with the reserved word friend. As the function is not a member of the class, we do not include the scope resolution operator or the name of the class in the function’s definition.

Let’s add a friend function to the Circle class.

class Circle {
private:
	double radius;
	const double cPi = 3.141593;
	friend void circleExpand(Circle &cObject);
	friend void circleContract(Circle &cObject);
public:
	double diameter();
	double area();
	Circle& setRadius(double r);
};

//no need for friend keyword in definition
void circleExpand(Circle &cObject) {
	cObject.radius++;
}

void circleContract(Circle &cObject) {
	cObject.radius--;
}

int main()
{
	int i = 1;
	Circle myCircle;

	std::cout << std::fixed << std::showpoint << std::setprecision(2);

	myCircle.setRadius(i);

	while (i++ < 10) {
		std::cout << " The diameter is " << myCircle.diameter() << " and the area is " << myCircle.area() << std::endl;
		circleExpand(myCircle);
	}

    return 0;
}

C++ comes with some operators that are already overloaded. For example, the arithmetic operators are able to handle both integers and floating point numbers, and the addition and subtraction operators can also handle pointers. The right and left shift operators are likewise used to insert and extract data from the input and output streams.

While the only built-in operations on classes are the assignment operator and the member selection operator, it is possible to extend the definitions of most of the operators that can be applied to classes.

In order to write functions to overload operators, we use the reserved operator keyword followed by the operator we want to overload.

C++ operator functions can be either member functions or friend functions of a class. Let’s take a quick look at operator overloading with member functions.

#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>

class Point {
private:
	double x;
	double y;
public:
	void setX(double xValue);
	void setY(double yValue);
	double getX() const;
	double getY() const;
	std::string toString() const;
	double getDistance(Point p) const;
	
	//overloaded operators
	Point operator+(const Point& p) const;
	Point operator-(const Point& p) const;
	bool operator==(const Point& p) const;
	
};


void Point::setX(double xValue) {
	this->x = xValue;
}
void Point::setY(double yValue) {
	this->y = yValue;
}

double Point::getX() const {
	return this->x;
}

double Point::getY() const {
	return this->y;
}

std::string Point::toString() const {
	std::ostringstream oStringStream;
	oStringStream << "(" << this->x << ", " << this->y << ")"; 	
       return oStringStream.str(); 
} 

double Point::getDistance(Point p) const {
 	return sqrt(pow(this->x - p.x, 2) + pow(this->y - p.y, 2));
}


//overloaded operator functions------
Point Point::operator+(const Point& p) const {
	Point tempPoint;
	tempPoint.x = this->x + p.x;
	tempPoint.y = this->y + p.y;
	return tempPoint;
}

Point Point::operator-(const Point& p) const {
	Point tempPoint;
	tempPoint.x = this->x - p.x;
	tempPoint.y = this->y - p.y;
	return tempPoint;
}

bool Point::operator==(const Point& p) const {
	return (this->x == p.x && this->y == p.y);
}
//end overloaded operator functions------------------

int main()
{
	Point pointA;
	pointA.setX(42.73);
	pointA.setY(538);

	Point pointB;
	pointB.setX(14);
	pointB.setY(10.11);

	Point pointC;


	if (pointA == pointB) {
		std::cout << "Point A and Point B equal each other." << std::endl;
	}
	else {
		std::cout << "Point A and Point B do not equal each other." << std::endl;
	}

	std::cout << "Point A is at " << pointA.toString() << " and Point B is at " << pointB.toString() << std::endl;
	std::cout << "The distance between Point A and Point B is " << pointA.getDistance(pointB) << std::endl;

	pointC = pointA - pointB;

	std::cout << "Point C is at " << pointC.toString() << std::endl;


    return 0;
}

Leave a comment