当前位置:网站首页>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 ;
边栏推荐
- Puzzle (016.3) is inextricably linked
- [ue4] HISM large scale vegetation rendering solution
- Zzuli:1046 product of odd numbers
- Common commands for getting started with mongodb database
- 远程服务器后台挂起 nohup
- Adc128s022 ADC Verilog design and Implementation
- Zzuli:1047 logarithmic table
- 7-3 count the number of words in a line of text
- On MEM series functions of C language
- [qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
猜你喜欢

Qt—绘制其他东西

dllexport和dllimport

Detailed explanation of four modes of distributed transaction (Seata)

Creation of data table of Doris' learning notes

Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million

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

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

QT - draw something else

Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块

提高效率 Or 增加成本,开发人员应如何理解结对编程?
随机推荐
C # realizes the login interface, and the password asterisk is displayed (hide the input password)
Zzuli:1052 sum of sequence 4
Pytorch深度学习和目标检测实战笔记
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
Zzuli:1053 sine function
Zzuli:1040 sum of sequence 1
Luogu p5536 [xr-3] core city solution
Dllexport et dllimport
Use of constraintlayout
C string format (decimal point retention / decimal conversion, etc.)
[ue4] HISM large scale vegetation rendering solution
Zzuli:1054 monkeys eat peaches
Zhejiang University Edition "C language programming (4th Edition)" topic set reference ideas set
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
论文分享:Generating Playful Palettes from Images
556. 下一个更大元素 III : 简单构造模拟题
Creation of data table of Doris' learning notes
从书本《皮囊》摘录的几个句子
The picture quality has been improved! LR enhancement details_ Lightroom turns on AI photo detail enhancement: picture clarity increases by 30%
adc128s022 ADC verilog设计实现