1. Global functions and class member function conversions
Mutual transformation of global functions and member functions: you only need to modify a pointer to this class .
#include <iostream>using namespace Std;class test{public:test (int a, int b) {this->a = A;this->b = b;} member function test &GADD2 (test &t2) {this->a = this->a + t2.a;this->b = this->b + T2.b;return *this;} Public:int A;int b;};/ /global function Test &GADD1 (test &t1, test &t2) {test t3 (0, 0); t3.a = t1.a + t2.a;t3.b = t1.b + t2.b;return t3;} Converting from a member function to a global function requires only a pointer to the This class (a pointer to the class)//from the global function to the member function of the class is, need to subtract one to do the left operand argument void main () {Test T1 (1, 2), T2 (3, 4);//test t3 = GADD1 (t1, t2); T1. GADD2 (T2); System ("Pause");}
2. Friend $2.1 friend function
#include "iostream" using namespace Std;class test2{public: //friend function is characterized by a friend class pointer or a reference friend int Opmem (Test2 *p, int a); Friend function Test2 (int a, int b) { this->a = A; This->b = b; } int Geta () { return this->a; } Protected:private: int A; int b;}; int Opmem (Test2 *p, int a) { p->a = A; return 0;} void Main () { Test2 T1 (1, 2); T1.geta ();//without friend, if you get the private data of the object T1, you need to use the Get method. Opmem (&T1, ten); System ("Pause");}
2.2 Friend Class
#include <iostream>using namespace Std;class a{friend class b;//b is a friend public:void Display () {cout << x << Endl; };p rivate:int x;};/ /design pattern in the combined scene. Representation is set B100, which essentially modifies the data of a. Class b{public:void Set (int i) {aobject.x = i;} void Display () {aobject.display ();} Private:a aobject;}; void Main () {B bobject; Bobject.set (100); Bobject.display (); System ("Pause");}
3.operator OverloadingC + + has many built-inData type, including int,char,double, and so on, each type has manyOperator, such as add, subtract, multiply, divide, etc. When a user defines an object of a class, these operations cannot be performed between two objects, such as an object a+b of the Hyong class, such that a statement without overloading +Operatorwill go wrong. But C + + allows users to put theseOperatoradded to your own class to facilitate operations between class objects likeBuilt-in typesis as convenient as the object a+b, so it is clear that it is easier to understand, of course, you can also define a function that adds to an object in a class, such as A.add (b) Call function Add () to add two objects A and B, but this statement is no easier to understand than a+b.
3.1 operator overloading via a friend function,
#include <iostream>using namespace Std;class fushu{private: int A; int b; Using friend function to implement + operation friend Fushu operator+ (Fushu &c1, Fushu &c2);p ublic: fushu (int a=0, int b=0) { This->a = A; This->b = b; } void print () { cout << a << "+" << b << "i" << Endl; }}; Fushu operator+ (Fushu &c1, Fushu &c2) { Fushu tmp; Tmp.a = c1.a + c2.a; tmp.b = c1.b + c2.b; return TMP;} void Main () { Fushu C1 (1, 2), C2 (3, 4); Fushu C3 = C1+c2; C3.print (); System ("Pause");}
#include <iostream>using namespace Std;class fushu{private: int A; int b; Using the friend function to implement the Pre + + operation friend Fushu &operator++ (Fushu &c2);p ublic: fushu (int a=0, int b=0) { this- >a = A; This->b = b; } void print () { cout << a << "+" << b << "i" << Endl; }}; Fushu &operator++ (Fushu &c2) { c2.a++; c2.b++; return C2;} void Main () { Fushu C1 (1, 2), C2 (3, 4); ++C2; C2.print (); System ("Pause");}
3.2 operator overloading through member functions of a class
#include <iostream>using namespace Std;class fushu{private:int A; int b; Through the Friend function implementation + operation friend Fushu operator+ (Fushu &c1, Fushu &c2);p ublic://through the class member function implementation-Operation Fushu operator-(Fushu &C2) {Fushu tmp; Tmp.a = this->a-c2.a; TMP.B = b-c2.b;//Here can omit the return TMP; }public:fushu (int a=0, int b=0) {this->a = A; This->b = b; } void Print () {cout << a << "+" << b << "i" << Endl; }}; Fushu operator+ (Fushu &c1, Fushu &c2) {Fushu tmp; Tmp.a = c1.a + c2.a; tmp.b = c1.b + c2.b; return TMP;} void Main () {Fushu C1 (1, 2), C2 (3, 4); Fushu C3 = C1+c2;//operator+ (C1,C2); C3.print ();//target through the member functions of the class, complete the operator overload//1 to admit that the operator overload is a function, to write function prototype//2 write function call language c1.operator-(C2)//3 perfect function prototype Fushu C4 = c1-c2;/ /operator-(C1,C2)-->c1.operator-(C2)-->fushu operator-(Fushu &c2); C4.print (); System ("Pause");}
#include <iostream>using namespace Std;class fushu{private: int A; int b; Using the friend function to implement the Pre + + operation friend Fushu &operator++ (Fushu &c2);p ublic: fushu (int a=0, int b=0) { this- >a = A; This->b = b; } void print () { cout << a << "+" << b << "i" << Endl; } PUBLIC://is implemented by the member function of the class-operation fushu& operator--() { this->a--; this->b--; return *this;} ; Fushu &operator++ (Fushu &c2) { c2.a++; c2.b++; return C2;} void Main () { Fushu C1 (1, 2), C2 (3, 4); The former + + operation ++C2 is realized by the friend function . C2.print (); Through the member functions of the class before-operation --c2; C2.print (); System ("Pause");}
Comprehensive:
#include <iostream>using namespace Std;class fushu{private:int a;int b;//using friend function to implement pre + + operation friend Fushu &operator++ (Fushu &C2);//through the friend function to achieve after + + operation friend Fushu operator++ (Fushu &c2, int);//with default parameter placeholder, front + + differentiate Public:fushu (int a=0, int b=0) { This->a = A;this->b = b;} void print () {cout << a << "+" << b << "i" << Endl;} public://through the member functions of the class before-operation fushu& operator--() {This->a--;this->b--;return *this;} Before the member function of the class is implemented--operation fushu& operator--(int)//with default parameter placeholder, followed--differentiate {Fushu tmp;tmp = *this;this->a--;this->b--;return tmp;}}; Fushu &operator++ (Fushu &c2) {C2.a++;c2.b++;return c2;} Fushu operator++ (Fushu &c2, int) {Fushu tmp;tmp = C2;c2.a++;c2.b++;return tmp;} void Main () {Fushu C1 (1, 2), C2 (3, 4),//the first + + operation through the friend function ++c2;c2.print ();//through the member functions of the class before-operation--c2;c2.print ();//After the friend function to achieve the Post + + Operation C2++;c2.print ();//through the member function of the class-operation C2--;c2.print (); System ("Pause");}
3.3 The normal notation of operator overloading can get the private property of the friend class through the friend function, which is equivalent to acquiring a backdoor of the program.
#include <iostream>using namespace Std;class fushu{private:int a;int b;friend ostream& operator<< ( Ostream &cout, Fushu &c);p ublic:fushu (int a=0, int b=0) {this->a = A;this->b = b;} void print () {cout << a << "+" << b << "i" << Endl;} public://the first + + operation through the member functions of the Class Fushu &operator++ () {This->a++;this->b++;return *this;} Pre-operation fushu& operator--() {This->a--;this->b--;return *this by the member function of the Class) After the implementation of the member function of the class-operation Fushu operator--(int)//with the default parameter placeholder, followed--distinguish {Fushu tmp;tmp = *this;this->a--;this->b--;return tmp;} The Post + + operation is implemented by the member function of the class Fushu operator++ (int)//with the default parameter placeholder, followed--differentiates {Fushu tmp;tmp = *this;this->a++;this->b++;return Tmp;}};o stream& operator<< (ostream &co, Fushu &c) {cout << "I am plural:" << endl;cout << c.a << ; "+" << c.b << "i" << Endl;return Co;} void Main () {Fushu C1 (1, 2), C2 (3, 4); ++c2;c2.print ();--c2;c2.print (); C2++;c2.print (); C2--;c2.print (); cout << 1 << Endl; cout << "Hello" << endl;//function returns worth Lvalue, need to return a reference to an object, implement chained programming cout << C2 << "continuous" << endl;//no method To add function operator<< to the cout class, only through the global function. cout.operator<< (C1);//void operator<< (Ostream &out, Complex &c1) system ("pause");
3.4 Application of operator overloading in a project: basic array Frame Array.h
Array.h#ifndef _array_h_#define _array_h_class array{private:int mlength;//array length int* mspace;//memory Space Public:array ( int length); Array (const array& obj);//copy constructor int length ();//get array length void setData (int index, int value); int getData (int index); Array ();}; #endif
Array.cpp
Array.cpp#include "Array.h" array::array (int length) {if (length < 0) {length = 0;} Mlength = Length;mspace = new Int[mlength];} Array::array (const array& obj) {mlength = Obj.mlength;mspace = new Int[mlength];for (int i = 0; i<mlength; i++) {MSPA Ce[i] = Obj.mspace[i];}} int Array::length () {return mlength;} void Array::setdata (int index, int value) {Mspace[index] = value;} int array::getdata (int index) {return mspace[index];} Array::~array () {mlength = -1;delete[] mspace;}
ArrayTest.cpp
Arraytest.cpp#include "Array.h" #include <iostream>using namespace Std;int main () {Array a1 (n); for (int i = 0; i&l T;a1.length (); i++) {A1.setdata (I, i); A[i] = 1;} for (int i = 0; I<a1.length (); i++) {printf ("Array%d:%d\n", I, A1.getdata (i));//printf ("Array%d:%d\n", I, A1[i]);} Array a2 = a1;for (int i = 0; I<a2.length (); i++) {printf ("Array%d:%d\n", I, A2.getdata (i));} System ("pause"); return 0;}
code after overloading the [] = = = = = Operator:
Array.h#ifndef _array_h_#define _array_h_#include <string.h>class array{private:int mlength;//The length of the array int* mspace;//memory space Public:array (int length); Array (const array& obj);//copy constructor int length ();//get array length void setData (int index, int value); int getData (int index); Array ();p Ublic:int & operator[] (int i), void operator= (array &a2), bool operator== (array &a1); bool operator! = (Array &a1);}; #endif
Array.cpp#include "Array.h" array::array (int length) {if (length < 0) {length = 0;} Mlength = Length;mspace = new Int[mlength];} Array::array (const array& obj) {mlength = Obj.mlength;mspace = new Int[mlength];for (int i = 0; i<mlength; i++) {MSPA Ce[i] = Obj.mspace[i];}} int Array::length () {return mlength;} void Array::setdata (int index, int value) {Mspace[index] = value;} int array::getdata (int index) {return mspace[index];} Array::~array () {mlength = -1;delete[] mspace;} int& array::operator[] (int i) {return this->mspace[i];} void Array::operator = (Array &a2) {if (this->mspace! = NULL) {delete[] mspace;} Mlength = A2.mlength;mspace = new Int[mlength]; memcpy (Mspace, A2.mspace, mlength);} BOOL array::operator== (Array &a1) {//First compare length if (mlength! = A1.mlength) {return false;} Compare contents for (int i = 0; i < a1.mlength; i++) {if (A1[i]! = Mspace[i]) {return false;}} return true;} Use equality to judge unequal bool Array::operator!= (Array &a1) {return! A1 = = *this);}
Arraytest.cpp#include "Array.h" #include <iostream>using namespace Std;int main () {Array a1 (n); for (int i = 0; i&l T;a1.length (); i++) {//a1.setdata (I, i); A1[i] = i;} for (int i = 0; I<a1.length (); i++) {//printf ("array%d:%d\n", I, A1.getdata (i));p rintf ("Array%d:%d\n", I, A1[i]);} Array a2 = a1;for (int i = 0; I<a2.length (); i++) {//printf ("array%d:%d\n", I, A2.getdata (i));p rintf ("Array%d:%d\n", I, A1[i]);} Array A3; a3 = A2; Execute = Action if (a2 = = A1) {printf ("equal");} else{printf ("unequal");} System ("pause"); return 0;}
"C + + Institute" (8) global functions and class member functions conversions/friend/operator overloading