C + + preliminary core programming Part 1: classes and encapsulation
Content directory:
00 packaging significance
- Attribute and behavior as a whole to express things in life
- Write attributes and behaviors together to express things
01 access rights
Permission type | - | - | - | - |
---|---|---|---|---|
Public authority | pubilc | member | Accessible within class | Out of class accessible |
Protection Authority | protected | member | Accessible within class | Subclass accessible |
Private rights | private | member | Accessible within class | Subclass not accessible |
Summary:
- No matter what permission, it can be accessed within the class
- Public, protected and private permissions tend to increase in permission levels
- Protected and private rights content is not accessible outside the class
02 difference between struct and class
struct and class are basically the same, but the default permissions are different
The default permission of struct is public
class the default permission is private
03 initial design case
-
Design cube class
class Cube { public: void Init(int length, int width, int hight) { m_length = length; m_width = width; m_hight = hight; } long long Volume()//Volume of cube { long long volume = m_length * m_width * m_hight; return volume; } long long Perimeter()//Perimeter of cube { long long perimeter = (m_length + m_width + m_hight) * 4; return perimeter; } private: int m_length, m_width, m_hight; };
-
Design a relationship class between point and circle
class PointAndCircle { public: void Init(int point_x, int point_y, int c_x, int c_y, int c_r) { this->point_x = point_x;//Here, the formal parameter name is the same as the variable name in the class, which needs to be distinguished by this pointer this->point_y = point_y; this->c_x = c_x; this->c_y = c_y; this->c_r = c_r; } void Distance() { double temp = pow(point_x - c_x, 2) + (point_y - c_y, 2); dist = sqrt(temp); } void Check() { Distance(); if (dist > c_r) cout << "The point is outside the circle" << '\n'; else if (dist == c_r) cout << "Point on circle" << '\n'; else cout << "Point in circle" << '\n'; } private: int point_x, point_y, c_x, c_y, c_r; double dist; };
04 construction and destructor
1. Constructor
Constructor: a constructor is a special function used to initialize an object's member variables when an object is instantiated
-
Characteristics of constructors
- The form is: class name () {}
- Constructor: no return value and no void is written
- The function name is the same as the class name, (and 1.) confirm
- Constructors can have arguments, so overloading can occur
- When the program calls the object, it will automatically call the construct without manual operation
-
Classification of constructors
Constructors can be divided into parametric constructors and nonparametric (default) constructors
-
copy constructor
Features of copy function:
Copy constructor is a special constructor. The name of the function must be consistent with the class name. One of its parameters must be a reference variable of this type.
Shape: cube (const Cube & A)
Call timing of copy function:
- Use an object that has been created
- Pass values to parameters by value
- Value returns a local object
About deep and shallow copies:
A brief explanation: shallow copy will lead to the repeated release of heap memory, which needs to be solved by deep copy (you need to construct your own copy constructor). Therefore, if members have heap, you must provide their own copy constructor, otherwise when copying, the address will be copied. The two members share one address and one party will change it, The other party will follow, and after one party releases its own memory, the other party can no longer access it.
-
Call to constructor
Take the Cube class as an example
-
bracketing
1. Default (parameterless) constructor: Cube c1;
2. Parameterized constructor: different parameters and different calls, for example:The constructor is: Cube(int a, int b) {}, which is called in the form of Cube c2(10, 20)
3 copy constructor: Cube c3(c2); -
Explicit method
Cube c2 = Cube(10, 20); Where cube (10, 20) is an anonymous object
-
Implicit transformation method
Parametric structure: Cube c4 = {10, 20}// The old c + + Standard cannot do this
Copy structure: Cube c5 = c4;
-
2. Destructor
Destructor: the function of destructor (method) is just opposite to that of constructor. It is the last method automatically called by the object before the object is destroyed
-
The form is: ~ Cube() {}
-
Destructor: no return value, no void written
-
The name of the destructor is the same as the class name, preceded by~
-
Destructor has no parameters and cannot be overloaded
-
The program will automatically call destructor before the object is destroyed without manual operation
05 initialization list
Method 1:
class Cube { public: Cube(int a, int b, int c) { m_a = a; m_b = b; m_c = c; } int m_a, m_b, m_c; };
Method 2:
class Cube { public: int m_a, m_b, m_c; Cube() : m_a(10), m_b(20), m_c(30){} };
Method 3:
class Cube { public: int m_a, m_b, m_c; Cube (int a, int b, int c) : m_a(a), m_b(b), m_c(c){} };
06 class objects as class members
class A {}; class B { A a; }
When other class objects are members of this class, they are constructed first and then themselves. The order of deconstruction is opposite to that of construction
07 static members and functions
-
Characteristics of static members and functions
- Non static variables are not accessible
- All functions share
- Have public and private permissions
-
There are two access methods: static member and function
1). Access through objects: Cube c; c.func();
2). Access through class name: Cube::func();
Only non static members belong to the object of the class, and static member functions do not belong to the class
be careful:
-
The size of the empty class is 1
class Cube { }; //Cube c; sizeof(c) is 1
-
Once the class is not empty, the size is the memory occupied by non static variables (variables are all of the same type, regardless of memory alignment)
class Cube { int m_a, m_b, m_c; }; //Cube c; sizeof(c) is 12, when int is 4 bytes
-
Memory alignment is different. Please see the details memory alignment
When there are many different types of variables in the class, there is a memory alignment operation to make
class Cube { int m_a, m_b, m_c; double m_x; }; Cube c;
sizeof © ≠ {\neq} = 20, while sizeof © KaTeX parse error: Undefined control sequence: \eq at position 2: {\ ̲ e ̲ q ̲} twenty-four
08 this pointer and null pointer call member function
-
Introduction to this pointer
The member function in the class has a built-in this pointer, which means this class
this->m_a. That is, the member m in the class_ a. This - > func() is the member function func in the class
-
The function of this pointer
-
Resolve name conflicts
For example:
class Cube { public: Cube(int m_a)//Constructor { m_a = m_a; } int m_a, m_b; };
Write like this, m in the class_ The value of a does not change, these two m_a is the same local variable. It needs to be written as follows:
class Cube { public: Cube(int m_a)//Constructor { this->m_a = m_a; } int m_a, m_b; };
This will make a good distinction.
-
Return the object itself (chain programming idea): the return value is a reference, and the specific content, The this pointer returns a reference
class Cube { public: Cube(int m_a)//Constructor { this->m_a = m_a; } Cube& set_m_a(int a) { m_a = a; return *this; } int m_a, m_b; };
-
-
Null pointer access member function
The null pointer does not allocate memory, so the null pointer cannot access variables in the class. It can only access some member functions, which need to meet the condition of not accessing members in the class.
class Cube { int m_a, m_b; public: void showName() { cout << "zhangsan" << '\n'; } }; Cube *c = NULL; c->showName();//Can run
09 constant functions and constant objects
1. Constant function
Member functions modified with const are called constant functions
In a constant function, the member property cannot be modified. If it must be modified, the member property can be modified after adding the keyword mutable when declaring it
void showName() const//The value pointed to by this cannot be modified { }
Use mutable int m_a; The modification can be completed in the constant function
2. Common objects
Objects decorated with const before a class are called constant objects
const Cube c;
In a constant object, no matter in an in class function or an out of class function, all variables cannot be modified, and variables cannot be accessed in an in class function, but only outside the class
10 friends
Three ways of friends
-
Global function as friend
The global function is written outside the class and cannot access the private part of the class. You need to write the function declaration into the class, which is decorated with friend
#include <iostream> #include <cstdlib> using namespace std; class A { int m_a; public: A(int a) : m_a(a) {}; friend void func(A *a_point); }; void func(A *a_point)//Global function as friend { cout << a_point->m_a << '\n'; } int main() { A * a_point = new A(10); func(a_point); return 0; }
The global function is a friend to realize the access of the global function to all attribute members in the class.
-
Class as friend
#include <iostream> #include <cstdlib> using namespace std; class B; class A { public: void func(); B *b_point; }; class B { public: friend class A;//Class as friend B()//Constructor of class B { name = "zhangsan"; num = 18; } private: string name; int num; }; void A::func() { b_point = new B; cout << b_point->name << ' ' << b_point->num << '\n'; } int main() { A a; a.func(); return 0; }
Class as a friend implements access to all attribute members of another class.
-
Member function as friend
#include <iostream> #include <cstdlib> using namespace std; class B; class A { public: void func(); B *b_point; }; class B { public: friend void A::func();//Member function as friend B()//Constructor of class B { name = "zhangsan"; num = 18; sex = 'M'; } string name; int num; private: char sex; }; void A::func() { b_point = new B; cout << b_point->name << ' ' << b_point->sex << '\n'; } int main() { A a; a.func(); return 0; }
The member function of a class acts as a friend to access all attribute members in another class.
Summary: the friend is written in the accessed class, and once the friend is established, all attribute members in the class can be accessed
The End...