In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What are the C++ classes and objects? for this question, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Needless to say, let's go straight to the subject: object: everything in the objective world can be regarded as an object, and each object should have attributes (static characteristics, such as a class, a major, a classroom) and behavior (dynamic characteristics, such as learning, meetings, sports, etc.). An object is made up of a set of properties and a set of behaviors. Class: the type of object that represents the common characteristics and characteristics of a group of objects. A class is an abstraction of an object, and an object is a concrete instance of a class.
2.1 introduction of classes
In C language, we define a structure as follows:
Struct Student {int _ age; char* _ Gender; char* _ Name;}; int main () {struct S; return 0;}
As we all know, in C, "data" and "operations (functions) that process data" are separate, and the language itself does not support the correlation between "data and functions". So, what should we do if we want to associate the data with the function in a particular situation? Let's first look at an example:
Obviously, the compiler made a mistake, and there are still many. That is to say, it is not allowed to define functions in structures in C language, so is it the same in C++?
By comparison, we obviously find that C++ can do the operation that we want to have a certain relationship between the data and the function. In order to distinguish the definition of struct in C language, we often use class instead of struct in C++.
2.2 definition of CLA
# include using namespace std;class Student {public: char* _ name;private: char* _ Gender; int _ age;}; int main () {class S; return 0;}
Class is the keyword that defines a class in C++. Student is the name of the class, and {} is the body of the class, similar to the structure, followed by;. The elements in the class are called members of the class, the data in the class is called the attributes of the class or the member variables of the class, and the functions in the class are called the member functions of the class.
Classes are usually defined in two ways:
1. Put the declaration and definition of the class in the class body za2, the declaration of the class in the .h header file, and the definition in the .cpp file
In the code, we see private and public, two things we haven't seen before. Next, we'll talk about the three major features of C++: inheritance encapsulation, polymorphic encapsulation, hiding the properties and realistic details of objects, only public interfaces and objects interacting with objects, and organically combining data with methods for manipulating data.
The public and private here are the access qualifiers in C++. There are three access qualifiers: public (public) protected (protected) private (private). We will use these qualifiers in the following blog posts, which will be introduced in the following articles, which readers only need to know.
Here are some instructions:
1. Public members can be accessed directly outside the class. 2. Protected and private members cannot be accessed directly outside the class. Here we simply regard them as the same. (the difference between the two is that the private member of the base class cannot be accessed in the derived class. If the member of the base class does not want to be accessed outside the class, but needs to be accessible in the derived class, it is defined as protected 3. Their scope starts from the location where the access qualifier appears until the next access qualifier appears. 4. The default access permission of class is private, while the default access permission of struct is public (because struct is compatible with C #).
So how do we access private member variables in a class outside the class? (interview questions)
Method 1: add a common method to the class
We can see that public functions defined in public can access private member variables outside the class, which is a way to access them.
Scope of the class
In the C language stage, we know that there are only two kinds of scope of variables: local domain and global domain, while in C++, there are four kinds of scope: local domain, global domain, namespace domain and class domain. I believe we are familiar with the previous domains. I'll talk about class domains here.
The class defines a new scope, and all members of the class must be in the scope of the class. The formal parameter table and function body 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. Outside the scope of a class, operators can only be accessed through objects (or pointers) with the help of members. And-> to access class members, and the name after the access operator must be in the scope of the associated class.
Let's take a look at how to implement the above meaning in the code:
# define _ CRT_SECURE_NO_WARNINGS 1#include using namespace std;namespace N1 / / Namespace domain {int a = 10;} int a = 20; / / Global domain void FunTest () {cout
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.