C++ constructor, destructor, copy constructor

1. Definition

    Constructor: is a function defined in a class, and its role is to be called automatically when you create an object of this class.

                    If you define your own, call your own, otherwise call the default one.

    Destructor: is a function defined in the class, its role is to be called when your class object dies to delete the class pair

                    If you define it yourself, call your own, otherwise call the default one.

    Before understanding the copy constructor, let's understand deep copy and shallow copy:

         If a class owns resources, and when objects of this class are copied, resources are allocated to new objects, then we say

         It is called a deep copy, otherwise it is called a shallow copy.

    Copy constructor: When you do not define a copy constructor, when your class object is copied, the default call is provided by C++

                            The copy constructor is a shallow copy; if you allocate the required

                            Resources are deep copies.

2. Use

    Here I use a class to describe the constructor, destructor and copy constructor.

    The copy constructor is called when the class object is copied.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


using namespace std;


class A
{
	public :
		A(const char *p)
		{
			str=new char[10];
			strcpy(str,p);
			printf("This is the constructor of A\n");
		}
		A(const A &c)
		{
			str = new char[10];
			if(str!=0)
			strcpy (c.str, str);
			printf("This is the copy constructor of A\n");	
		}		
		~A()
		{
			delete str;
			printf("This is the destructor of A\n");
		}
	public :
		    char *str;	
};


class b:public A
{
	public :
		b(const char *s,const char *p):A(p)//Pay attention to the writing of the subclass constructor when both the parent class and subclass constructors contain parameters
		{
			string = new char[10];
			strcpy(string,s);
			str=new char[10];
			strcpy(str,p);
			printf("This is the constructor of b\n");
		}
		b(const b &c):A(c)//! Note how the subclass copy constructor is written
		{	
			this->string = new char[10];
			strcpy(c.string,string);
			this->str = new char[10];
			strcpy (c.str, str);
			printf("This is the copy constructor of b\n");
		}
		~b()
		{
			delete string;
			printf("This is the destructor of b\n");
		}
	public :
		char *string;	
};
intmain()
{
	A a("lucifer");
	printf("----------------------\n");
	b c("lucifer","devil");
	printf("----------------------\n");
	A d = a;
	printf("----------------------\n");		
}	


    Here we see that the constructor of the parent class is called first, and then the constructor of the child class is called. When destructing, the subclass's method is called first

    The destructor then calls the destructor of the parent class.

Then you have to pay attention to the initialization list of the constructor     when both the constructor and copy constructor of the parent class and the child class have parameters ,

    There is also a copy constructor.

    ! Welcome to point out the shortcomings


        

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326642369&siteId=291194637