当前位置:网站首页>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 ;
边栏推荐
- 表单文本框的使用(一) 选择文本
- C language to implement a password manager (under update)
- Some concepts about agile
- 【7.3】146. LRU caching mechanism
- CentOS7部署哨兵Redis(带架构图,清晰易懂)
- 论文分享:Generating Playful Palettes from Images
- tonybot 人形机器人 定距移动 代码编写玩法
- 光猫超级账号密码、宽带账号密码 获取
- China PETG market forecast and Strategic Research Report (2022 Edition)
- Zzuli: sum of 1051 square roots
猜你喜欢
![[engine development] rendering architecture and advanced graphics programming](/img/a4/3526a4e0f68e49c1aa5ce23b578781.jpg)
[engine development] rendering architecture and advanced graphics programming

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

Puzzle (016.3) is inextricably linked

Zzuli:1053 sine function
![[ue4] material and shader permutation](/img/8f/7743ac378490fcd7b9ecc5b4c2ef2a.jpg)
[ue4] material and shader permutation

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

The picture quality has been improved! LR enhancement details_ Lightroom turns on AI photo detail enhancement: picture clarity increases by 30%

Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules

tonybot 人形机器人 查看端口并对应端口 0701

retrofit
随机推荐
Yolov5进阶之九 目标追踪实例1
How to query the baby category of tmall on Taobao
Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)
Rasterization: a practical implementation (2)
关于敏捷的一些概念
[ue4] geometry drawing pipeline
Some concepts about agile
NOI OPENJUDGE 1.6(09)
Tonybot humanoid robot checks the port and corresponds to port 0701
cpu飙升排查方法
NOI OPENJUDGE 1.5(23)
[engine development] in depth GPU and rendering optimization (basic)
Creation of data table of Doris' learning notes
7-9 one way in, two ways out (25 points)
Solve the problem that PR cannot be installed on win10 system. Pr2021 version -premiere Pro 2021 official Chinese version installation tutorial
puzzle(016.3)千丝万缕
puzzle(016.4)多米诺效应
Tonybot humanoid robot infrared remote control play 0630
复合类型(自定义类型)
ASTC texture compression (adaptive scalable texture compression)