0819-/member functions and const-mutable/structure and destruction/copy construction deletedefault and deep copy/static member function member variable classes in memory storage default parameters/friend classes and friend functions

Source: Internet
Author: User

class the member function and the const - mutable  

member functions

Fushu.h

#pragma once#include <iostream>class fushu{public:int x;int Y;public:fushu (); ~fushu (); void show (), inline void  showall (int x, int y);//explicit inline void   setxy (int x, int y);//compiler optimizations, default implicitly inline void Show (int x, int y);/*inline void  Sho Wall (int x,int y) {//Compound code std::cout << (this->x = x) << (this->y = y) << Std::endl;} The */};//inline function is put in principle in the header file, minus the inline identifier//inline function needs to be expanded, (VS2013 is required to be placed in the header file) void Fushu::showall (int x, int y) {std::cout << ( this->x = x) << (this->y = y) << Std::endl;}

Fushu.cpp

#include "fushu.h"//:: The preceding must be a class or namespace Fushu::fushu () {std::cout << "object is created" << Std::endl;} Fushu::~fushu () {std::cout << "object destroyed" << Std::endl;} The class calls the member function, requiring that the object of that class be explicitly called void Fushu::show () {std::cout << "show" << Std::endl;} void   fushu::setxy (int x, int y)//compiler optimization, default implicit inline {this->x = X;this->y = Y;std::cout << (this->x) << (this->y) << Std::endl;} void  fushu::show (int x, int y) {std::cout << (this->x) << (this->y) << Std::endl;}

member functions . cpp

#include <iostream> #include "fushu.h" void Stackrun () {Fushu fushu1;//object on the stack Fushu1.show ();} void Heaprun () {Fushu *pfushu = new fushu;//object on the heap pfushu->show ();p Fushu->showall (9);p Fushu->setxy (19, 29); Pfushu->show (1, 2);//internal member function overloading, function pointers, explicit parameters, delete Pfushu;} void Main () {Heaprun (); Std::cin.get ();}

Const-mutable

Constmutalbe.h

#pragma once#include <iostream>class constmutable{public:int a;int b;int c;const int d=0;//constant is a must exist initialization mutable int E ;//Limit public:void setabc (int A, int b, int c) {this->a = A;this->b = B;this->c = C;}, which is not restricted by const void Showabc () const{//function const, can be qualified to assign no value to member variable, this->a = 10;//this->c = 90;std::cout << this->a << This->b << this->c << Std::endl;} Constmutable (); ~constmutable ();};

Constmutable.cpp

#include "constmutable.h" constmutable::constmutable () {}constmutable::~constmutable () {}

Construction and destruction

The difference between a constructor and an assignment

#include <iostream>//All classes default to a constructor, destructors//constructors, overloads,//No return value, Class Myclass{public:int Num;public:myclass ()//: Num (4) Initializes the first method {//num = 10; the second way std::cout << "class create";} MyClass (int data)//constructor can overload {Std::cout << "class create by  data"; num = data;} ~myclass () {std::cout << "class Delete";}}; void Run () {//myclass MyClass1 (Ten),//myclass myclass1 = 101;//myclass *p = new MyClass (102); MyClass *p (New MyClass (102)); /P (new MyClass), Std::cout << (*p). Num << std::endl;//std::cout << myclass1.num << std:: Endl;} void Main1 () {run (); int num = 4;num = 4;int data (4);//data (5); Std::cin.get ();}

The Order of construction and destruction

#include <iostream>//system automatically gives you the constructor and destructor//contained, the first allocation, finally the release//inclusion of others, the last allocation, the first release of Class Fushu{public:fushu (); ~fushu ( );}; Fushu::fushu () {std::cout << "Fushu build" << Std::endl;} Fushu::~fushu () {std::cout << "Fushu destroy" << Std::endl;} Class Math{public:fushu Fushu1;math () {std::cout << "math Build" << Std::endl;} ~math () {std::cout << "math Destroy" << Std::endl;}; void Go () {math math1;} void Main2 () {//fushu fushu1;go (); Std::cin.get ();}

Explicit

#include <iostream> #include <array>class classobj{public:int num;public:explicit classobj (int data) { This->num = data;std::cout << "constructed" << num << Std::endl;} Classobj ()//{//std::cout << "constructed Yuan" << num << std::endl;//}~classobj () {std::cout << "destroyed" & lt;< num << Std::endl;} Protected:private:};void Main () {//C language style array, constructs an array, destroys an array classobj obj (0),//Unique constructor classobj Objx[3] = {classobj (0), Classobj (1), Classobj (2)};//C language style array construction method Classobj (*ppobja) [3] = &objx; Pointer to array classobj *pobj (new Classobj (0)), Classobj * ppobj[3];//array, each element is pointer ppobj[0] = new Classobj (0);pp obj[1] = new C Lassobj (1);pp obj[2] = new Classobj (2);//classobj *p= new Classobj[10];///delete[]p;std::cin.get ();} void main11111 () {//classobj num = 5;//assignment number, type conversion//classobj data (5);//classobj Obj;classobj obj (0);//Create object must be appropriate constructor//clas Sobj *p= new classobj;//c++ style array classobj * p = new Classobj (5); Std::array <classobj, 2 > myarray = {obj, *p};st D:: Cin.get ();} 
Copy Construction deletedefault and depth copy

Copy Construction

#include <iostream>//rules ClassA (int x, int y)//:a (x) are not generated for class Classa{private:int A;int b;public://copy constructs if the declaration is already defined , b (y) {//a = x;//b = y;} void print () {std::cout <<a << b << Std::endl;}}; void main12313 () {ClassA  Class1 (10,100);//compiler generates default constructor ClassA  Class2 (Class1) by default;// The compiler generates the default copy constructor Class1.print (); Class2.print ();//Default copy constructor//classa CLASS3 (4); Std::cin.get ();}

Delete-default

Delete can disable the default generated function, disable construction can not instantiate//disable copy construction, can implement prohibit others copy you//default default exists class Myclassa{public://myclassa () = delete;// Default Delete constructor, cannot instantiate//myclassa () = default;//default exists//myclassa (const Myclassa &) = delete;//copy constructor//myclassa (const Myclassa &) = default;//=//default Assignment function ~myclassa ();}; void main211 () {//myclassa Myclassa1;//myclassa myclassa2 (MYCLASSA1);//myclassa myclassa3 = myclassa1;//overloaded =, judging by type Myclassa A1;}

Deep copy Shallow copy

 #define _crt_secure_no_warnings#include <iostream> #include <string>    Class String{public:char *p;int length;string (int num,char *str) {//Get length, allocate memory, copy content length = num; p = new Char [Length];memset (p, 0, length);//strcpy (P, str);} String (const string & string1) {//qian//this->p = String1.p;//this->length = String1.length;//shenthis->p = New Char[string1.length];this->length = String1.length;memset (this->p, 0, this->length);//strcpy (this- >p, STRING1.P);} ~string () {delete[] p;//}};void main () {string *pstr1 = new String (Ten, "Hello"); std::cout <<pstr1->p<< std :: Endl;string *pstr2 = new String (*PSTR1);d elete pstr1;std::cout << pstr2->p << std::endl;std::cin.get () ;} void Main1 () {string str1 (ten, "Hello"), std::cout << str1.p << std::endl;string str2 (str1); Std::cout << STR2.P << std::endl;std::cin.get ();} 
static member function member variable class stores default parameters in memory

Class and Memory

#include <iostream>class myclass{public:int num;int data;int *p;const int coint;int & myint;static int shu;// Declares the static const int dashu;public:static void Go () {}void run () {}//constant, reference, constructor initialization must be overloaded, MyClass (int a, int b): Myint (a), Coint ( b) {//reference is a shared address, constant new backup mechanism std::cout << &a << "<< &b << std::endl;std::cout << &   Myint << "" << &coint << std::endl;const int *p = &coint;//address std::cout << *p << " "<< coint << std::endl;int *px = Const_cast<int *> (p);//Remove const Conversion *PX = 12;std::cout << coint &l t;< "" << *px << Std::endl;} ~myclass () {}};int Myclass::shu = 0;//Initialize const int myclass::d Ashu = 20;//constant void main ()//try to remove the const attribute {const int *px = & ( MyClass::d Ashu) std::cout << px << std::endl;int *p = const_cast<int *> (px), *p = 123;//static constant zone can be accessed and cannot be modified , Std::cout << *px << "<< *p <<" "<< MyClass::d ashu;std::cin.get ();} ClThe Mywindoww{public:int #//reference, which must be initialized in the construction,//references can be quoted today by this person, and tomorrow it references the person PUBLIC:MYWINDOWW (int data): num (data) {}};int MAINRR () {int data = 20;//reference must be initialized, repeatedly assigned, references in the class must be initialized in the constructor Mywindoww my1 (data); std::cout << my1.num;//plus Endl off output, as address, Otherwise as variable int dataa = 201;my1.num = dataa;std::cout << my1.num;//plus endl off output, as address, otherwise as variable std::cin.get (); return 0;} int Mainr () {int data2 = 11;int Data1 = 22;//reference must be initialized, repeatedly assigned, int & da = data1;std::cout << da<< std::endl;da = Data2;std::cout << da<<std::endl;std::cin.get (); return 0;} Class Mywindowww{public:const int num;public:mywindowww (int data): num (data) {}};void mainconst () {int int1 = 20; Mywindowww mywindowWW1 (INT1);//initialization, constants must be constructed when initializing//class outside once initialized, will not read memory, automatically generated from symbol table in code area, Std::cout << Mywindowww1.num << std::endl;//mywindowww1.num = 19;//MYWINDOWWW1 ": Cannot assign a value to a constant Std::cin.get ();} void main2312312 () {//class member variable//class name Variable name//stack on//class name * Pointer name =new class name//class static member static zone//member function, static function all in code area, class function is shared//myclass MYCLA SS1 (9);//int a (5);//void (MYCLASS::* p1) () = &myclass::run;//code share, all class objects shared object,//void (*P2) () = &myclass::go;//static function, no relation to object//reference is essentially a variable alias, 4 bytes , essentially a pointer to MyClass MyClass1 (9);//static const int Dashu; Static zone, modify////int a;//int &ra;std::cin.get ();}

Default parameters

#include <iostream>class goodclass{public:int num=1;//default initialized value, c++11 specific const int Data=90;//const, write less constructor public: static void Show (Goodclass good1) {std::cout << good1.num << "  " << good1.data<<std::endl;}};/ The const in/class can still be modified by default, with the C language const consistent void main () {Goodclass good1;goodclass::show (GOOD1); const int *px = & (Good1.data); Std::cout << px << std::endl;int *p = const_cast<int *> (px), *p = 123;std::cout << *px << " c1/> "<< *p <<"   "<< good1.data<<std::endl;goodclass::show (GOOD1); Std::cin.get ();}
friend class and friend function

1, why to introduce friend function???

Reduce system overhead and increase efficiency when data sharing between classes is implemented. Specifically, in order to enable member functions of other classes to access the private variables of the class directly. That is, allow the outside class or function to access the class's private and protection variables, so that two classes share the same function.

Advantages: Can improve efficiency, express simple, clear

Disadvantage: The friend function breaks the encapsulation mechanism, tries not to use the member function, unless the unavoidable situation makes the UF meta-function.

2. When to use friend function??

1) operator overloading requires the use of friends for some occasions.

2) two classes to share the data

















Copyright notice: This blog all articles are original, welcome to Exchange, welcome reprint, reprint do not tamper with the content, and indicate the source, thank you!

0819-/member functions and const-mutable/structure and destruction/copy construction deletedefault and deep copy/static member function member variable classes in memory storage default parameters/friend classes and friend functions

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.