how to overload the plus operator ?


I keep getting the error too many parameters for this operator function.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;


class Book {

private:

	string author_ = "unknown";
	int isbn_ = 0;
	int price_ = 0;
	string title_ = "unknown";


public:

	string getAuthor() {

	  return author_; 
	
	}


	string getTitle() {

		return title_;

	}


	int getIsbn() {

		return isbn_;

	}

	int getPrice() {

		return price_;

	}


	void setAuthor(string author) {
		author_ = author; 		
	}

	/*
	// Overload + operator to add two Books
	Book operator+(const Book& b) {
		Book b3;
		b3.author_ = this->author_ + b.author_;
		b3.isbn_ = this-> isbn_ + b.isbn_;
		b3.price_ = this-> price_ + b.price_;
		b3.title_ = this->title_ + b.title_;
		return b3;
	}*/



	Book operator + (const Book& b1 , const Book& b2) {
		Book b3;
		b3.author_ = b1.author_ + b2.author_;
		b3.isbn_ =  b1.isbn_ + b2.isbn_;
		b3.price_ = b1.price_ + b2.price_;
		b3.title_ = b3.title_ + b3.title_;
		return b3;
	}





};


bool sortbyauthor(Book lhs, Book rhs)
{
	return lhs.getAuthor() < rhs.getAuthor(); 
}


ostream& operator<<(ostream& os, Book& dt)
{
	os << dt.getAuthor() << '/' << dt.getIsbn() << '/' << dt.getPrice() << '/' << dt.getTitle() << endl; 
	return os;
}





int main()
{
	Book b1;
	b1.setAuthor("bb");

	Book b2;
	b2.setAuthor("aa");

	vector <Book> v;

	v.push_back(b1); 
	v.push_back(b2);

	cout << "before sorting by author" << endl; 

	for (auto x : v)
		cout << x ;

	sort(v.begin(), v.end(), sortbyauthor);

	cout << "after sorting by author" << endl;

	for (auto x : v)
		cout << x ; 


	cout << "after sorting by author displaying using ranged for loop " << endl;
	for(int unsigned i = 0; i < v.size(); ++i)
	{
		cout << v[i] ;
	}
	
	Book b3;
	b3 = b2 + b1; 
	cout << " b1 + b2 is " << b3 << endl; 


}

Member functions only need one additional argument.
Non-member functions need both arguments.

I find Wikipedia to be useful:
https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
if i use this function :

Book operator+(const Book& b) {
Book b3;
b3.author_ = this->author_ + b.author_;
b3.isbn_ = this-> isbn_ + b.isbn_;
b3.price_ = this-> price_ + b.price_;
b3.title_ = this->title_ + b.title_;
return b3;
}


and at the line in main():

b3 = b2 + b1;

how do i know which Book (b1 or b2) is referred by the "this" keyword in the overloaded function ?

how come this always refers to the Book which comes second in the expression :

b3 = b2 + b1;

which is b1 in this case ?






> how do i know which Book (b1 or b2) is referred by the "this" keyword in the overloaded function ?

b2 + b1 is equivalent to either b2.operator+(b1) or operator+( b2, b1 )


> how to overload the plus operator ?

This is the canonical way:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct A
{
    // step one: first overload the compound assignment operator +=
    A& operator+= ( const A& that ) { this->a += that.a ; return *this ; }
    A& operator+= ( int n ) { this->a += n ; return *this ; }

    int a = 0 ;
};

// step two: then overload the + operator in terms of the += operator
A operator+ ( A first, const A& second ) { return first += second ; }
A operator+ ( A first, int n ) { return first += n ; }
A operator+ ( int n, A first ) { return first += n ; }

You can make the function a friend and would work like you expect:

friend Book operator + (const Book& b1 , const Book& b2) { // Note: friend in front
Hi JLBorges,

i think u might have gotten it mixed up.


> how do i know which Book (b1 or b2) is referred by the "this" keyword in the overloaded function ?

b2 + b1 is equivalent to either b2.operator+(b1)

it should be

b2 + b1 is equivalent to either b1.operator+(b2) , because the this keyword refers to b1,
as I have tried it.
If operator+ is a member function, b2 + b1 is equivalent to b2.operator+(b1)
The member function is invoked on object b2 and the this pointer points to b2
(the object on the left hand side of the +)
I am totally sure that the this keyword

for the line

b3 = b2 + b1;

points to b1.

you can try it out with my code.
you can try it out with my code.


OK, here goes:
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Book {

private:

    string author_ = "unknown";
    int isbn_      = 0;
    int price_     = 0;
    string title_  = "unknown";

public:

    string getAuthor() { return author_; }
    string getTitle()  { return title_;  }
    int getIsbn()      { return isbn_;   }
    int getPrice()     { return price_;  }

    void setAuthor(string author) { author_ = author; } 

    friend ostream& operator<<(ostream& os, Book& dt);

    Book operator+(const Book& b)
    {
        cout << "this is " << *this << '\n';
        Book b3;
        b3.author_ = this->author_ + b.author_;
        b3.isbn_   = this->isbn_   + b.isbn_;
        b3.price_  = this->price_  + b.price_;
        b3.title_  = this->title_  + b.title_;
        return b3;
    }
};


bool sortbyauthor(Book lhs, Book rhs)
{
    return lhs.getAuthor() < rhs.getAuthor(); 
}


ostream& operator<<(ostream& os, Book& dt)
{
    os << dt.getAuthor() << '/' << dt.getIsbn() << '/' 
       << dt.getPrice() << '/' << dt.getTitle() << endl; 
    return os;
}


int main()
{
    Book b1;
    b1.setAuthor("b1");

    Book b2;
    b2.setAuthor("b2");

    cout << "b1 is " << b1 << '\n';
    cout << "b2 is " << b2 << '\n';
    
    Book b3;
    b3 = b2 + b1; 
    cout << " b2 + b1 is " << b3 << endl; 
}



b1 is b1/0/0/unknown

b2 is b2/0/0/unknown

this is b2/0/0/unknown

 b2 + b1 is b2b1/0/0/unknownunknown
Last edited on
Topic archived. No new replies allowed.