C ++ class private members
1. class private members
The constructor for the class object data members have assignments, but still allow any part of the program essentially confuse the members belonging to the object, it is probably a bit contradictory. If arranged excellent level of surgeons knife after our bodies, letting local plumbers or masons go, this is hardly appropriate. We need to take measures to protect the data members of the class.
Use the keyword when defining a class member private
, you can get the security you need. Typically, private class members can only access the class member functions. Of course, there is an exception, but we'll consider it later. There is no direct way to access common functions private class members.
Private class members to specify the internal interface of the class and implemented separately. The interface consists of class public
members and public
member functions composition, when necessary, public
function members may include providing private
indirect access to all class members, including members of. For example, the class designated as internal members private
, they can be modified to improve performance, but without having to modify the code used by the class public interface. In order to ensure the security of data members and member functions of the class to make it from unnecessary intervention will not need to expose those members declared to private
be a good habit. Only those members of the class declaration in order to use and as required public
.
//============================================================================
// Name : std::class
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using std::cout;
using std::endl;
class CBox // Class definition at global scope
{
public:
// Constructor definition using an initialization list
explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) : m_Length { lv }, m_Width { wv }, m_Height { hv }
{
cout << "Constructor called." << endl;
}
// Function to calculate the volume of a box
double volume()
{
return m_Length * m_Width * m_Height;
}
private:
double m_Length; // Length of a box in inches
double m_Width; // Width of a box in inches
double m_Height; // Height of a box in inches
};
int main()
{
CBox match { 2.2, 1.1, 0.5 }; // Declare match box
CBox box2; // Declare box2 - no initial values
cout << "Volume of match = " << match.volume() << endl;
// Uncomment the following line to get an error
// box2.m_Length = 4.0;
cout << "Volume of box2 = " << box2.volume() << endl;
return 0;
}
Constructor called.
Constructor called.
Volume of match = 1.21
Volume of box2 = 1
2. The example shows
Now CBox
constructor declared explicit
, so we do not expect to avoid implicit conversion. CBox
Now contains two class definitions section. And a front member comprising a constructor functionvolume()
of thepublic
portion, followed by data member containingprivate
portion. Now, three data members can only be accessed by member functions of this class. Without having to modify any member functions, in any case they can access all the data members of the class. However, if themain()
function to the objectbox
of them_Length
members of the assignment statement no longer is a comment form, then the compiler will get an error message stating that the data members are not accessible. If you have not already done so, you canClass View
look over the tabCBox
member of the class. Icon next to each member pointed out that the accessibility of the member. If a member is private, it will display a small padlock icon.
A point to remember is that the only way now using a constructor or member function is private to the object's data members assigned. You must ensure that all methods used to set or modify private data members of the class are provided by the member function.
Function may also put class private
section. In this case, they can only be called by other members of similar function. If the function volume()
into the private
part, the compiler will give an error message, because the main()
function of the statement attempts to use this function. If the constructor into the private
part, it will not declare any object of that class.
Defined as data members have the private
following access characteristics, CBox
class can still work. The main difference is that these members are now fully protected, from unauthorized access and modification.
Unless otherwise specified, the default access class properties are members private
. Therefore, we can all private members on the beginning of the class definition, and by omitting key private
, so they have default private
access properties. But in any case explicitly specified access features are better, so for our purposes there is any doubt do not.
Of course, you do not have to make all the data have become members private
. The use according to class, some data may be defined as members private
, and is defined as the others public
, it depends entirely on our purpose. If no member of the class designated as public
the reason, it is best to assign them to private
, this could make the class more secure. Any ordinary function can not access the class private
members.
Standard Template Library,STL:标准模板库
References
Ivor Horton’s Beginning Visual C++ 2013
http://www.wrox.com/WileyCDA/WroxTitle/productCd-1118845714.html