3 C++ 3.1 3.2 3.3 3.4 new delete 3.5 this 3.6 3.7
3.1
3.1 class struct union struct union C class C++ C++
3.1 3.1 #include <alloc.h> struct STRING { typedef char *CHARPTR; // CHARPTR s; // int strlen( ); // STRING(CHARPTR); // ~STRING( ); // }; int STRING::strlen( ) // :: { for(int k =0; s[k]!=0; k++); return k; }
3.1 STRING::STRING(char *t) // :: { for(int k =0; t[k]!=0; k++); s=(char *)malloc(k+1); for(k=0; (s[k]=t[k])!=0; k++); } STRING::~STRING( ) { free(s); }// :: struct STRING x("simple"); void main( ){ STRING y("complex"), *z=&y; int m=y.strlen( ); m=z->strlen( ); }
3.1., ->,.*, ->*, :: ::
3.1 exit exit main abort abort main return return void main( ){ ; if (error) return; ;} abort exit
3.1 Unix, Windows
3.1 3.3 #include <string.h> #include <alloc.h> #include <iostream.h> struct STRING{ char *s; STRING(char *); ~STRING( ); }; STRING::STRING(char *t){ s=(char *)malloc(strlen(t)+1); strcpy(s,t); cout<<"construct: "<<s; }
3.1 STRING::~STRING( ){ if(s==0) return; // s1 cout<<"deconstruct: "<<s; free(s); s=0; // } void main(void){ STRING s1("string varible 1\n"); STRING s2("string varible 2\n"); STRING("Constant\n"); cout<< "RETURN\n"; }
3.1
3.1
3.2 private: C protected: C public: ( C )
3.2 class private struct union public( C )
3.2 3.4 FEMALE class FEMALE{ // private int age; // public: // public typedef char *NAME;// protected: // protected NAME nickname;// NAME getnickname( ); public: // public NAME name; // };
3.2
3.3 inline 2 ( ) ( ) ( )
3.3 3.7 class COMPLEX{ double r, v; public: COMPLEX(double rp, double vp){ r=rp; v=vp; } // inline double getr( ); //inline double getv( ); }; inline double COMPLEX::getv( ){return v; } // void main(void){ COMPLEX c(3, 4); double r=c.getr( ); //getr double v=c.getv( ); // } inline double COMPLEX::getr( ){ return r; } //
3.3 3.8 #include <iostream.h> struct{// ( ) random int x; int random( ) { return x=(23*x+19)%101; } }r={1}; void main(void){ for(int i=0; i<8; i++)cout<<r.random( )<<"\n"; }
3.3 C++ C static #include <iostream.h> static union { int x, y, z; }; int y=5; // : y void main(void){ x=3; cout<<y; // 3} static int x; static int &y=x; static int &z=x; X,y,z
3.3 3.10
3.3 class struct union <long: char, short, int, enum
3.3 3.11 #include <iostream.h> enum ALPHA{a, b, c, d}; class SWITCH{// 28 4 int power:3; int water:5; int gas:5; int oil:6; int start:1; int alarm:3; ALPHA stop:1; int manual:4; };
3.3 union STATE{// 9 2 int speed:9;// speed unsigned run:2; }; void main(void){ cout<<"the size of int is "<<sizeof(int)<<" bytes\n"; cout<<"the size of SWITCH is "<<sizeof(switch)<<" bytes\n"; cout<<"the size of STATE is "<<sizeof(state)<<" bytes\n"; }
3.4 new delete malloc new free delete malloc 0 new malloc free delete free new malloc delete free
3.4 new delete new < > int *p=new int; int *q=new int[20]; delete < > delete p; delete [ ]< > delete [ ]q; delete < >
3.4 new delete 3.12 #include <alloc.h> #include <process.h> class ARRAY{ //class private int *a, r, c; // a r c private public: // public ARRAY(int x, int y); // public ~ARRAY( ); }; ARRAY::ARRAY(int x, int y){ a=new int[(r=x)*(c=y)];// malloc int }
3.4 new delete ARRAY::~ARRAY( ){ //a int if(a){ delete []a; a=0;}// free, delete a } ARRAY x(3, 5); void main(void) { int error=0; ARRAY y(3, 5), *p; p=new ARRAY(5, 7); // malloc ARRAY delete p; // free ARRAY }
3.4 new delete p a r.c=35 p a=null r=5 r=0 c=7 c=0 (a) p=new ARRAY(5,7) (b) p=(array*)malloc(sizeof(array)) p p r.c=35 (c) delete p (d) free (p)
3.4 new delete 3.13 new int (*)[10][20]=int[x][10][20] #include <iostream.h> class A{ //A int i; public: A(int x) { i=x; } // A(int x) A( ) A( ) { i=0; } // };
3.4 new delete void main(void){ int x(3); // int x=3 int *m=new int(5); // m=new int; *m=5 int *n=new int[x]; int (*p)[10]=new int[x][10]; int (*q)[10][20]=new int[x][10][20]; A *r=new A(5); // A(int) A *s=new A[x]; // A( ) A (*t)[10]=new A[x][10]; // A( ) A (*u)[10][20]=new A[x][10][20]; // A( ) } //delete m,n,p,q,r,s,t,u
3.4 new delete new 3.14 STRING STRING x ("Hello!"); x. ~STRING ( ); new (&x) STRING ("The World");
3.5 this this this const this++ this= *this const volatile
3.5 this 3.15 #include <iostream.h> class TREE{ int value; TREE *left, *right; public: TREE (int); ~TREE( ); TREE *find(int); }; TREE::TREE(int value){ // this this->value=value; // TREE::value=value left=right=0; //C++ NULL 0 }
3.5 this TREE::~TREE( ){//this if(left) { delete left; left=0; } if(right) { delete right; right=0; } } TREE* TREE::find(int v){ //this if(v==value) return this; //this if(v<value) //, this=left return left!=0?left->find(v):0; return right!=0?right->find(v):0; // }
3.6 new {} C
3.6 3.16 class A{ int a; public: A(int x) { a=x;} A( ){ a=0; } }; class B{ const int b; // int c, &d, e, f; //b,d,g,h A g, h; // b, c, d, e, f, g, h public: // B h h A( ) B(int y): d(c), c(y), g(y),b(y), e(y){ c+=y; f=y; }//f y }; void main(void){ int x(5); //int x=5 int x(5) A a(x), y=5; //A y=5 A y(5) B b(7), z=(7,8); //B z=(7,8) B z(8) }
3.6 3.18 class A{// ( ) int a; public: A( ); // A(int x); // A(int x, int y); // }; class B{ int b; } //
3.6 void main(void){ A a[6]={3,(4,5),a(6),a(7,8),a( )};// A *b=new A[4]; // B c, *d=new B[4]; // C++ delete [ ]b; delete [ ]d; }
3.7 X sizeof(x) X MOV EAX, X X 4
3.7 3.19 #include <iostream.h> struct MESSAGE{ char flag; // int size; // char buff[255];// long sum; // }; void main(void) { cout<<"size of single word is "<<sizeof(int); cout<<"size of double word is "<<sizeof(long); cout<<"\nsize of Message is: "<<sizeof(message); }
3.7 sizeof(int)=2, sizeof(long)=4: sizeof(message)=1+2+255+4=302 size( ) MESSAGE 0 flag 1 size sizeof(int) size buff sizeof(char) buff 259 1 sum 4 sizeof(message)=1+1+2+255+1+4=304