on a board level, operators in c++ can be classified into two type: unary operators and binary operators.

unary operators

operators that function on a single operand are called unary operators.

type of unary operators

operator name
++ increment
-- decrement
* pointer dereference
-> member selection
! logical not
& Address-of
~ one's complement
+ unary plus
- unary negation
conversion operators conversion into other types

programming a ++/-- operator

prefix increment operator (++):

1
2
3
4
5
6
Date & operator ++()
{
//operator inmplementation code

return this;
}

postfix increment operator (++)

1
2
3
4
5
6
Date operator ++ (int)
{
Date copy (*this)

return copy;
}

A calendar class that handles day, month, and year, and allows incrementing and decrementing days. demolist12_1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;

class Date
{
private:
int day, month, year;

public:
Date(int urDay, int urMonth, int urYear)
: day(urDay), month(urMonth), year(urYear) {}

Date &operator++()
{
++day;
return *this;
}

Date &operator--()
{
--day;
return *this;
}

void DisplayDate()
{
cout << day << "/" << month << "/" << year << endl;
}
};

int main()
{
Date mysteryDay(30, 7, 2023);
cout << "the date initialized to ";
mysteryDay.DisplayDate();

++mysteryDay;
cout << "using prefix increment operator the date updated to ";
mysteryDay.DisplayDate();

--mysteryDay;
cout << "using prefix decrement operator the date updated to ";
mysteryDay.DisplayDate();

return 0;
}

the output

1

prefix increment operators as demonstrated in this sample need to return a reference to the instance after the increment operation.

To support postfix increment and decrement:

1
2
3
4
5
6
7
8
9
10
11
12
13
Date operator ++(int)
{
Date copy(day,month,year);
++day;
return copy;
}

Date operator --(int)
{
Date copy(day,month,year);
--day;
return copy;
}

The syntax using postfix:

1
2
3
4
5
6
7
mysteryDay++;
cout << "using postfix increment operator the date updated to ";
mysteryDay.DisplayDate();

mysteryDay--;
cout << "using postfix decrement operator the date updated to ";
mysteryDay.DisplayDate();

comparison:
Better using prefix increment of decrement for avoiding the creation of temporary copy.

programming conversation operators

if you use demolist12_1 and insert the following line in main():

1
cout << holiday; //error in absence of conversion operator

The code would result in the following compile failure:

1
error: binary '<<': no operator found which takes a right-hand operand of type 'Date'.

This error essentially indicates that cout doesn't know how to interpret an instance of Date as class Date. Adding an operator to get cout work with an instance of type Date.

e.g. Implementing conversion operator const char* for class Date demolist12_2·

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

class Date
{
private:
int day, month, year;
string dateInString;

public:
Date(int inMonth, int inDay, int inYear)
: month(inMonth), day(inDay), year(inYear){};

operator const char *()
{
ostringstream formattedDate;
formattedDate << month << "/" << day << "/" << year;

dateInString = formattedDate.str();
return dateInString.c_str();
}
};

int main()
{
Date Holiday(12, 25, 2016);

cout << "Holiday is on: " << Holiday << endl;
// string strHoliday (Holiday); // OK!
// strHoliday = Date(11, 11, 2016); // also OK!

return 0;
}

As shown in line 16 and 23, the benefit of implementing operator const char* is visible in line inmain. Now an instance of class Date can directly be used in a cout statement, taking advantage of the fact that cout understands const char*.

programming dereference operator (*) and member selection operator (->)

This lesson takes a brief look at how overloading operators helps in making smart pointers work.

e.g. using smart pointer unique_ptr to manage a dynamically allocated instance of class Date.demolist12_3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <memory>
using namespace std;

class Date
{
private:
int day_, month_, year_;
string date_String_;

public:
Date(int input_day, int input_month, int input_year) : day_(input_day), month_(input_month), year_(input_year){};

void DisplayDate()
{
cout << day_ << "/" << month_ << "/" << year_ << endl;
}
};

int main()
{
unique_ptr<int> smart_int_ptr(new int);
*smart_int_ptr = 42;

cout << "smart int is " << *smart_int_ptr << endl;

unique_ptr<Date> SmartHoliday(new Date(5, 8, 2023));
cout << "The new date is ";
SmartHoliday->DisplayDate();//using pointer operator ->!!!

return 0;
}

Line 22 where I declare a smart pointer to type int. this line shows template initialization syntax for smart pointer class unique_ptr. Similarly, line 27 declare a smart pointer to an instance of Date. The pointer class std::unique_ptr that is smart because it implements operator(*) and operator(->).