6 default functions for C + + fundamentals grooming--c++

Source: Internet
Author: User

C + + has six default functions:

1, default constructor;

2, the default copy constructor;

3, the default destructor;

4, assignment operator;

5, the value operator;

6, the value operator is const;

Cases:

person.h#ifndef Person_h#definePerson_h#include<iostream>#include<string>using namespacestd;classperson{ Public: Person (); //Deafault constructor function;Person (Constperson&);//Default copy constructor~person ();// Destructorsperson&operator= (ConstPerson &);//Assignment OperatorsPerson *operator& ();//value operator    ConstPerson *operator& ()Const;//value Operator Const Public:    stringGetName () {returnSName;} stringGetCode () {returnSCode;} voidSetName (stringname); voidSetcode (stringcode); voidprintinfo ();Private:    stringSName; stringSCode;};#endif //Person_h

Person.cpp#include"Person.h"Person ::P Erson () {cout<<"run: default constructor;"<<Endl;} Person::P Erson (ConstPerson &src) {cout<<"run: copy constructor;"<<Endl; SName=Src.sname; SCode=Src.scode;} Person::~Person () {cout<<"run: destructor;"<<Endl;} person&person::operator=(ConstPerson &src) {cout<<"run: assignment operator;"<<Endl; SName=Src.sname; SCode=Src.scode; return* This;} person*person::operator&() {cout<<"run: accessor operator;"<<Endl; return  This;}ConstPerson *person::operator& ()Const{cout<<"run: accessor operator const;"<<Endl; return  This;}voidPerson::setname (stringname) {SName=name;}voidPerson::setcode (stringcode) {SCode=Code;}voidPerson ::p rintinfo () {cout<<"Name:"<< SName <<Endl; cout<<"Code:"<< SCode << Endl <<Endl;}
Main.cpp#include<iostream>#include"Person.h"using namespacestd;intMain () {//Create aPerson A; A.setname ("Li Ming"); A.setcode ("0101"); //Create BPerson B (a); B.setcode ("0102"); //Create CPerson C; C=b; C.setcode ("0103"); //Create DPerson *D; D= &A; D->setcode ("0104"); //OutputA.printinfo ();    B.printinfo ();    C.printinfo (); D-Printinfo (); return 0;}

Output Result:

When you declare only an empty class and do not use it, the compiler generates it by default:

1, default constructor;  2, the default copy constructor;  3, the default destructor; 4, assignment operator;

constructor function:

The constructor is used to create the object, and when the object is created, the compilation system allocates memory space to the object and automatically calls the constructor.

The process by which the constructor runs is:

1, the system creates the memory space, calls the constructor function;

2, the initial variable table;

3, function body part of the operation;

Destructors:

The object is dropped when it is destroyed, used for the destruction of the object, freeing the memory;

Copy constructor:

6 default functions for C + + fundamentals grooming--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.