In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to define and declare classes in C++. The content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this C++ article on how to define and declare classes. Let's take a look.
C++ language can be regarded as an extension and improvement of C language. Compared with C language, C++ language mainly adds object-oriented features. Class (Class) is the implementation of C++ object-oriented programming.
No matter what programming language it is, object-oriented programming will start from the design of the class. What is a class? In fact, it is very simple, we can understand it as a user-defined data type, similar to the struct in C language.
First, let's take a look at the declaration of a class, and let's look at an example.
[example 1] the simplest class declaration:
Class student
{
/ / you can declare class member variables and member functions here
}
In example 1, a student class is created, in which member variables and member functions of the student class can be declared to describe all aspects of student, such as name, student number, age, and so on.
In example 1, class is the C++ keyword and is also the keyword used for class declaration. Immediately after the class keyword is our custom class name student.
The class definition in example 1 can be understood to mean that we define a new data type whose identifier is student (this is similar to int is the C++ identifier).
In particular, it is important to note that at the end of the class declaration, the closing parenthesis "}" follows the semicolon to the right; ", this semicolon must not be forgotten, it is part of the class declaration. If it is missed, it will fail in the compilation of the program.
Once the student data type is declared, we can use it to define variables, such as:
Student LiLei; / / create an object
In this statement, a variable of LiLei is declared using the student data type, which is the same as
Int a; / / define the variable × × ×
Statement defines an integer variable to express a similar meaning. The variable LiLei is what we call the object of the student class.
When defining an object with a class, be sure to give the class declaration first, just as when you define a variable with a custom data type, we must first give a declaration for that data type. Because C++ itself integrates some commonly used data types, such as int, bool, double, etc., we no longer need to make type declarations when declaring variables with these data types.
The class keyword can be used or not when defining the objects of a class, as shown in example 2, but we usually omit the class keyword out of habit.
[example 2] the class keyword can be used or not:
Class student LiLei; / / correct
Student LiLei; / / is also correct
When defining a class object, it is feasible to define an array or pointer with a class in addition to being able to define a single variable.
[example 3] define an array of objects or pointers:
Student all_student [1000]
Student * pointer
In example 3, we define an all_student array with 1000 elements, each of which is of type student. In addition, we define a pointer of type student, pointer, which can point to a variable of type student, which is used in the same way as a normal pointer
This is the end of the article on "how to define and declare classes in C++". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to define and declare classes in C++". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.