当前位置:网站首页>On insect classes and objects
On insect classes and objects
2022-06-26 13:48:00 【Hua Weiyun】
Classes and objects
Preliminary understanding of process oriented and object-oriented
C Language is == Process oriented == Of ,== Focus on == Yes. == The process ==, Analyze the steps to solve the problem , Solve the problem step by step through the function call .
C++ yes == Based on object-oriented == Of ,== Focus on == Yes. == object ==, Split a thing into different objects , It depends on the interaction between objects
Class introduction
==C In language , Only variables can be defined in the structure , stay C++ in , Not only can variables be defined in a structure , You can also define functions ==
== such as c Define a stack in language ==
== and c++ Class is introduced in What is class Class is to define a new type ==
== Class consists of two parts :1. Member variables ( attribute )2. Member functions ( Behavior )==
==c++ Medium struct compatible c All uses of , meanwhile c++ Zhongba struct Upgrade to class ==
The definition of the above class ,== stay C++ I prefer to use class Instead of ==
This is why the definition of class in school is not struct, It is class
The definition of a class
class A{ // The class body : It consists of member functions and member variables }; // Like the structure, don't forget the semicolon==class== Define the of the class == keyword ==,==A Is the name of the class ==,=={} Is the body of the class ==, Be careful == Class definition ends with a semicolon ==.
The elements in the class are called == Class members ==: Class == data == be called == Attributes of a class == perhaps == Member variables ==; Class == function == be called == Class method == perhaps == Member functions ==.
Two ways to define classes
Declarations and definitions are all placed in the class body , We need to pay attention to : If a member function is defined in a class , The compiler may treat it as an inline function
The reason is .
The statement is placed in .h In file , The definition of a class is placed in .cpp In file
Class access qualifier and encapsulation
Access qualifier
C++ The way to achieve encapsulation :== Use classes to combine the properties and methods of objects , Make the object more perfect , Selectively provide its interface to external users through access rights ==
== Access qualifier description ==
- public Decorated members can be accessed directly outside the class
- protected and private Decorated members cannot be directly accessed outside the class ( here protected and private It's similar )
- The access scope is from == The position where the access qualifier appears begins until the next access qualifier appears ==
- class Of == The default access is private==,==struct by public==( because struct To be compatible with C)
Interview questions :c++ in struct and class What's the difference
C++ Compatibility required C Language , therefore C++ in struct It can be used as a structure . in addition C++ in struct It can also be used to define classes . and class Yes, the definition class is the same , The difference is that ==struct The default access method for members is public,class Yes, the default access method for members is private.==
Interview questions : Three characteristics of object-oriented encapsulation Inherit polymorphic
In fact, there are more than three characteristics such as == abstract Reflection ==
In the class and object phase , We only study == Class encapsulation features ==, What is encapsulation ?
== encapsulation ==: Organically combine data and methods of operating data , Hide object properties and implementation details , Only expose interfaces to interact with objects .
== Data and methods are encapsulated in classes , Manage , I want to define your access as shared , What you don't want to access is defined as private or protected ==
== Encapsulation is essentially management ==: How do we manage the terracotta warriors and horses ? For example, if you don't care about anything , The terracotta warriors and horses were destroyed at will . So we first built a house to encapsulate the terracotta warriors and horses . But our purpose is completely encapsulated , Don't let others see . So we opened the ticket channel , You can buy tickets and visit under a reasonable supervision mechanism . Class is the same , We use class data and methods encapsulated in .
What you don't want others to see , We use protected/private Encapsulate members . Open some common member functions for reasonable access to members . So encapsulation is essentially a kind of management .
边栏推荐
- Here document interaction free and expect automatic interaction
- 7-2 a Fu the thief
- A primary multithreaded server model
- H5 video automatic playback and circular playback
- MySQL explanation (II)
- Stack, LIFO
- What features are added to Photoshop 2022 23.4.1? Do you know anything
- Thinking caused by the error < note: candidate expectations 1 argument, 0 provided >
- Nexys A7开发板资源使用技巧
- I have a good word to say, and I admire myself
猜你喜欢

Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached

2021-10-18 character array

Es sauvegarde et restauration des données par instantané

Awk tools

MySQL explanation (II)

I met the problem of concurrent programming in an interview: how to safely interrupt a running thread

Bigint: handles large numbers (integers of any length)

Beifu PLC based on NT_ Shutdown to realize automatic shutdown and restart of controller

Zero basics of C language lesson 7: break & continue

character constants
随机推荐
character constants
Design of simple digital circuit traffic light
shell脚本详细介绍(四)
Log in to the server using SSH key pair
Applicable and inapplicable scenarios of mongodb series
mysql配置提高数据插入效率
Mysql database explanation (6)
Es sauvegarde et restauration des données par instantané
[how to connect the network] Chapter 2 (next): receiving a network packet
免费的机器学习数据集网站(6300+数据集)
Analysis of state transition diagram of Beifu NC axis
Zero basics of C language lesson 7: break & continue
虫子 类和对象 上
Select tag - uses the default text as a placeholder prompt but is not considered a valid value
2021-10-09
Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached
古瑞瓦特冲刺港交所上市:创下“多个第一”,获IDG资本9亿元投资
Global variable vs local variable
Design of PHP asymmetric encryption algorithm (RSA) encryption mechanism
Wechat applet SetData dynamic variable value sorting





