当前位置:网站首页>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 ;
边栏推荐
- Zzuli:1047 logarithmic table
- [ue4] material and shader permutation
- Zzuli:1057 prime number determination
- Write a 2-minute countdown.
- Time conversion ()
- dllexport和dllimport
- Solve the problem that PR cannot be installed on win10 system. Pr2021 version -premiere Pro 2021 official Chinese version installation tutorial
- Niuke bm83 string deformation (case conversion, string inversion, string replacement)
- Zzuli:1048 factorial table
- 提高效率 Or 增加成本,开发人员应如何理解结对编程?
猜你喜欢
Creation of data table of Doris' learning notes
远程服务器后台挂起 nohup
B2020 分糖果
C language DUP function
Luogu p4047 [jsoi2010] tribal division solution
C # realizes the login interface, and the password asterisk is displayed (hide the input password)
adc128s022 ADC verilog设计实现
[ue4] material and shader permutation
Tonybot humanoid robot starts for the first time 0630
Qt development - scrolling digital selector commonly used in embedded system
随机推荐
【微信小程序】WXSS 模板样式
Zzuli:1052 sum of sequence 4
C language dup2 function
Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
Output student grades
Zzuli:1045 numerical statistics
[graphics] real shading in Unreal Engine 4
Use of form text box (I) select text
Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
Zzuli:1041 sum of sequence 2
[graphics] hair simulation in tressfx
Common commands for getting started with mongodb database
PS tips - draw green earth with a brush
Zzuli: cumulative sum of 1050 factorials
Bucket sorting in C language
零拷贝底层剖析
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
Devaxpress: range selection control rangecontrol uses
Analysis of gene family characteristics - chromosome location analysis
Common shortcut keys in PCB