当前位置:网站首页>Chapter 14 class part 1
Chapter 14 class part 1
2022-07-03 14:50:00 【III VII】
14.1 Member functions 、 Object replication and private members
14.1.1 Related foundations of classes
1. Composition of classes : Member variables and member functions ;
2. Accessing members of a class , If you use class objects to access , So it's “ Object name . Member name ”; If it is a pointer to an object to access , So it's “ Pointer name -> Member name ”;
3 struct and class The difference between :
- struct and class You can customize the data type , Inheritance operations are also supported .
- struct The default access level in is public, The default inheritance level is also public;class The default access level in is private, The default inheritance level is also private.
- When class Inherit struct perhaps struct Inherit class when , The default inheritance level depends on class or struct In itself , class(private Inherit ),struct(public Inherit ), That is, it depends on the default inheritance level of the derived class .
tip: It is recommended to set the member only variable to struct;
14.1.2 Member functions
Definition of member function :
class Time {
public:
int Hour;
int Minute;
int Second;
void initTime(int tmphour, int tmpmin, int tmpsec) // Member functions
{
Hour = tmphour;
Minute = tmpmin;
Second = tmpsec;
}
14.2 Constructors 、explicit And initialization list
14.2.1
How to write a constructor
class A
{
public:
{
void myfunc()
{
Implementation code
}
}
}
// How to write it 2
class A
{
public:
{
void myfunc()
}
}
void A::myfunc()
{
Implementation code
}
14.2.2 Constructors
1. Constructor is automatically called when creating class object ;
2. The constructor is to initialize the data members of the class
Implementation of constructor :
Time::Time(int tmphour, int tmpmin, int tmpsec)
:Hour(tmphour),Minute(tmpmin) // This is called constructor initialization list
{
Hour = tmphour;
Minute = tmpmin;
Second = tmpsec;
initMillTime(0);
}
matters needing attention :1. Constructor has no return value
2. Constructors cannot be called manually
3. Constructor is public function , Because he will be called by the system
4. Constructor if there are parameters , Then it should also have corresponding parameters when creating objects
Time myTime = Time(12, 13, 52);
Time myTime2(12, 13, 52);
Time myTime3 = Time{
12, 13, 52 };
Time myTime4{
12, 13, 52 };
// Time myTime5 = { 12, 13, 52 };
//Time myTime6(); // It can be , No parameters , It may be mistaken by the compiler for a function declaration
//Time myTime7(12, 13); // Can not be , Lack of parameter
14.2.3 Multiple constructors
A class can provide multiple construction parameters , It means that you can provide multiple creation methods for this class ;
14.2.4 Function default parameters
If the constructor is written as follows
Time::Time(int tmphour, int tmpmin, int tmpsec=12)
Then you can pass only two parameters to the constructor ;
14.2.5 Implicit transformation sum explicit
add explicit The declaration indicates that the constructor must perform a display conversion ;
边栏推荐
- How to color ordinary landscape photos, PS tutorial
- [opengl] geometry shader
- [opengl] bone animation blending effect
- 复合类型(自定义类型)
- B2020 分糖果
- Tonybot humanoid robot starts for the first time 0630
- Adc128s022 ADC Verilog design and Implementation
- Tensor 省略号(三个点)切片
- [ue4] HISM large scale vegetation rendering solution
- Use of form text box (I) select text
猜你喜欢

QT - draw something else

retrofit

puzzle(016.4)多米诺效应

Tonybot Humanoïde Robot Infrared Remote play 0630

My QT learning path -- how qdatetimeedit is empty
![洛谷P4047 [JSOI2010]部落划分 题解](/img/7f/3fab3e94abef3da1f5652db35361df.png)
洛谷P4047 [JSOI2010]部落划分 题解
![[engine development] in depth GPU and rendering optimization (basic)](/img/71/abf09941eb06cd91784df50891fe29.jpg)
[engine development] in depth GPU and rendering optimization (basic)

How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03

【微信小程序】WXSS 模板样式

C string format (decimal point retention / decimal conversion, etc.)
随机推荐
Table of mathematical constants by q779
Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
The picture quality has been improved! LR enhancement details_ Lightroom turns on AI photo detail enhancement: picture clarity increases by 30%
[ue4] material and shader permutation
零拷贝底层剖析
Four data flows and cases of grpc
Code writing and playing method of tonybot humanoid robot at fixed distance
Write a 2-minute countdown.
Optical cat super account password and broadband account password acquisition
Zzuli:1052 sum of sequence 4
C language to implement a password manager (under update)
[graphics] real shading in Unreal Engine 4
Tonybot humanoid robot infrared remote control play 0630
puzzle(016.3)千丝万缕
链表有环,快慢指针走3步可以吗
Zzuli:1044 failure rate
Showmebug entered Tencent conference, opening the era of professional technical interview
mmdetection 学习率与batch_size关系
C language DUP function
C language STR function