C ++ class Revisited

Some point about classes missed.

. 1 #include <the iostream>
 2 #include <the typeinfo>
 . 3 #include < String >
 . 4  the using  namespace STD;
 . 5  
. 6  class the Person {
 . 7      // set friend, non-public access to members of the class 
. 8      Friend void Read (the istream & IS , the Person & P);
 . 9      Friend void Print (the ostream & OS, the Person & P);
 10  Private :
 . 11      String name;
 12 is      String address;
 13 is      the mutable int= COUNT 0 ; // const function to change the limit value of a breakthrough 
14  
15  public :
 16      static  String T;
 . 17      the Person () = default ;
 18 is      the Person ( String the _name, String _address) {
 . 19          name = the _name;
 20 is          address = _address;
 21 is      }
 22 is      String getName () const {
 23 is          ++ COUNT; // the mutable variable value 
24          T = " Blue"; //static 也可变
25         return this->name;
26     }
27     string getAddress() const {
28         return this->address;
29     }
30     int getCount() const {
31         return this->count;
32     }
33 };
34 
35 string Person::t = "yellow";
36 
37 //read the obj
38 void read(istream &is, Person &p)
39 {
40     is >> p.name >> p.address;
41 }
42 
43 //print the obj
44 void print(ostream &os, Person &p) 
45 {
46     os << p.name << " " << p.address;
47 }
48 
49 int main()
50 {
51     Person yoci("nan", "Shan'an-Xi");
52     cout << yoci.getName() << endl;
53     cout << yoci.getAddress() << endl;
54     cout << yoci.getCount() << endl;
55     cout << Person::t << endl;
56 
57     return 0;
58 }

to sum up:

1. The friend function and friend class : the class declared inside a friend (in front of the function / class can add friend), the friend has access to all members of the non-public members, including;

2. the mutable keyword, break boundaries. Statement mutable variable that has been in a state can be changed, even in the const function, not according to the error;

3. The default constructor generated ClassName () = default ; generating a default constructor for the class;

4. Delete Function: FUNC () = Delete ; prohibit implementation of the function;

5. static members :

(1) static data member: static statement, instead of belonging to an object class, all objects can share the variable scope operator ::  object. Member function three ways to access.

(2) static member function: does not contain this pointer can not be declared as const, you can not access non-static members (need this pointer, but it does not)

6. static data members and members of the general differences :

(1) Who does not exclusively belong to everyone (all objects);

(2) outside the class initialization;

(3) can be used as the default parameters

(4) belongs to the class type may be

(5) can still be modified inside const function (such as in the addition of the same mutable)

(6) access: You can not use this-> to access.

 

References:

【1】https://www.cnblogs.com/qionglouyuyu/p/4620401.html

Guess you like

Origin www.cnblogs.com/yocichen/p/11110628.html