String Concatenation via Member Function Overloading
Operator overloading within a class can be achieved using member functions. In the following example, the addition operator is overloaded to concatenate two text buffers. The left-hand operand is modified and returned by reference.
#include <iostream>
#include <cstring>
class TextBuffer {
char data[256];
public:
TextBuffer(const char* input) {
std::strncpy(data, input, sizeof(data) - 1);
data[sizeof(data) - 1] = '\0';
}
TextBuffer& operator+(TextBuffer& rhs) {
std::strncat(data, rhs.data, sizeof(data) - std::strlen(data) - 1);
return *this;
}
const char* retrieve() const { return data; }
};
int main() {
TextBuffer txt1("Hello"), txt2("World");
txt1 = txt1 + txt2;
std::cout << txt1.retrieve() << std::endl;
return 0;
}Output:
HelloWorldArithmetic Operations on Imaginary Numbers Using Friend Functions
When overloading binary operators where the left-hand operand is not an object of the class, or when accessing private members directly is preferred, friend functions are utilized. This example implements addition, subtraction, multiplication, and division for imaginary numbers.
#include <iostream>
class ImaginaryNumber {
double re, im;
public:
ImaginaryNumber(double r = 0.0, double i = 0.0) : re(r), im(i) {}
void print() const {
std::cout << "(" << re << ", " << im << "i)" << std::endl;
}
friend ImaginaryNumber operator+(const ImaginaryNumber& a, const ImaginaryNumber& b);
friend ImaginaryNumber operator-(const ImaginaryNumber& a, const ImaginaryNumber& b);
friend ImaginaryNumber operator*(const ImaginaryNumber& a, const ImaginaryNumber& b);
friend ImaginaryNumber operator/(const ImaginaryNumber& a, const ImaginaryNumber& b);
};
ImaginaryNumber operator+(const ImaginaryNumber& a, const ImaginaryNumber& b) {
return ImaginaryNumber(a.re + b.re, a.im + b.im);
}
ImaginaryNumber operator-(const ImaginaryNumber& a, const ImaginaryNumber& b) {
return ImaginaryNumber(a.re - b.re, a.im - b.im);
}
ImaginaryNumber operator*(const ImaginaryNumber& a, const ImaginaryNumber& b) {
return ImaginaryNumber(a.re * b.re - a.im * b.im, a.im * b.re + a.re * b.im);
}
ImaginaryNumber operator/(const ImaginaryNumber& a, const ImaginaryNumber& b) {
double denominator = b.re * b.re + b.im * b.im;
return ImaginaryNumber((a.re * b.re + a.im * b.im) / denominator, (a.im * b.re - a.re * b.im) / denominator);
}
int main() {
ImaginaryNumber val1(3.0, 4.0), val2(5.0, -10.0), res;
res = val1 + val2; res.print();
res = val1 - val2; res.print();
res = val1 * val2; res.print();
res = val1 / val2; res.print();
return 0;
}Output:
(8, -6i)
(-2, 14i)
(55, -10i)
(-0.2, 0.4i)Prefix and Postfix Increment Overloading
To distinguish between prefix (++obj) and postfix (obj++) increment operators, the postfix version takes an extra dummy integer parameter. The prefix operation increments the value and returns the updated object, whereas the postfix operation returns the original value before incrementing.
#include <iostream>
class Stopwatch {
int mins, secs;
public:
Stopwatch(int m = 0, int s = 0) : mins(m), secs(s) {}
Stopwatch operator++() { // Prefix
secs++;
if (secs >= 60) {
secs -= 60;
mins++;
}
return *this;
}
Stopwatch operator++(int) { // Postfix
Stopwatch temp = *this;
secs++;
if (secs >= 60) {
secs -= 60;
mins++;
}
return temp;
}
void show() const { std::cout << mins << ":" << secs << std::endl; }
};
int main() {
Stopwatch sw(34, 59);
++sw; sw.show(); // 35:0
Stopwatch sw2 = sw++; // sw becomes 35:1, sw2 is 35:0
sw.show(); sw2.show();
return 0;
}Output:
35:0
35:1
35:0Custom Tally Class with Addition Operator
A constant member function can overload the addition operator to ensure the original objects remain unmodified during the operation. The result is returned as a new instance of the class.
#include <iostream>
class Tally {
int total;
public:
Tally(int val = 0) : total(val) {}
Tally operator+(const Tally& other) const {
return Tally(total + other.total);
}
void log() const {
std::cout << "Total: " << total << std::endl;
}
};
int main() {
Tally t1(3), t2(4);
Tally t3 = t1 + t2;
t3.log();
return 0;
}Output:
Total: 7