Class & object

A generic class:

Class Box{// Private member private: double length; double width; // Public member public: int size; Double getV(){return length*width; } // Void setLength(double _length); void setWidth(double _width); }; Void Box::setLength(double _length){length = _length; } void Box::setWidth(double _width){ width = _width; }Copy the code

Call:

    Box box;
    box.setLength(100);
    box.setWidth(20);
Copy the code

The modifier

Common modifiers are private and public, as well as a protected member:

Add a protected member to the Box class:

    protected:
        int age;
Copy the code

Derived classes:

// Class SmallBox:Box {public: void setSmallAge(int _age); int getSmallAge(); }; void SmallBox::setSmallAge(int _age){ age = _age; } int SmallBox::getSmallAge(){ return age; }Copy the code
  • Protected members are like private members in that external objects are not accessible, but their derived classes are.

  • Derived classes can access public members of their parent class, but not private members

Class constructor

The constructor is used to create the object and assign its initial value.

The constructor of the Box class can also be understood as a member method that returns the Box type, and also needs to be defined in the class and implemented internally or externally.

// Public member public: int size; // Constructor: Box(); Box(double _length,int _size); //Box(double _length,int _age);Copy the code

Implementation:

// Constructor Box::Box(){} Box::Box(double _length,int _size){length = _length; size = _size; } Box::Box(double _length,int _age){length = _length; age = _age; }Copy the code

Use:

Box box1 (12, 14); cout << "box1:" << box1.size << endl;Copy the code

The destructor

The destructor of a class is a special member function of a class that is executed each time the object created is deleted.

The name of the destructor is exactly the same as the name of the class, prefixed with a tilde (~), and it does not return any value, nor can it take any arguments. Destructors help free resources before exiting the program, such as closing files, freeing memory, and so on.

Public: // destructor: ~Box(); // Destructor Box::~Box(){cout << "Box deleted" <<endl; }Copy the code

We print when the object is deleted to see if the destructor has been executed.

A friend function

Friend functions use the friend modifier, not just for functions, but also for classes, called friend classes.

We define a normal function outside the class:

Void printSth(Box Box){cout << "friend:" << box-length << endl; }Copy the code

Add and use the friend modifier to the Box class:

Class Box{// Private member private: double length; double width; // public member public: // friend function friend void printSth(Box Box); // Derived member protected: int age; };Copy the code

Friend functions can access private and protected members of the class.

Reference: www.cnblogs.com/balingybj/p…

Inline function

Similar to kotlin’s inline function

// Inline function inline int Max(int I,int j){return I >= j? i : j; }Copy the code

This pointer

An address that points to the class or function used to call its own members.

        void prin(){
            cout << this->length <<endl;
        }
Copy the code

Friend functions do not have a this pointer because they are not members of the class; only member functions have this Pointers.

Class pointer

A pointer to a class member that uses -> to use its class member

Box box; // class pointer to Box *ptrBOX; ptrBOX = &box; // use cout << ptrBOX->size <<endl;Copy the code

The static static

Static members

  • For static members of a class, there is only one copy of the static member, no matter how many objects there are.
  • Static members are shared across all class objects.
  • Every time you create a new class object, you need to pay attention to static member values.
    private:
        static int money;
Copy the code

Initialize outside the class:

Int Box::money = 10;Copy the code

The following shows how static members change when multiple class objects are created:

class Box { private: static int money; Static int initCount; public: void setMoney(int _money){ this->money = _money; } int getMoney(){ cout << "init:"<<initCount<<" money:"<<money <<endl; return money; } Box(){ initCount++; } static int getCount(){return initCount; }}; Output: init:1 money:1 init:2 money:1Copy the code

Static functions

Create a static function:

Static int getCount(){return initCount; }Copy the code

Static functions cannot access non-static members of a class, so the this pointer cannot be used either.

Static functions exist at compile time, so they can be called without requiring an object of the class:

Box box; // Call the method: Box::getCount() box-.getCount ()Copy the code

Reference: www.w3cschool.cn/cpp/cpp-cla…

inheritance

Create a base class:

class Base
{
    private:
        int i;
    public:
        int j;
    protected:
        int k;
};
Copy the code

An inherited class, also called a derived class:

class Activity : Base { private: public: int getJ(){ return j; } int getK(){ return k; }};Copy the code

Derived classes can access the public and protected members of the base class, but not the private members.

Error: ‘I’ (‘ I ‘);


        int getI(){
            return i;
        }
Copy the code

Multiple types of derived classes:

  • Public inheritance: When a class is derived from a public base class, the public members of the base class are also the public members of the derived class. The protected members of the base class are also the protected members of the derived class. The private members of the base class cannot be accessed directly by the derived class, but can be accessed by calling the public and protected members of the base class.
  • Protected inheritance: When a class is derived from a protected base, the public and protected members of the base become protected members of the derived class.
  • Private Inheritance (private) : When a derived class has a base class, the public and protected members of the base class become private members of the derived class.
Class test: public Base{}; class test2: protected Base{ }; class test3: private Base{ };Copy the code

Multiple inheritance

class test4 : Base,test2{

};

class test5 : public Base,private test2,protected test3{

};
Copy the code

encapsulation

Omit…

polymorphism

Different objects of the same class behave differently in different situations

#include <iostream> using namespace std; class Shape{ private: int length; int width; public: int area(){ cout << "Shape" << endl; }}; class Rectangle : public Shape{ public: int area(){ cout << "Rectangle" << endl; }}; int main(){ Shape *shape; Rectangle rec; shape = &rec; shape->area(); system("pause"); return 0; }Copy the code

Shape executes the area method of Shape and does not execute the derived method.

Since the *shape pointer is defined as shape, the area method already exists in the compiler, so the area method is executed at the time of the compiler.

Virtual functions were introduced in CPP to solve polymorphisms. The virtual keyword was changed in the base acre method:

class Shape{ private: int length; int width; public: virtual int area(){ cout << "Shape" << endl; }};Copy the code

Rectangle = Rectangle

In addition, we can set the virtual function to an empty implementation and make it a pure virtual function:

// virtual int area() = 0;Copy the code

Reference: www.w3cschool.cn/cpp/cpp-pol…

interface

CPP has no interfaces, but only abstract classes. As long as a class has a pure virtual function decorated with virtual, it is an abstract class.

CPP abstract class features:

  • Cannot be instantiated, must be instantiated by a subclass.
  • In practice, to emphasize that a class is abstract, the constructor for that class can be described as protected access control.
  • A derived class must implement a pure virtual function of the abstract class. If it does not, the derived class is an abstract class.