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

How to understand the classes and objects in C++

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to understand the classes and objects in C++, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail, people with this need can come to learn, I hope you can gain something.

The origin of C++ class (or why to add class): we know that c language is a process-oriented language, C++ is both process-oriented and process-oriented language. So what is the difference and relationship between the two?

A big difference between C language and C++

Next, we use the struct of c language to illustrate:

In C language, only variables can be defined in structures. In C++, the body of a structure can define not only variables but also functions. For example, if we implement the function in C language, then we will find that in struct we can only define the types of variables, while the functions we need have to be implemented separately.

Typedef struct Student {char _ name [20]; int _ age; char _ sex [10];} student; void SetStudentInfo (const char* name, const char* sex, int age) {strcpy (_ name, name); strcpy (_ sex, gender); _ age = age;} void print (student * s) {printf ("% c:%age-%c", s-> _ name, s-> _ age, s-> _ sex);}

The words implemented in C++ are:

/ / in order to distinguish from the c language, the definition of the function is also put directly into the structure body as an inline function. If the definition code of the function is too much or the number of times it needs to be called, it is recommended to declare it here, and then define struct Student {void SetStudentInfo (const char* name, const char* sex, int age) {strcpy (_ name, name); strcpy (_ sex, gender); _ age = age separately. } void print (student * s) {printf ("% c:%age-%c", s-> _ name, s-> _ age, s-> _ sex); / in order to distinguish it from the c language struct, the output method of c language} char _ name [20]; int _ age; char _ sex [10];}

From the above examples, we can see that struct can contain function definitions and variables in C++, whereas struct can only contain variables in c language.

The difference between struct and class

In C++, we introduce a keyword "class" to specify that the type that can contain both variables and function declarations is C++. Struct is also a class in C++, but there are some differences between it and class embellishments. We use the above code to illustrate this difference:

This is a class decorated by struct

Struct Student {void SetStudentInfo (const char* name, const char* sex, int age); void print (student * s) char _ name [20]; int _ age; char _ sex [10];}

This is a class decorated by class

Struct Student {public:void SetStudentInfo (const char* name, const char* sex, int age); void print (student * s) private: char _ name [20]; int _ age; char _ sex [10];}

We protect our data in the class, and we introduce three modifiers: public, private, and protected, which represent different permissions.

And struct default is that member functions and member variables are public, and class modification, then member functions and member variables must be modified, generally we use public to modify member functions and member variables we use private to modify.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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