Object-oriented Programming-c++ Operator overloading & Type conversion (Static) "11th class notes"

Source: Internet
Author: User

This class continues to explain the [], and other specific examples of operator overloading

A case of type conversion for a class of a single parameter of C + + is also explained

Finally, the third function of static is mentioned slightly: data members at rest

I have commented on the details, we can look at it slowly.

Any questions can be in this article under the message I will promptly answer:)

#include <iostream>#include<cmath>using namespacestd;classMyArray {Private:    float*p; unsignedintsize; Public: MyArray (unsignedintLen =0); ~MyArray (); unsignedintGetSize ()Const; DoubleMoudar ()Const; Const float&operator[] (intIndexConst;//Subscript operators can only be overloaded in member functions    float&operator[] (intindex);//Only member function};myarray::myarray (unsignedintLen) {    float* TMP =New float[Len]; if(NULL = = tmp) {//Check whether new work OKcout <<"memory allocation error!"<<Endl; P=NULL; Size=0; } Else{p=tmp; Size=Len;  for(inti =0; i < Len; ++i) {P[i]=0.0f; }}}myarray::~MyArray () {if(NULL! =p) {delete [] p; Size=0; } P= NULL;//ensure that the P pointer is empty after the destruction}unsignedintMyarray::getsize ()Const {    returnsize;}DoubleMyarray::moudar ()Const {    //float sum = 0.0f;    Doublesum =0.0;  for(inti =0; i < size; ++i) {floatA =P[i]; Sum+ = A *A; } Sum=sqrt (sum); returnsum;}Const float& MyArray::operator[] (intIndexConst {    returnP[index];}float& MyArray::operator[] (intindex) {    returnP[index];} Ostream&operator<< (Ostream & out,ConstMyArray &Ma) {    intSize =ma.getsize ();  out<<"{";  for(inti =0; I < size-1; ++i) { out<< Ma[i] <<", "; }     out<< Ma[size-1] <<"}"<<Endl; return  out;}intMain () {MyArray MA (4);//init one object    DoubleLength =Ma.moudar (); floatelement = ma[2];//Get The elementma[3] =12.5f;//Update the element valuema[0] =3.8f; cout<< Ma << Endl;//!! Only Golbal function    return 0;}

//= () []->* only overloaded by member function//<< When the output is expressed, it can only be overloaded as a global function, and when the shift operation is expressed,#include<iostream>using namespacestd;classMyclassa { Public:    inti; Myclassa (intK = $): I (k) {cout <<"Myclassa Init"<<Endl;};} ;classMYCLASSB {//Class B is responsible for creating a child object, and if nothing is written, it is with the default//constructor is created, otherwise it needs to be written in the member initialization list Public:    intm;                Myclassa A; //object member, when a B object is created, the A object is constructed firstMyclassa *operator- (); //MYCLASSB * operator ();MYCLASSB (intJ): A (j), M (Ten) {cout <<"MYCLASSB Init"<<Endl;};} ;/*MYCLASSB * myclassb::operator-> () {cout << "Myclassb * myclassb::operator-> ()" << Endl; return this;}*/Myclassa* MYCLASSB::operator-() {cout<<"Myclassa * myclassb::operator-> ()"<<Endl; return&A;}intMain () {MYCLASSB B ( at); cout<< B->i <<Endl; //cout << b->m << Endl;    return 0;}

//type conversion of a class for a single parameter of C + +//Conversion of Classes#include<iostream>using namespacestd;//class A;classInteger {inti; //A; Public:    //explicit: Tell the compiler that if you want to convert, it must be an explicit        ExplicitInteger (intK =0): I (k) {cout <<"Integer (int k = 0)"<<Endl;} Integer&operator= (ConstInteger &obj); Friend Ostream&operator<< (Ostream & out,ConstInteger &ing); //operator A () {return A;}    Explicit operator int() {returnI }//type conversion function    Explicit operator Double() {returni +45.51; }//type conversion function};ostream&operator<< (Ostream & out,ConstInteger &ing) {     out<< ing.i <<Endl; return  out;} Integer& Integer::operator= (ConstInteger &obj) {    if( This= = &obj)return* This;//Check self-assigned valuei =obj.i; cout<<"operator = (const Integer & obj)"<<Endl; return* This;}intMain () {Integer A ( $), B; b= Integer ( the); cout<< b <<Endl; intm =int(b);//convert an integer type to reshapecout << M <<Endl; DoubleD =Double(b); cout<< D <<Endl; return 0;}

//static data member#include<iostream>using namespacestd;classInteger {//static int i;    Static intNumber//the entire class has only one version, all objects are shared Public: Integer (intK =0) { ++Number ;} Static intGetNumber ()Const{returnNumber ;}};intInteger::number =0;intMain () {Integer Zhao, Jin, Wei, Shi, Tian, Cai; cout<< Zhao.getnumber () <<Endl; cout<< Integer::getnumber () <<Endl; return 0;}

Object-oriented Programming-c++ Operator overloading & Type conversion (Static) "11th class notes"

Related Article

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.