C++ learns like as a member function parameter

foreword

Classes as parameters are an important concept when it comes to object-oriented programming. In the code example you provided, we can see a simple cube class (Cube), and in the isSame method, it accepts another cube object as a parameter. Below I will explain this knowledge point step by step.

text

  1. Define the cube class (Cube)
class Cube
{
public:
    void set_length(int len)
    {
        length = len;
    }

    int get_length()
    {
        return length;
    }

    void isSame(Cube& a)
    {
        if (length == a.get_length())
        {
            cout << " 两个立方体是一样的 " << endl;
        }
        else {
            cout << " 两个立方体不是一样的 " << endl;
        }
    }

private:
    int length;
};

In this code snippet, we define a class called Cube. This class represents a cube and has the following members:

set_length(int len): Set the side length of the cube.
get_length(): Get the side length of the cube.
isSame(Cube& a): Compare whether the side lengths of the current cube object and another cube object a are equal, and output the result.

  1. Create a cube object and use the method
int main()
{
    Cube a, b, c;
    a.set_length(10);
    b.set_length(10);
    c.set_length(11);
    a.isSame(b);
    a.isSame(c);
}

In the main function, we create three cube objects a, b and c. Then, we set the side length of the cube using the set_length method. Note that we set the same side length (10) for a and b, while c has side length 11.

Next, we compare the side lengths of the cube objects by calling the isSame method. This method accepts a parameter of type Cube, so we can pass other cube objects to it. The isSame method outputs different messages depending on whether the side lengths are equal.

To sum up, the concept of classes as parameters allows us to pass objects of one class to methods of another class to perform various operations and comparisons. In your example, this concept is used to compare whether different cube objects have equal side lengths.

understand deeper

Explain the class member function isSame

When we define the class member function isSame in object-oriented programming, we are actually adding a method to the class to compare whether two objects are equal. In the example, isSame is a member function that compares whether two cube objects have the same side length. Let me explain in detail what this function does and its implementation.

void isSame(Cube& a)
{
    if (length == a.get_length())
    {
        cout << " 两个立方体是一样的 " << endl;
    }
    else {
        cout << " 两个立方体不是一样的 " << endl;
    }
}

The isSame function is defined as a public member function in the Cube class. It accepts a reference parameter a of Cube type, which is used to compare whether the side lengths of the current cube object and the passed cube object are equal.

The implementation of the function is based on the following logic:

Use the length member variable of the current cube object to compare with the side length of the passed cube object a.
If the two sides are equal, output "the two cubes are the same".
If the two side lengths are not equal, output "The two cubes are not the same".
This design allows us to easily compare the properties of two cube objects by calling the isSame function without directly accessing the private member variables of the objects. Instead, we perform comparison operations by calling public functions, consistent with the concept of encapsulation.

In this way, we can organize, encapsulate and manage operations and comparisons between objects through class methods. This improves the maintainability and extensibility of the code, because the implementation details of the class are encapsulated inside the class, and the client code only needs to call the method of the class to complete the required operation.

Guess you like

Origin blog.csdn.net/wniuniu_/article/details/132603879