In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use constructors in C++. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. Constructor:
1. What is a constructor?
The simple understanding of this constructor is that in a class, there is a function whose name is the same as the class name, and the constructor does not return a value type (Test () this function is the constructor. ):
# include
Class Test:
{
Public:
Test ()
{
Printf ("Test ()\ n")
}
}
2. Call the constructor:
(1) in general, the constructor is called automatically when it is defined (the main function is to initialize the property in the class automatically, which is what we call a variable. And the automatic meaning here, that is, when you create an object, it will automatically call the constructor, you do not have to write the constructor in the main function. ):
# include
Class Test
{
Public:
Test ()
{
Printf ("Test ()\ n")
}
}
Int main ()
{
Test t; / / call Test ()
Return 0
}
The results of the demonstration are as follows:
Root@txp-virtual-machine:/home/txp/c++#. / a.out
Test ()
(2) in some special cases, you need to call the constructor manually (this will have a case study in the constructor with parameters below)
Second, the constructor with parameters
(1) the constructor can define parameters as needed.
Class Test
{
Public:
Test (int v)
{
}
}
(2) there can be multiple overloaded constructors in a class (what overloaded function, to put it simply, can be the same as the function name, but its passing parameter type or return type is different is the overloaded function. Let's take a look at a specific example of a parameter constructor:
# include
Class Test
{
Private:
Int m_value
Public:
Test ()
{
Printf ("Test ()\ n")
M_value = 0
}
Test (int v)
{
Printf ("Test (int v), v =% d\ n", v)
M_value = v
}
Int getValue ()
{
Return m_value
}
}
Int main ()
{
Test ta [3] = {Test (), Test (1), Test (2)}
For (int item0; I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.