Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the default member function of a class in C++

Shulou Source: shulou.com Published: 2022-06-01 22:56:01 10月05日 Update

This article mainly introduces the C++ class in the default member function how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

In C++, for any class, we will be provided with four default member functions (if we do not show to declare)-constructor, destructor, copy constructor, assignment function. These functions are called automatically under certain circumstances, but they do not mean that they can perform specific functions or tasks as users expect. More often, we need to implement the functions of these functions ourselves.

A (); / / default constructor ~ A (); / / destructor A (const A &); / / default copy function A & operator = (const A & a); / / default assignment function 1, constructor

The constructor is a special member function with the same name as the class, which is automatically called by the compiler when the object is created through the class, ensuring that each data member has an appropriate initial value and is called only once in the object's life cycle. The function of the constructor is implemented by the implementer of the class, and the function body and function parameters are designed according to the actual situation. The constructor must have one or can have more than one.

Both the class Person {public: / / no-parameter constructor and the full default constructor are called default constructors, and there can only be one default constructor. / / Note: no-parameter constructors, all default constructors, and constructors that we did not write default generated by the compiler can all be considered default member functions. Person (string name = "classmate Feng", int age = 18) {_ name = name; _ age = age;} / / print message void Print () const {cout

Tags: Functions copies objects compilers compilers members classmates types builds Clippers memory programs names ages functions parameters features articles systems operators Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple Shulou Technology Xiaomi Microsoft