Operators in C++

Jinku Hu Oct 12, 2023
  1. Common Operators Provided in C++ Language
  2. Overloaded Operations in C++
Operators in C++

This article will demonstrate multiple methods about how to use operators in C++.

Common Operators Provided in C++ Language

Operators are the essential part of expressions in C++ programming. They provide different operations conducted on supplied operands, and the evaluated results are usually utilized by the programmer as needed. We can loosely divide operands into two types: common and special operators. Common operators are almost universally implemented operations in most programming languages, and these include arithmetic, logical, comparison, assignment, member access operators, and several others.

Similar to the rules in algebra, there are precedence and associativity characteristics when multiple operators form the expression. We will summarize some of these rules in this article, but the full description should be referred to the language documentation and operator precedence table. Note that rules of precedence and associativity for basic arithmetic operations are similar to the common math rules. Thus, the expressions in the following example output the integers 20 and 10 instead of 60 and 30.

#include <iostream>

using std::cout;
using std::endl;

int main() {
  cout << 10 + 2 * 5 << endl;
  cout << 40 - 20 - 10 << endl;

  return EXIT_SUCCESS;
}

Output:

20
10

The programmer can use parentheses to override general rules of precedence and associativity, similarly as done in algebra. Each parenthesized compound expression will be treated as a unit that is evaluated separately. One of the common mistakes when using the pointer arithmetic is to miss the parentheses.

If we access the element in the array using the pointer arithmetic plus the dereference operation, we need to put the arithmetic one in parentheses. Otherwise, the value pointed by the given pointer would be incremented by the number operand and not the pointer itself because dereference operator has higher precedence than the addition.

#include <iostream>

using std::cout;
using std::endl;

int main() {
  int vec[] = {10, 22, 63, 14};
  cout << *vec + 2 << endl;
  cout << *(vec + 2) << endl;

  return EXIT_SUCCESS;
}

Output:

12
63

Overloaded Operations in C++

One of the most powerful features one can encounter in the C++ language is that it allows the programmer to define how operators should behave when called on user-defined objects like classes. Since this feature gives alternative operations to the given operator, it’s called overloaded operators. However, take note that the number of operands and precedence can’t be modified by implementing an overloaded operator.

The following code snippet declares a Person class that contains two string data members and overloads the+ operator for this type. Overloaded operators are implemented similar to other class functions with special names, starting with the operator keyword followed by the operator symbol itself. These functions can have a return type and parameter list. Mind that one of the restrictions of operator overloading is that the programmer can not create new operators like $> or !!.

#include <iostream>
#include <string>
#include <utility>
#include <vector>

using std::cout;
using std::endl;
using std::string;
using std::vector;

class Person {
 public:
  Person(string s1, string s2) : name(std::move(s1)), surname(std::move(s2)) {}

  Person operator+(const Person &p) {
    Person ret(name + p.name, surname + p.surname);
    return ret;
  }

  void printPerson() { cout << name << " " << surname << endl; }

 private:
  string name;
  string surname;
};

int main() {
  Person P1("Buddy", "Rich");
  Person P2("Lady", "Bee");
  P1 = P1 + P2;

  P1.printPerson();

  exit(EXIT_SUCCESS);
}

Output:

BuddyLady RichBee
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Operator