Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What do C++ classes and objects refer to?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces what the C++ class and object refers to, the article is very detailed, has a certain reference value, interested friends must read it!

1. Classes in C++

The Class in C++ can be seen as an updated version of the Struct in the C language. A structure is a construction type, which can contain several member variables, each of which can have a different type; a structure variable can be defined through a structure, and each variable has the same property.

For example:

# include / / define the member variable char * name; int age; float score;} contained in the structure Studentstruct Student {/ / structure; / / display the member variable void display (struct Student stu) {printf ("% s age is% d, score is% f\ n", stu.name, stu.age, stu.score);} int main () {struct Student stu1 / / assign values to member variables of the structure stu1.name = "Xiaoming"; stu1.age = 15; stu1.score = 92.5; / / call function display (stu1); return 0;}

Running result:

Xiao Ming's age is 15 and his score is 92.500000.

The class in C++ is also a construction type, but with some extensions, the members of the class can be not only variables, but also functions; variables defined by the class also have a specific name, called "object".

For example:

# include / / define the variable char * name; int age; float score; / / contained in the class class Student {public: / / through the keyword class void say () {printf ("% s age is% d, score is% f\ n", name, age, score);}; int main () {/ / define variables through the class, that is, create an object class Student stu1 / / you can also omit the keyword class / / assign the member variable of the class stu1.name = "Xiaoming"; stu1.age = 15; stu1.score = 92.5f; / / call the member function of the class stu1.say (); return 0;}

The running result is the same as the previous example.

For readers familiar with C++, this code is not standard, please ignore this detail, the focus of this section is to introduce the concept of classes and objects. This C++ tutorial is written on the basis of C, I don't want to put forward too many C++ concepts from the beginning, I hope the readers will gradually transition from C to C++, and write the standard C++ code from the second chapter. In other words, including this section, the first chapter has a lot of irregular C++ code, please ignore it again if you are familiar with C++.

Class and public are both keywords in C++, so for beginners, please ignore public (more on this later) and focus on class.

Struct in C can only contain variables, while class in C++ can contain functions as well as variables. Display () is a function used to deal with member variables, in the C language, we put it outside the struct Student, it is separate from the member variables; in C++, we put it inside the class Student, so that it and member variables together, looks more like a whole.

Both structure and class can be regarded as a complex data type defined by users. Variables can be defined by structure name in C language and class name in C++. The difference is that variables defined by structures are still called variables, while variables defined by classes have a new name, called Object.

In the second piece of code, we first define a class Student with the class keyword, and then create an object stu1 through the Student class. Variables and functions are members of the class, and you can pass the period after creating the object. To use them.

The class can be compared to the drawing, and the object to the part, which describes the parameters of the part (member variable) and the task it undertakes (member function). One drawing can produce multiple parts with the same nature. different drawings can produce different types of parts.

The class is just a drawing, which plays an illustrative role and does not occupy memory space; objects are specific parts, and memory space will be occupied only if there is a place to store them.

In C++, an object can be created by a class name, that is, a drawing is made into a part. This process is called class instantiation, so the object is also called an instance of the class (Instance).

Some materials also call the member variables of the class Property and the member functions of the class Method.

2. Object oriented programming (Object Oriented Programming,OOP)

Class is a general concept, C++, Java, C #, PHP and many other programming languages support classes, you can create objects through classes. Classes can be seen as an upgraded version of the structure. The younger generation of the C language saw the shortcomings of the C language, tried to improve it, inherited the idea of the structure, and upgraded it, making it easier for programmers to develop or expand large and medium-sized projects.

Because C++, Java, C #, PHP and other languages all support classes and objects, writing programs in these languages is also known as object-oriented programming, and these languages are also known as object-oriented programming languages. C language is called a process-oriented programming language because it does not support the concepts of classes and objects.

In C language, we will package the code that is reused or have a certain function into a function, put multiple functions with related functions in a source file, and then provide a corresponding header file, which is a module. When using the module, the corresponding header file can be introduced.

In C++, there is an extra layer of encapsulation, that is, Class. A class consists of a set of associated functions and variables. You can put one or more classes in a source file and introduce the corresponding classes when you use them. The following is a comparison of the way the C and C++ projects are organized:

Do not underestimate the encapsulation of Class, which has many features, which greatly facilitates the development of large and medium-sized programs, and makes C++ an object-oriented language.

Object-oriented programming has absolutely no advantage in code execution efficiency, and its main purpose is to facilitate programmers to organize and manage code, quickly sort out programming ideas, and bring about innovation in programming ideas.

Object-oriented programming is proposed for large-scale programs in development, in order to improve the efficiency of software development. Do not oppose object-oriented and process-oriented. Object-oriented and process-oriented are not contradictory, but have their own uses and complement each other. If you want to develop a gluttonous snake game, classes and objects may be redundant, a few functions can be done, but if you develop a large game, you can't do without object-oriented.

The above is all the content of this article "what do C++ classes and objects refer to?" thank you for reading! Hope to share the content to help you, more related knowledge, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report