C + +: The default constructor

Source: Internet
Author: User
Tags function definition gety


Note: If the user does not have a constructor defined in the class, the system automatically provides a default constructor that is empty for the function body.
However, as long as a constructor is defined in the class (not necessarily a parameterless constructor), the system will no longer provide it
The default constructor. Because the default constructor is overridden by the provided constructor in the class. However
Once the object is established, it needs to be initialized, so the user sets the default in the program at this time according to their own needs.
constructor function .....
Default constructor with no parameters

//Example 3.10#include <iostream>using namespacestd;classlocation{ Public:    //Location ()//parameterless (default) constructors set by the programmer himself//   {    //     //   } Location ()//parameterless (default) constructors set by the programmer himself{X=0; Y=0; } location (intMintN) {X=m; Y=N; }        voidInitintINITX,intinity) {X=Initx; Y=inity; }       intGetX () {returnX; }       intGetY () {returnY; } Private:       intX; intY; };intMain () {location A;//once the object is established, the parameterless (default) constructor is invoked to initialize theA.init (785,980); cout<<a.getx () <<","<<a.gety () <<Endl;return 0;}//Example 3.11 Example of a timer#include<iostream>using namespacestd;classtimer{ Public: Timer ()//Parameterless constructor, give seconds clear 0{seconds=0; } timer (Char*T)//constructor with a number string argument char *t = "a "{seconds= Atoi (t);//Atio Effect: Converting a string to a growth integer} timer (intT//constructor with an integer parameter{seconds=T; } timer (intMinintSec//constructors with two integer parameters{seconds= Min * -+sec; }       intgettime () {returnseconds; } Private:       intseconds; }; intMain () {timer A; //Create a class timer object A, call the parameterless constructorTimer B (Ten);//Create object B of the class timer, call the constructor that contains an integral type parameterTimer C (" -");//Create object C of Class timer, call constructor with a number string argumentTimer d (1,Ten);//Create class Timer object D, call the constructor that contains the parameters of two integral typescout<<"seconds1="<<a.gettime () <<Endl; cout<<"seconds2="<<b.gettime () <<Endl; cout<<"seconds3="<<c.gettime () <<Endl; cout<<"seconds4="<<d.gettime () <<Endl;return 0; }
Constructors with default parameters
#include<iostream>#include<cmath>using namespacestd;classcomplex{ Public: Complex (DoubleR=0.0,DoubleI=0.0);//Specify default parameter values when declaring constructors, initialization complete       DoubleAbscomplex ();Private:       DoubleReal; DoubleImag; }; Complex::complex (DoubleRDoubleI//when a constructor is defined outside the class{real = R;imag = i;}//you can no longer specify default values for parametersDoubleComplex::abscomplex () {DoubleT; T= real*real+imag*Imag; returnsqrt (t);} intMain () {Complex S1; //defines the object S1 of the complex class, without passing arguments, all with default valuescout<<"the absolute value of the complex number 1 is:"<<s1.abscomplex () <<Endl; Complex S2 (1.1);//defines the object s2 of the complex class, passing only one argumentcout<<"the absolute value of the complex number 2 is:"<<s2.abscomplex () <<Endl; Complex S3 (1.1,2.2);//defines an object s3 for the complex class, passing two argumentscout<<"the absolute value of the complex number 3 is:"<<s3.abscomplex () <<Endl; return 0;}

Analysis: In class complex, the two parameters of the construction complex all contain the default parameter value 0.0, so in the definition object
The default value can be used as needed, 3 objects S1, S2, and S3 are defined in main function main , which are
a legitimate object. Because of the different number of arguments passed, their private members real and imag get different
values. Because no arguments are passed when defining the object S1, both real and imag get the default value of the function to assign it
therefore, both real and imag are 0.0, and because when you define an object s2, only one parameter is passed, which is passed to the construction
the first parameter of the function, and the second parameter goes to the default value, so the real acquisition value of the Object S2 is 1.1,imag value
obtained 0.0; When the object S3 is defined, two parameters are passed, respectively, and the two arguments are assigned to real and imag to obtain
1.1, 2.2.

running Results The absolute value of complex 1 is: 0
run result the absolute value of complex 2 is: 1.1
run result the absolute value of complex 3 is: 2.45967

Expand the knowledge point:
1. If the constructor is set outside the declaration of the class, then the default parameter should be specified when the constructor prototype is declared within the class,
and cannot be specified when the function definition is outside the class.


2. If you specify a default value for all parameters of the constructor, you can specify one or several arguments when you define the object .
It is also possible to give no arguments, at which point the constructor is also the default constructor

For example: Comolex (double r=0.0, double i=0.0);

because there can be only one default constructor in a class, you cannot declare a non-argument at the same time.
The constructor functions are as follows:
Complex ();

Cause: If you create an object Complex S1;
The compilation system will not recognize that one of the above constructors should be called, resulting in a two semantic


3. After you define a constructor that is all default parameters in a class, you cannot define an overloaded constructor.

For example:
There is a declaration of the following constructors in a class:
Complex (double r=0.0, double i=0.0); Constructors that declare all the default arguments
Complex (double R);

Reason: If an object is defined Complex S2 (1.1);
The compilation system will not be able to determine which constructor should be called

C + +: The default constructor

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.