当前位置:网站首页>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 ;
边栏推荐
- QT - draw something else
- [opengl] bone animation blending effect
- Zzuli: cumulative sum of 1050 factorials
- NOI OPENJUDGE 1.6(09)
- How to color ordinary landscape photos, PS tutorial
- Code writing and playing method of tonybot humanoid robot at fixed distance
- cpu飙升排查方法
- 牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
- Pyqt interface production (login + jump page)
- 2021-10-16 initial programming
猜你喜欢
[ue4] material and shader permutation
C language DUP function
C string format (decimal point retention / decimal conversion, etc.)
Protobuf and grpc
Adc128s022 ADC Verilog design and Implementation
Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
Creation of data table of Doris' learning notes
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
[engine development] in depth GPU and rendering optimization (basic)
远程服务器后台挂起 nohup
随机推荐
Adobe Premiere Pro 15.4 has been released. It natively supports Apple M1 and adds the function of speech to text
Zzuli:1041 sum of sequence 2
Analysis of gene family characteristics - chromosome location analysis
7-3 count the number of words in a line of text
Awvs batch operation script
7-1 positive integer a+b (15 points)
Zzuli:1048 factorial table
[opengl] pre bake using computational shaders
Tonybot humanoid robot checks the port and corresponds to port 0701
[graphics] real shading in Unreal Engine 4
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
Find books ()
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
Implement Gobang with C language
从书本《皮囊》摘录的几个句子
dllexport和dllimport
B2020 分糖果
556. 下一个更大元素 III : 简单构造模拟题
Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)