Class pointer

Both methods destructor configuration:

range::range (float a ,float b,float c){width=a,length=b,hight=c;
 cout<<"构数成功造函"<<endl;}  

When this method of writing, the function of internal order does not matter.

2、

range::range(float a,float b,float c)
    {width=a,length=b,hight=c,cout<<"构造析构函数成功"<<endl;}  

eg:

class  A
{ public :   // 构造和析构必须public
       A( ) { cout<<"构造 A()"<< endl;   }
     ~A( ) { cout<<"析构~A()"<< endl; }
};
class  B
{ public : 
       B( ) { cout<<"构造 B()"<<endl; }
     ~B( ) { cout<<"析构~B()"<<endl; }
};  

Constructor Compliance: first construct, the destructor
After configuration, the first destructor
***

Construct an array of objects:

  • Each element is an object
  • An array of how many objects the number of times the constructor is called

    Block:

class range
{
    float width;
    float length;
    float hight;
    float area;
    int x;
public:  

range::range(float a,float b,float c)
    {width=a,length=b,hight=c,cout<<"构造析构函数成功"<<endl;}
    void get_x(int a){this->x=a;}  
float range::getarea(void)
    {return width*length*hight;}  
}  
int main()
{
    range data[3]={range(2,3,4),range(5,6,7),range(22,2,2)};
    for(int i=0;i<3;i++)
        cout<<"数组"<<i+1<<"的面积:"<<data[i].getarea()<<endl;
    //point point1,point2;  
system("pause");
return 0;
}  

Run results:

The destructor successful
the destructor successful
the destructor success
array area 1: 24
area array 2: 210
area array 3: 88
Press any key to continue.
***

Object pointers and arrays:

class point
{
    int x,y;
    static int count;
public:
    void set_data(int a,int b){x=a,y=b;}
    void display(void)
    {
        cout<<"x="<<x<<"  "<<"y="<<y<<endl;
    }
};
int main()
{
    point a[5];
    point *p[5];
    for(int i=0;i<5;i++)
    {
        p[i]=&a[i];
        p[i]->set_data(i,i+2);
        //a[i].set_data(i,i+2);
        p[i]->display();
        (*p)++;
    }
system("pause");
return 0;
}

Guess you like

Origin www.cnblogs.com/lixianhu1998/p/11919637.html