Constructors for C + +

Source: Internet
Author: User


What I just learned: C + + constructors.
has been in confusion until then, now afraid to forget. So hurry to write him down, used to review later, there may be mistakes in the place, please Daniel pointed out.
C + + knowledge is more abstract than C, but it does understand, that's the same thing. This is what seniors say, and I just learned the constructor, still dizzy somewhere else.
C + + 's constructors are divided into 3 types:
One. Constructors without parameters
Class car
{
Public
? Car ()
? {
?? cout << "123" << Endl;
?}
? ~car ()
? {
?? cout << "123" << Endl;
?}
};
int main ()
{
? car A;
? return 0;
}//results will output two 123
Two. Constructor with parameters
Class car
{
int a;//in C + + if the data type is not indicated, the system defaults to private type
int b;
Public
Car (int a,int b)//defines a constructor for a parameter
{
cout << a << b << endl;
}
~car ()
{
cout << "123" << Endl;
}

};
int main ()
{
Car A (from);
return 0;
}//results will output two 123
Three. Delegate constructors
Class Clock
{
Public
Clock (int A, int b, int c);
void timegive (int newh, int newm, int newg);
void Timeshow ();
Clock (): Clock (0, 0, 0) {}//c++) in order to avoid multiple occurrences of the constructor, and to easily modify the value of the constructor, the delegate constructor
Private
int hour, minute, second;
};
Clock::clock (int A, int b, int c)//Initializes the object of the class//
{
hour = A;
minute = b;
second = C;
}
void clock::timegive (int newh, int newm, int newg)
{
hour = NEWH;
minute = NEWM;
second = NEWG;
}
void Clock::timeshow ()
{
cout << Hour << ":" << minute << ":" << second;
}
int main ()
{
Clock C1 (0, 0, 0);
Clock C2;
C1.timeshow ();
C2.timeshow ();
}
Four. Copy constructor

The copy function of the constructor function
Class car
{
Public
Car (); constructor function
Car (const car &s); & Reference S copy constructor begins by defining a reference object to copy the data member values from the object car.
~car (); Destructors
void print ();
Private
int x;
int y;//data member PTR points to the assigned string
};
Car::car ()
{
int x = 10;
int y = 12;
}
Car::car (const car &s)
{
x = s.x;
y = s.y;
cout << "completed copy" << Endl;
}
void car::p rint ()
{
cout << x << ":" <<y<<endl;
}
int main ()
{
Car lol;
Car qwe (LOL);
return 0;
}
A const-decorated data type is a constant type, and the value of a variable or object of a constant type cannot be updated. When we're learning to quote, we'll find that the references are both changing.

Just study, please give me more advice

Constructors for C + +

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.