Object-oriented): An abstract model based on the concept of object (entity) to simulate the analysis, design, and implementation of software in the objective world.
Class ):Abstract set with the same attributes and operations.
Class member:
The class includes member variables and member functions. It has three attributes: private, public, and protected ).
PRIVATE: It can only be accessed by functions in the class and accessed by friends functions;
Cannot be accessed by any other user, nor can the object of this class be accessed.
Public: it can be accessed by functions of the class, sub-classes, and friends functions, or by objects of the class.
Protected: it can be accessed by functions of the class, sub-class functions, and friend functions.
Cannot be accessed by objects of this class.
Class creation and initialization:
When creating a class, you must declare and define the class.
Constructors ):Is a special member function. It is mainly used to initialize objects when creating objects.
The unique syntax limit of the constructor is that the return type cannot be specified. The constructor name has the same name as the class name.
Constructor includes default constructor and copy constructor.
Default constructor ):When the class does not provide an Explicit initialization value, it is used to create the object's constructor. By default, the constructor has no parameters.
Only when no constructor is defined does the compiler provide the default constructor. After the constructor (with parameters) is defined for the class, you must provide it with an explicit default constructor.
Provide default values for all parameters of an existing constructor.
1 # ifndef _NUMBER_H_ 2 # define _NUMBER_H_ 3 4 class Number 5 { 6 public: 7 Number(int a = 0, double b = 0.0); 8 // Number(int , double ); 9 // Number();10 protected:11 private:12 int number_a;13 double number_b;14 };15 16 # endif // _NUMBER_H_
Number. h
1 # include "number.h" 2 3 Number::Number(int a, double b) 4 { 5 number_a = a; 6 number_b = b; 7 } 8 9 //Number::Number()10 //{11 // number_a = 0;12 // number_b = 0.0;13 //}
Number. cpp
1 # include <iostream>2 # include "number.h"3 4 int main()5 {6 Number a(1, 1.1);7 Number b;8 return 0;9 }
Main. cpp
Result:
0 errors, 0 warnings
You can use overload to define another constructor.
1 # ifndef _NUMBER_H_ 2 # define _NUMBER_H_ 3 4 class Number 5 { 6 public: 7 // Number(int a = 0, double b = 0.0); 8 Number(int , double ); 9 Number();10 protected:11 private:12 int number_a;13 double number_b;14 };15 16 # endif // _NUMBER_H_
Number. h
1 # include "number.h" 2 3 Number::Number(int a, double b) 4 { 5 number_a = a; 6 number_b = b; 7 } 8 9 Number::Number()10 {11 number_a = 0;12 number_b = 0.0;13 }
Number. cpp
1 # include <iostream>2 # include "number.h"3 4 int main()5 {6 Number a(1, 1.1);7 Number b;8 return 0;9 }
Main. cpp
Result:
0 errors, 0 warnings
Copy constructor ):Is a special constructor called by the compiler to construct and initialize Other Objects Based on the same class.
The copy constructor is implemented by common constructor and value assignment operator.
If no copy constructor is explicitly declared in the class, the compiler will automatically generate a bitwise copy for non-static members between objects. This implicit copy constructor simply associates all class members.
In C ++, the following three objects need to call the copy constructor:
- An object is used as a function parameter and passed in the function body as a value.
- An object acts as the return value of a function and is returned from the function by passing values.
- An object is used to initialize another object.
1 # ifndef _NUMBER_H_ 2 # define _NUMBER_H_ 3 4 class Number 5 { 6 public: 7 // Number(int a = 0, double b = 0.0); 8 Number(int , double ); 9 Number();10 Number(const Number &);11 int getNumber_a();12 double getNumber_b();13 protected:14 private:15 int number_a;16 double number_b;17 };18 19 # endif // _NUMBER_H_
Number. h
1 # include "number.h" 2 3 Number::Number(int a, double b) 4 { 5 number_a = a; 6 number_b = b; 7 } 8 9 Number::Number()10 {11 number_a = 0;12 number_b = 0.0;13 }14 15 Number::Number(const Number & num)16 {17 number_a = num.number_a;18 number_b = num.number_b;19 }20 21 int Number::getNumber_a()22 {23 return number_a;24 }25 26 double Number::getNumber_b()27 {28 return number_b;29 }
Number. cpp
1 # include <iostream> 2 # include "number.h" 3 using namespace std; 4 5 int main() 6 { 7 Number a(1, 1.1); 8 Number b(a); 9 cout << a.getNumber_a() << " , "10 << a.getNumber_b() << endl;11 cout << b.getNumber_a() << " , "12 << b.getNumber_b() << endl;13 return 0;14 }
Main. cpp
Result:
1, 1.1
1, 1.1
Destructor ):It is also a special member function. Its function is opposite to the constructor. when an object is out of its scope (for example, the function of the object has been called), the system automatically executes the destructor.
The Destructor name should also be the same as the class name, but add a backslash before the function name "~ To distinguish it from the constructor.