Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C++ constructors and destructors
C++ constructors and destructors
/*

Design a class with floating-point pointer members. The constructor has an integer parameter count.

It allocates count element space for pointer members to store data. The destructor releases the space pointed by the pointer.

In addition, two function members are designed to accomplish the following functions:

(1) stores data in the space pointed by the pointer;

(2) Returns the average of these numbers.

*/

# include & ltiostream.h & gt

Category floating

{

Public:

FLOAT(int count = 10); //constructor, the default count is 10.

~ FLOAT(); //Destructor

bool setData(float e); //store data

Floating point average (); //Calculate the average value

Private:

float * p;

Integer of data; //Number of data elements

};

FLOAT::FLOAT (integer)

{

P = new floating point [count];

Data quantity = 0;

if(p==NULL)

{

Cout & lt& lt "Not enough memory!" & lt& ltendl

Exit (0);

}

Cout & lt& ltendl & lt& lt "Construction has been completed"

}

FLOAT::~FLOAT()

{

Delete [] p;

Cout & lt& ltendl & lt& lt "array is corrupted!" & lt& ltendl & lt& ltendl

}

bool FLOAT::setData(float e)

{

P[ data quantity] = e;

num _ of _ data++;

}

Floating point floating point:: average value ()

{

float sum = 0;

for(int I = 0; I < the number of data; i++)

sum+= p[I];

Returns the sum/quantity of data;

}

int main()

{

Cout & lt& lt "Enter the number of data to be entered:";

int n;

CIN & gt; & gtn;

FLOAT my FLOAT(n); //Call the constructor

Cout & lt& lt "Please enter"

for(int I = 0; I & ltn;; I++)// Call the setData function.

{

Floating e;

CIN & gt; & gte;

my float . setdata(e);

}

The average value of cout & lt& lt "data is:"

Returns 0;

}