"C + +" operator overloading

Source: Internet
Author: User

This is a C + + feature and a basic syntax for C + +, but if you look at operator overloading as a special class method, and the name of the class method is an operator, everything becomes simple.

Here's a simple procedure to illustrate the problem:

Declaring two 3x3 two-dimensional matrices A, B, is actually the Matrix class, which stores data with a private one-dimensional array of length 9. Just when it's printed, it looks like a two-dimensional array.

The implementation of ~a or ~b can directly print a and B, that is, the equivalent of a B print out, this is the monocular operator overload.

Implements A+b, returning another matrix C after the addition.

This means that the effect is as follows:


The code is as follows:

#include <iostream>using namespace Std;class matrix{private:int matrix[9];//uses a one-dimensional array of length 9 to store 3x3 matrices Public:matrix () {//Here is a matrix class to write two different parameters of the construction method, mainly for programming convenience. for (int i=0;i<9;i++) {this->matrix[i]=0;}} Matrix (int m11,int m12,int m13,int m21,int m22,int m23,int m31,int m32,int M33) {//This construction method is clearly written, there are 9 numbers, respectively assigned to the matrix of 11 position, 12 position ... ... this->matrix[0]=m11;this->matrix[1]=m12;this->matrix[2]=m13;this->matrix[3]=m21;this->matrix[4 ]=m22;this->matrix[5]=m33;this->matrix[6]=m31;this->matrix[7]=m32;this->matrix[8]=m33;} void operator ~ () {//overload of Monocular operator ~, Strict here, cannot have formal parameters. for (int i=0;i<9;i++) {if (i%3==0) {Cout<<endl;} cout<<matrix[i]<< "";//There is no master matrix representing the private member matrix of the Matrix class involved in the operation. Not I lazy not write This-&gt,, is originally cannot have this->. The same}cout<<endl;} Matrix operator + (Matrix &b) {///pair of overloads of binocular operator +, here strict requirements for formal parameters can only be one. Represents the matrix class after the operator. Matrix c;for (int i=0;i<9;i++) {c.matrix[i]=matrix[i]+b.matrix[i];//This is not a private member of the Matrix class that indicates the matrix of the host operator, matrix} return c;}}; int main () {Matrix A (1,2,3,4,5,6,7,8,9); MAtrix B (9,8,7,6,5,4,3,2,1);cout<< "matrix A is as follows:";~a;cout<< "Matrix B is as follows:";~b;cout<< "Matrix A, B is added as follows:"; ~ (A+B); return 0;}

After reading the code, the role of operator overloading is obvious, if you want to declare a class printing method, according to the major object-oriented programming language, basically for this class to open a void xx () printing method, and then this XX printing method, the class's private members this->aa,this- &GT;BB is printed out, in the main function call, to a.xx () so that calls can be called.

After loading the ~, you can print directly to the Matrix class. The equivalent of declaring a print method called ~ in The Matrix class, overloading the original operator of the previous/C + +-bit inverse.

After the overloading of the +, the main is the two matrix class in the private members of the operation. Equivalent to declaring a method similar to Addmatrix (Matrix &b) in the Matrix class, with the addition of this->matrix[i] and B.matrix[i], after the main function matrix C=a.addmatrix (b) The meaning of it.

Finally, it is worth noting that operator overloading can only be used for operations between private members in a class and not for direct operation of the standard type of C. For example, I would like to load the ^ into a power operation between two int, which is absolutely not possible. This is the C language rule, not for what. Operator overloading begins with an operation that is used to customize a class directly.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"C + +" operator overloading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.