Dynamic creation and release of C++ objects

  • 2020-04-02 01:50:13
  • OfStack

= = = = = = = = = = = = = first below gives an example of a new and delete the basic application, review the basic usage of it = = = = = = = = = = = =


#include<iostream>
using namespace std;
int main()
{
 int *p;//Defines a pointer p to an int variable
 p=new int(3);//Creates a storage space for integers and returns an address to that storage space
 cout<<*p<<endl;
 delete p;//Free up that space
 char *p_c;
 p_c=new char[10];//Creates a space for an array of characters (including 10 elements) and returns the address of the first element
 int i;
 for(i=0;i<10;i++)
 {
  *(p_c+i)=i+'0';
 }
 for(i=0;i<10;i++)
 {
  cout<<*(p_c+i);
 }
 delete  [] p_c; 
 cout<<endl;
 return 0;
}

Operation results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130906155218593.png ">

Similarly, the new and delete operators can also be applied to the dynamic creation and deletion of classes

When memory is allocated dynamically with the new operator, it returns the value of a pointer to the new object, the starting address of the allocated memory space. The user can get this address and use it to access the object.

Of course, C++ also allows new objects to be initialized when new is executed.


Box *pt =new Box(12,15,18);

Dynamic objects created with new are generally accessed through Pointers instead of object names, and are mainly applied to dynamic data structures such as linked lists. To access the nodes in the linked list, it is not necessary to store the address of the next node in the previous node through the name of the object, so as to find the next node from the previous node and form a connection relationship.

When an object created with new is no longer needed, it can be freed with the delete operator. When the delete operator is executed, the destructor is called automatically before the memory space is freed.


#include<iostream>
using namespace std;
class Box
{
 public:
  Box(int w,int l,int h);
  ~Box();
  int width;
  int length;
  int height; 
};
Box::Box(int w,int l,int h)
{
 width=w;
 length=l;
 height=h;
 cout<<"======== Call constructor =======n";
} 
Box::~Box()
{
 cout<<"======== Call the destructor =======n";
} 
int main()
{
 Box * p=new Box(12,13,15);
 cout<<"n The data member of the object to which the output pointer points "<<endl;
 cout<<p->width<<"t"<<p->length<<"t"<<p->height<<endl;
 delete p;
 return 0;
}

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130906164805250.png ">

Then, take a more practical example

= = = = = = = = = = = = = = = = = = = = = = = = = title = = = = = = = = = = = = = = = = = = = = = = =

Write a program that requires you to input the number of students, and then input their student number, name and English and math scores.

Then print out the information of all the students


#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Student
{
 public:
  Student(int num, string nam, float e,float m);
  void show();
  Student * next;
 private:
  int number;
  string name;
  float English;
  float Math;
};
Student::Student(int num,string nam,float e,float m)
{
 number=num;
 name=nam;
 English=e;
 Math=m;
}
void Student::show()
{
 cout<<setiosflags(ios::left);
 cout<<setw(10)<<number;
 cout<<setw(15)<<name;
 cout<<"English:"<<setw(10)<<English;
 cout<<"Math:"<<setw(10)<<Math;
 cout<<endl;
}
int main()
{
 int i=0,n;
 cout<<" Please enter the number of students: ";
 cin>>n;
 Student * head=NULL;
 Student * node=NULL;
 Student * p=NULL;
 string name;
 float English;
 float Math;
 for(i=1;i<=n;i++)
 {
  cout<<" Please enter the first "<<i<<" Student's name, English score, math score "<<endl; 
  cin>>name>>English>>Math;
  node=new Student(i,name,English,Math);
  if(head==NULL)
   head=node;
  else
   p->next=node;
  p=node;
  if(i==n)
   p->next=NULL;
 }
 cout<<"============ Output student information =========="<<endl; 
 p=head;
 while(p!=NULL)
 {
  p->show();
  p=p->next;
 }
    //========== == free space occupied by the object =======
       p=head;
       Student * l;
       while(p!=NULL)
       {
                l=p;
                p=p->next;
                delete l;
        }
 return 0;
}

Operation results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130906202200828.png ">

Related articles: