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

Example Analysis of classes and objects in C++

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

Share

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

This article will explain in detail the example analysis of classes and objects in C++. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Foreword:

1. C language is process-oriented, pays attention to the process, analyzes the steps of solving, and solves the problem by calling function step by step.

2. Centering + is based on object-oriented and focuses on objects. Chiang splits a thing into different objects, which is accomplished by the interaction between objects.

For example: takeout system

Process-oriented is the three processes of placing an order, receiving an order and delivering food.

Object-oriented: the relationship between the three objects of the customer, the merchant and the rider.

1. Access qualifier:

Use classes to combine each other's properties and methods to make the object more perfect and selectively provide its interface to external users through access rights.

1. For the time being, let's think of protected and private as the same class, both of which are private types and cannot be accessed directly outside the class.

two。 Access permissions start with the scope's access qualifier and go to the next access qualifier.

3.public decorator members can be accessed outside the class

Note: access qualifiers are only useful when compiling. There is no difference between access qualifiers when data is mapped to memory.

[question] what is the difference between struct and class in C++?

Answer: C++ needs to be compatible with C language, so struct in C++ can be used as a structure. In addition, struct in C++ can also be used to define classes, which is the same as that of class. The difference is that the default access method for struct members is public, while the default access method for class members is private.

two。 Encapsulation

There are three characteristics of object-oriented: encapsulation, inheritance and polymorphism.

[question] in the class and object phase, we only study the encapsulation features of the class, so what is encapsulation?

Encapsulation: organically combine the data with the method of manipulating the data, hide the properties and implementation details of the object, and only expose the interface to interact with the object.

In a word: the essence of encapsulation is management, hiding details and opening interface.

3. Definition and declaration of class

[problem] void Push (STDataType x) {/ /... A [size] = x; size++;} / /. STDataType* a; int size; int capacity;}; int main () {/ / struct Stack st; Stack st; st.Init (); st.Push (1); st.Push (2); st.Push (3); return 0;}

Is the Push here a function called?

Answer: yes, the function exists in a code snippet in the common area.

4. Scope of the class

The class defines a new scope, and all members of the class are in the scope of the class. To define a member outside a class, you need to use the:: scope parser to indicate which class domain the member belongs to.

Class Person {public: void PrintPersonInfo (); private: char _ name [20]; char _ gender [3]; int _ age;}; / / you need to specify whether PrintPersonInfo belongs to the class domain void Person::PrintPersonInfo () {cout of Person

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: 264

*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