C + + -- classes and objects

Class declaration

Class is the keyword that defines the class, ClassName is the class name, and {} is the body of the class. Note that the semicolon is followed at the end of class definition.

class ClassName{
	//The body of a class is composed of member functions and member variables

Class members include:

Member variableMember function
Properties of classClass method

Use class description:
① : class can also be used to describe object types. The syntax is almost the same as that of struct.
② : has access attributes: public - public attribute, protected - protected attribute, private - private attribute.
③ : because the members in the class declared by struct are public by default, the members in the class declared by class are private by default. Therefore, in C + +, class is generally used to declare classes instead of struct.

Member function

class A{
	A()						//non-parameter constructor 
		printf("construct !!!!!!!!!!!!\n");
	A(int data)				//Parameterized constructor
		a = data;
	~A()					//Destructor

	void show()				//Member function
	void setdata(int data)	//Member function
		a = data;
	int getdata(void);		//Define member functions outside the class
	int a;					//Data member

int A::getdata(void)		//Define member functions outside the class
	return a;

int main()
	A x(100);//Call parameterized constructor
//	x.a = 100;
//	x.setdata(100);


  • The function of constructor is to initialize the object, which is inside the class;
  • Constructor is a special function with the same function name and class name. It has no return value type and can be overloaded;
  • The constructor will be called automatically once when the class constructs the object, and only once in the life cycle of the object.
  • Constructor must be a public attribute;
  • If the class does not have a constructor, the compiler will automatically generate a constructor at compile time, which does nothing;
  • Class should provide a constructor.

copy constructor

  1. Concept: copy constructor is a special constructor, which will be called when using an existing object to create another new object of the same type.
  2. Usage:
    ① : use existing objects to construct new objects, for example: a; A b = a; // Copy structure;
    ② : pass an object to a formal parameter of the same type;
    ③ : take an object as the return value of a function.
  3. Need to construct: when there is independent memory inside the object, you need to customize the copy, such as array, pointer, etc.
  4. be careful:
    ① : copy structure is to copy the data of the object word by word;
    ② : different objects cannot use the same piece of memory, so you need to customize the copy function;
    ③ Default copy of system structure:; Reallocate space for independent memory. The content in the copy space is called deep copy.
#include <stdio.h>
#include <string.h>
class A{
		p = new char[10];

	A(const A &x)
		printf("A (const A &x)\n");
		p = new char[10];
		delete [] p;
	char *p;


int main()
	A x;
	A y = x;

this pointer

  1. Concept:
    this pointer in the member function and constructor inside the class represents the object calling the member function or the object being constructed.
  2. usage method:
    ① You can use this pointer in member functions and constructors to distinguish between function parameters with duplicate names and member variables
    ② Can be used as parameters and return values of member functions
    ③ this pointer refers to the object, which represents the first address of the object.


  • Destructor is a special function. The function name is the same as the class name, but it should be preceded by a ~. The parameter list is empty, can not be overloaded, and has no return value;
  • The destructor will be automatically called once when the object is destroyed and released;
  • If there is no destructor in the class, the compiler will automatically generate a destructor that does nothing.

Usage: if you need to release resources when destroying objects, such as dynamic memory, apply for dynamic memory (new) in the constructor and use (delete) in the destructor.

Constant data member, constant member function, constant object

C + + recommends const instead of #define and mutable. Const data members are constants only during the lifetime of an object, but they are variable for the whole class (static exception) constant data members (assignment by constructor initialization table)

class A{
	mutable int x;			//Modifier that allows the const function to modify
    const int num;		     //const member variable
    A(int n):num(n){....;}   //Initialization parameter list
    void show(){.....}		 //Ordinary member function
    void show()const{.....}  //const member function     

A a;
const A b;//const object

① : const function can be overloaded with ordinary functions with the same name. Non const objects call non const function first. If there is no non const function, call const function again;
② : const objects can only call const functions, not non const functions
③ : const member function can only read member variables and cannot modify member variables. If you must modify them, you need to add mutable modification when declaring member variables.

Static member

  1. Concept:
    It is divided into static member variables and static member functions.
    ① : static member variable: add static when declaring. It must be initialized and must be initialized outside the class. The default initialization is 0, and the class type calls the default constructor.
    ② : static member function: add static when declaring. Static member functions can only access static members, not non static members.
  2. Syntax:
class A{
	int x;
    static int num;		//Static member variable 
    static void show(){	//Static member function
       					//Only static members can be accessed
    	//x = 100; 		// error   					

int A::num = 100;		//Out of class initialization

int main()
	cout<<A::num<<endl;	//Static members belong to classes, and all objects share the same piece of memory
	A::show;			//Call by class name, but call by object without error
	return 0;

  1. be careful:
    Static members do not need to be accessed through objects, but can be accessed directly through class names. Static members belong to classes, not to an object. There is no this pointer in a static function. The reason why static members have the above properties is that they are stored in separate memory.

Friend (destroy package)

① : the function of friends is to let the data outside the class break through the setting of access permission, and friends can access any data in the class;
② : you can declare a class / function as a friend of a class, and these classes and functions can access the data in the class.

Friend class: if class A is declared as a friend of class B, the data in class B can be queried in class A, which is not restricted by access attributes.

Friend function: a friend function is a global function, which can be declared as a friend within a class, so that the global function can access the data in the class.

friend member function

	//Friend function: declare inside the class:
    friend Function declaration;

	//Friend class: declare inside the class:
    friend class Class name;

be careful:
Friends are not restricted by access attributes. They can access all data in the class outside the class, which destroys the accumulated encapsulation attributes. Do not use them unless necessary.

Keywords: C++ Back-end

Added by supinum on Sun, 06 Feb 2022 23:16:46 +0200