C + + Primer Plus 10.3 Classes of constructors and destructors learning notes

Source: Internet
Author: User

10.3.1 declaring and defining constructors
Constructor prototypes:
Constructor prototype with some default arguments
Stock (const string &co, Long n = 0, double PR = 0.0);
Constructor definition:
Constructor definition
Stock::stock (const string & Co, long N, double PR)
{
Company = CO;
if (n < 0)
{
Std::cout << "Number of shares can ' t be negative;"
<< company << ' shares set to 0.\n ';
Shares = 0;
}
Else
Shares = N;
Share_val = PR;
Set_tot ();
}
10.3.2 using constructors
C + + provides two constructors for how objects are initialized. The first way is to explicitly call the constructor:
The stock food = the stock ("World cabbage", 250, 1.25);
Another way is to call the constructor implicitly:
Stock garment ("Furry Mason", 50, 2.5);
This format is more compact.
In general, use objects to invoke methods:
Stock1.show (); Stock1 object invokes Show () method
You cannot use an object to invoke a constructor because the object does not exist until the constructor constructs the object. So the constructor is used to create the object instead of being called by the image.
10.3.3 Default Constructors
Stock::stock ()
When a constructor is provided for a class, the programmer must provide it with a default constructor.
If a non-default constructor is provided and the default constructor is not provided, the following declaration will be faulted:
Stock Stock1; Not possible with Curent constructor
There are two ways to define a default constructor. One is to provide default values for all parameters of an existing constructor:
Stock (const string & co = "Error", int n = 0, double PR = 0.0);
Another method is to define another constructor--a constructor without parameters--through function overloading:
Stock ();
Because there can be only one default constructor, do not use both.
Once you have created a default constructor using either of these methods (no parameters or all parameters have default values), you can declare object variables without initializing them:
Stock first; Calls default constructor implicitly
Stock first = stock (); calls it explicitly
Stock *prelief = new stock; calls it implicitly
10.3.4 Destruction function
After an object is created with a constructor, the program is responsible for tracking the object until it expires. When an object expires, the program calls the destructor automatically.
Destructors complete cleanup work, so it's actually useful. Abuse, if the constructor uses new to allocate memory, the destructor uses Delete to free the memory.
Destructor Prototypes:
~stock ();
destructor Definition:
Stock::~stock ()
{
cout << "Bye," << company << "!\n";
}
When a destructor should be called is determined by the toilet, and usually should not be called in code to call the destructor.
If you create a static storage class object, its destructor will be called automatically at the end of the program.
If you are creating an automatic storage class object, its destructor is automatically called when the program executes the block of code that the object is defined in.
If the pair is created by using new, it will reside in the stack memory or free storage, and its destructor will be called automatically when you use Delete to free memory.
10.3.5 Improving the Stock class
......
Summary of 10.3.6 Constructors and destructors
......


C + + Primer Plus 10.3 Classes of constructors and destructors learning notes

Related Article

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.