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++