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 is the concept of C++ class and encapsulation and how to use it

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the concept of C++ class and encapsulation and how to use it". In daily operation, I believe many people have doubts about what the concept of C++ class and encapsulation is and how to use it. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what is the concept of C++ class and encapsulation and how to use it?" Next, please follow the editor to study!

I. combination of classes

Generally speaking, computers are composed of CPU, memory, motherboard, keyboard and hard disk.

Second, class encapsulation

Classes are usually divided into the following two parts

Implementation details of the class

How to use the class

When using a class, you don't need to care about its implementation details

When you create a class, you need to consider its internal implementation details

Example:

Ordinary users use mobile phones

You just need to learn how to text, make phone calls, take pictures, etc.

Mobile phone development engineer

You need to consider the implementation details inside the phone.

The basic concept of encapsulation

As a rule of thumb: not every property of a class is public

Girls don't want outsiders to know their weight and age

Boys do not want others to know their height and income

The properties of some classes are open to the public.

Such as: person's name, educational background, nationality, etc.

The exposure level of properties and behaviors must be defined in the class representation

Permissions for files similar to those in a file system

Encapsulation of classes in C++

Member variables: variables used to represent class attributes in C++

Member functions: functions used to represent class behavior in C++

In C++, you can define access levels for member variables and member functions.

Public

Member variables and member functions can be accessed and called inside and outside the class

Private

Member variables and member functions can only be accessed and called within the class

Let's take a look at a piece of code that accesses the properties of a class member:

# include struct Biology {bool living;}; struct Animal: Biology {bool movable; void findFood () {}}; struct Human: Animal {void sleep () {printf ("isimm sleeping...\ n");} void work () {printf ("isimm working...\ n");}}; struct Girl: Human {private: int age; int weight Public: void print () {age = 22; weight = 48; printf ("I'm a girl, Isimm% d years old.\ n", age); printf ("My weight is% d kg.\ n", weight);}}; struct Boy: Human {private: int height; int salary;public: int age; int weight; void print () {height = 175 Salary = 9000; printf ("I'm a boy, my height is% d cm.\ n", height); printf ("My salary is% d RMB.\ n", salary);}}; int main () {Girl g; Boy b; g.print (); b.age = 19; b.weight = 120; / / b.height = 180; b.print () Return 0;}

The following is the output:

Note: if we access the height in boy, because it is private, the following error will be reported at compile time:

Third, the scope of class members

Scope of class members

The scope of class members is only inside the class, and cannot be accessed directly from outside.

Member functions can directly access member variables and call member functions

Public members can be accessed by class variables outside the class

The scope of a class member has nothing to do with the access level

Note: all members of the class defined with struct in C++ defaults to public

Let's take a look at the code for the scope of a class member:

# include int i = 1; struct Test {private: int i; public: int j; int getI () {I = 3; return I;}}; int main () {int I = 2; Test test; test.j = 4; printf ("I =% d\ n", I); / / I = 2 Printf (":: I =% d\ n",:: I); / /:: I = 1; / / printf ("test.i =% d\ n", test.i); / / Error printf ("test.j =% d\ n", test.j); / / test.j = 4 printf ("test.getI () =% d\ n", test.getI ()); / / test.getI () = 3 return 0;}

The following is the output:

:: I means accessing the I variable in the default namespace, which is the global scope.

At this point, the study of "what is the concept of C++ class and encapsulation and how to use it" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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