Object-oriented extension capabilities
Note: When compiling a class, the compiler scans the class definition (without the body of the function) before scanning the class implementation (the function body of each member function, the initialization of the static member variable), so the member function in the class can access the members that appear later.
The member function called by the Const object also requires that the data for the member variable not be modified. The member function can use Const to declare that it does not modify the data of the current object after the parameter table, called the const member function. Therefore, you can only call the const member function with a const object.
The const object can be decorated with mutable if it does have data members that need to be modified.
#include <iostream>#include<ctime>#include<cstdlib>using namespaceStd;typedefintT;classa{//array classes, encapsulating and enhancing the functionality of arrayst*A; intLen; Public: A (intN,t init = T ()): A (NewT[n]), Len (n), cur (0){//0 Initialization for(inti =0; i<n;i++) A[i]=Init; } ~A () {Delete[] A; A =NULL; } int Set(intIdxintval) {A[idx]=Val; } int Get(intidx) { returnA[idx]; } voidRandfill () {Srand (Time (NULL)); for(inti =0; i<len;i++) A[i]= rand ()% -; } intSize ()Const{//after the parameter table, use const to declare that you do not modify the current object's data returnLen; } intNext ()Const{ returna[cur++%Len]; } mutableintCur//allow modified members inside a const object voidUseConsta& x)//the Const object calls a member function that also requires that the data of the member variable not be modified{cout<< x.size () <<Endl; for(intI=0;i<3; i++) cout<< X.next () <<" "; cout<<Endl; }};intMain () {A x (Ten); X.randfill (); for(inti =0; I<x.size (); i++) cout<<x.Get(i) <<" "; cout<<Endl; X.use (x); System ("Pause"); return 0;}
Operator overloading
In C + +, operators are treated as functions. C + + allows programmers to define how operators work by themselves, by defining the appropriate operator functions themselves.
Format: operatior+ operator e.g . operatior&,operatior+,operatior++ ..., called operator overloading. The operator function replaces the operator, where the function parameter corresponds to the operand of the operator, and the function returns the result of the corresponding operator.
C + + Learning Note 06