2012年2月20日 星期一

Unary vs Binary, Member vs.Non-Member


Only two types of overloaded operator functions may exist, unary or binary, and they may be defined as member or non-member functions. So, there are only four ways to define these functions. The following table summarizes these possibilities. Assume @ represents an overloaded operator. There is, of course, no such operator available in C++.

Example 6-7 - Matrix Arithmetic

The following example is an implementation of Matrix addition. It is meant to demonstrate the overloaded + and = operators. This example also uses the this operator in member functions, so that objects can be followed  in the program using the program output.

 Example 6-7 - Matrix Arithmetic

The following example is an implementation of Matrix addition. It is meant to demonstrate the overloaded + and = operators. This example also uses the this operator in member functions, so that objects can be followed in the program using the program output.

1      // File: ex6-7.cpp
2     
3      #include <iostream>
4      #include <cstdlib>
5      using namespace std;
6     
7      class Matrix
8      {
9      private:
10          int** element;
11          int rows;
12          int cols;
13          void alloc(void);
14          void release(void);
15      public:
16          Matrix(int, int);
17          Matrix(const Matrix&); // copy constructor
18          ~Matrix();
19          void print(void);
20          Matrix operator+(const Matrix&);
21          Matrix& operator=(const Matrix&);
22      };
23     
24      Matrix::Matrix(int r, int c)
25      {
26          cout << "Constructor called for object " << this <<endl;
27          rows = r;
28          cols = c;
29          alloc();
30          // initialize Matrix elements with random numbers 0-9
31          for (int i = 0; i < rows; i++)
32              for (int j = 0; j < cols; j++) element[i][j] = rand()%10;
33      }
34     
35      Matrix::Matrix(const Matrix& arg)
36      {
37          cout << "\nIn copy constructor for object " << this;
38          cout << ", argument: " << &arg << endl;
39          rows = arg.rows;
40          cols = arg.cols;
41          alloc();
42          for (int i = 0; i < rows; i++)
43             for (int j = 0; j < cols; j++)
44                element[i][j] = arg.element[i][j];
45      }
46     
47      Matrix::~Matrix(void)
48      {
49          cout << "\n~~ Destructor called for object: " << this << endl;
50          release();
51      }
52     
53      void Matrix::alloc(void)       // allocate heap memory for elements
54      {
55          cout << "Allocate heap memory for Matrix " <<this<<" elements\n";
56          element = new int*[rows];
57          for (int i = 0; i < rows; i++)
58              element[i] = new int[cols];
59      }
60     
61      void Matrix::release(void)
62      {
63          cout << "I got rid of Matrix " << this << "'s elements\n";
64          for (int i = 0; i < rows; i++) delete [] element[i];
65          delete [] element;
66      }
67     
68      void Matrix::print(void)
69      {
70          cout << "\nMatrix values for object: "<< this << endl;
71          for (int i = 0; i < rows; i++)
72          {
73             for (int j = 0; j < cols; j++)
74                cout << element[i][j] << '\t';
75             cout << endl;
76          }
77          return;
78      }
79     
80      Matrix Matrix::operator+(const Matrix& arg)
81      {
82          cout << "\nExecuting operator+ for object: " << this;
83          cout << ", argument: " << &arg << endl;
84          if (rows != arg.rows || cols != arg.cols)
85          {
86              cerr << "Invalid Matrix addition\n";
87              return (*this);
88          }
89          Matrix temp(rows,cols);
90     
91          for (int i = 0; i < rows; i++)
92             for (int j = 0; j < cols; j++)
93                temp.element[i][j] = element[i][j] + arg.element[i][j];
94             temp.print();
95          return temp;
96      }
97     
98      Matrix& Matrix::operator=(const Matrix& arg)
99      {
100          cout << "\nExecuting operator= for object: " << this;
101          cout << ", argument: " << &arg << endl;
102          for (int i = 0; i < arg.rows; i++)
103              for (int j = 0; j < arg.cols; j++)
104                  element[i][j] = arg.element[i][j];
105          return *this;
106      }
107     
108      int main(void)
109      {
110          Matrix a(2,2),b(2,2),c(2,2);
111          a.print();
112          b.print();
113          c = a + b;
114          c.print();
115     
116          return 0;
117      }


******  Output  ******
Constructor called for object 0x1427fff0
Allocate heap memory for Matrix 0x1427fff0 elements
Constructor called for object 0x1427ffea
Allocate heap memory for Matrix 0x1427ffea elements
Constructor called for object 0x1427ffe4
Allocate heap memory for Matrix 0x1427ffe4 elements

Matrix values for object: 0x1427fff0
6    0
2    0

Matrix values for object: 0x1427ffea
6    7
5    5

Executing operator+ for object: 0x1427fff0, argument: 0x1427ffea
Constructor called for object 0x1427ffcc
Allocate heap memory for Matrix 0x1427ffcc elements

Matrix values for object: 0x1427ffcc
12   7
7    5

In copy constructor for object 0x1427ffde, argument: 0x1427ffcc
Allocate heap memory for Matrix 0x1427ffde elements

~~ Destructor called for object: 0x1427ffcc
I got rid of Matrix 0x1427ffcc's elements

Executing operator= for object: 0x1427ffe4, argument: 0x1427ffde

~~ Destructor called for object: 0x1427ffde
I got rid of Matrix 0x1427ffde's elements

Matrix values for object: 0x1427ffe4
12   7
7    5

~~ Destructor called for object: 0x1427ffe4
I got rid of Matrix 0x1427ffe4's elements

~~ Destructor called for object: 0x1427ffea
I got rid of Matrix 0x1427ffea's elements

~~ Destructor called for object: 0x1427fff0
I got rid of Matrix 0x1427fff0's elements

How would you change the operator=() function so that you could assign a matrix with a different number of rows or columns?

沒有留言:

張貼留言