[C++] Basic usage of class

Original link: https://grimoire.cn/cpp/cpp-class.html

75

Tips: The class-related content of C++ is similar to the class of java, you can refer to it for learning

As an object-oriented language, C++ is of course the concept of classes and objects, so how should we create a class?

In C++, we can create a class through the class keyword, and its basic structure is as follows:

Class and Object Basics

 class className { // 类名Access specifiers: // 访问修饰符Date members/variables; // 变量Member functions() {} // 方法}; // 以分好结束一个类

According to the above structure, we can try to write a class as follows:

 class Box { public: double m_length; // 长度double m_breadth; // 宽度double m_height; // 高度// 成员函数声明void setLength(double length); void setBreadth(double breadth); void setHeight(double height); double getVolume(); }; void Box::setLength(double length) { m_length = length; } void Box::setBreadth(double breadth) { m_breadth = breadth; } void Box::setHeight(double height) { m_height = height; } double Box::getVolume() { return m_height * m_length * m_breadth; }

After the class is written, we can instantiate it in this way:

 int main() { Box box; box.setBreadth(12); box.setHeight(13); box.setLength(14); cout << box.getVolume() << endl; return 0; }

Such a simple class is written, but in our class, we need to call the setxxx function to set the length, height and width. For our example, it may be too complicated. We can try to use the constructor to To solve this problem, refer to the above example

Constructor

 class Box { public: double m_length; // 长度double m_breadth; // 宽度double m_height; // 高度// 构造函数声明Box(); // 成员函数声明void setLength(double length); void setBreadth(double breadth); void setHeight(double height); double getVolume(); };忽略成员函数的实现...

In this way, we declare a simplest constructor. Of course, if you do not write this constructor, C++ will also write a default constructor for you, so don’t worry; although we wrote this constructor, but The corresponding function is still not implemented, but in order to achieve the target function, we only need to do a little more simple work

 class Box { public: double m_length; // 长度double m_breadth; // 宽度double m_height; // 高度// 构造函数声明Box(double length, double breadth, double height); // 成员函数声明void setLength(double length); void setBreadth(double breadth); void setHeight(double height); double getVolume(); }; Box::Box(double length, double breadth, double height) { m_height = height; m_breadth = breadth; m_length = length; }

Then we can simply declare an object through the constructor

 int main() { Box box(12, 13, 14); cout << box.getVolume() << endl; return 0; }

Of course, the constructor can also be overloaded, so we can also retain the default constructor while retaining the constructor in the above example

 class Box { public: double m_length; // 长度double m_breadth; // 宽度double m_height; // 高度// 构造函数声明Box(double length, double breadth, double height); Box(); // 默认构造函数// 成员函数声明void setLength(double length); void setBreadth(double breadth); void setHeight(double height); double getVolume(); }; Box::Box(double length, double breadth, double height) { m_height = height; m_breadth = breadth; m_length = length; }

Of course, similar to the simple constructor that assigns values ​​to each member variable in the class in the above example, C++ also provides a simpler way of writing;

 class Box { public: double m_length; // 长度double m_breadth; // 宽度double m_height; // 高度// 构造函数声明Box(); Box(double length, double breadth, double height): m_breadth(breadth), m_height(height), m_length(length){}; // 成员函数声明void setLength(double length); void setBreadth(double breadth); void setHeight(double height); double getVolume(); };

access modifier

In C++, there are also access modifiers, including (private, protected, public). The differences between these three are:

  1. private: can only be accessed by members of the class, not by external members, nor by derived classes
  2. protected: can be accessed by members of the class and derived classes, but cannot be accessed by external members
  3. public: can be accessed by members of the class, derived classes, and the outside world

static member variable

In a class, we can let all classes share the same variable through static member variables. For example, we have a rabbit class now, and we need to count the number of objects instantiated through this rabbit class, then we can be written like this

 #include <iostream> using namespace std; class Rabbit { public: static int count; Rabbit(); int getFriendsNum() { return count; } }; Rabbit::Rabbit() { count++; } int Rabbit::count = 0; // 一定记得在使用静态成员变量的时候要先在外面声明int main() { for (int i = 0; i < 10; i++) { Rabbit rbt; cout << "IDX: " << i << "Num: " << rbt.getFriendsNum() << endl; } return 0; }

This article is reprinted from: https://grimoire.cn/cpp/cpp-class.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment

Your email address will not be published.