C + + Learning Note 06

Source: Internet
Author: User

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

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.