C ++ Object-Oriented Programming-class constructor and destructor

Source: Internet
Author: User

Note that this section is the focus of c ++!


What is a constructor?

In the previous tutorial, we briefly introduced some basic content about the class. For Class Object member initialization, we always set up a member function and manually call this function to assign values to the members, in c ++, is there a more convenient way for classes to automatically initialize member variables when an object is created? This is crucial for the operation to protect members, the answer is yes. for the initialization of c ++ class members, special constructors are provided for automatic operations without manual calls, before proceeding, let's take a look at a basic definition of the constructor in c ++.

1. C ++ stipulates that each class must have a default constructor, and objects cannot be created without constructors.

2. if no constructor is provided, c ++ automatically provides a default constructor, which is a constructor without parameters, it is only responsible for creating objects without assigning values.

3. As long as any constructor is provided in the class, c ++ does not automatically provide the default constructor.

4. the definition of class objects is similar to that of variables. When using the default constructor to create an object, if a static or global object is created, the bitwise mode of the object is 0, otherwise, it will happen immediately.



Let's look at the following code:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
Using namespace std;
Class Student
{
Public:
Student () // No parameter Constructor
{
Number = 1;
Score = 100;
}
Void show ();

Protected:
Int number;
Int score;

};

Void Student: show ()
{
Cout <number <endl <score <endl;
}

Void main ()
{
Student;
A. show ();
Cin. get ();
}



Student (), which defines the same name as the class and does not return any type, is the constructor. It is a non-parameter constructor that is automatically called when an object is created, if the code in the body of the Student () function is removed, it is equivalent to the default constructor provided by c ++.

Constructor can contain any number of formal parameters, which is the same as that of common functions!

Let's take a look at how a constructor with parameters performs initial operations on objects.

The Code is as follows:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
Using namespace std;
Class Teacher
{
Public:
Teacher (char * input_name) // constructor with Parameters
{
Name = new char [10];
// Name = input_name; // The Value assignment is incorrect.
Strcpy (name, input_name );
}
Void show ();

Protected:
Char * name;

};

Void Teacher: show ()
{
Cout <name <endl;
}

Void main ()
{
// Teacher a; // The error is returned because no constructor without parameters exists.
Teacher a ("test ");
A. show ();
Cin. get ();
}


We have created a constructor with the form parameter Teacher (char * input_name) with a character pointer, you can call this method to create an object by adding the class name and object name to the extension number and parameters in the extension number. This is similar to calling a function, but it also has different meanings, because the constructor is set up to create an object, the meaning here is not simply to call a function, but to create a class object.

Once there is a constructor with parameters in the class and there is no parameter constructor, the system will not be able to create objects without parameters, so the above Code

Teacher;

It is wrong !!!

Note that:

// Name = input_name; // The Value assignment is incorrect.

Because name refers to the memory heap zone, if name = input_name; is used, it will cause the pointer to change not to the heap zone but to the stack zone, an error occurred while calling the Destructor delete to release the heap space! (The content of the Destructor will be introduced later)

If you need to call a constructor that can be executed, you need to add another constructor without parameters.

The code above is transformed as follows:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
Using namespace std;
Class Teacher
{
Public:
Teacher (char * input_name)
{
Name = new char [10];
// Name = input_name; // The Value assignment is incorrect.
Strcpy (name, input_name );
}
Teacher () // non-parameter constructor for function Overloading
{

}
Void show ();

Protected:
Char * name;

};

Void Teacher: show ()
{
Cout <name <endl;
}

Void main ()
{
Teacher test;
Teacher a ("test ");
A. show ();
Cin. get ();
}

Create a Teacher () parameter-free function with the same name that is not elaborated, and use a load method to differentiate calls, since constructors and common functions have the same overload feature, programmers can add any number of constructors to a class to use different parameters for initialization!

Now let's take a look at the situation where a class object is another type of data member. If you think it is a bit confusing, you can simply understand that the definition of Class Members can be nested with each other, a member of a class can use another class for definition declaration.


C ++ specifies that if a class object is another type of data member, the system will automatically call the class constructor when creating the object.

The following is an example.

The Code is as follows:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
Using namespace std;
Class Teacher
{
Public:
Teacher ()
{
Director = new char [10];
Strcpy (director, "Wang ");
}
Char * show ();
Protected:
Char * director;
};
Char * Teacher: show ()
{
Return director;
}
Class Student
{
Public:
Student ()
{
Number = 1;
Score = 100;
}
Void show ();

Protected:
Int number;
Int score;
Teacher teacher; // a member of this class, teacher, is created and initialized using the Teacher class.

};

Void Student: show ()
{
Cout <teacher. show () <endl <number <endl <score <endl;
}

Void main ()
{
Student;
A. show ();
Student B [5];
For (int I = 0; I <sizeof (B )/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.