In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use the C++ constructor". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to use the C++ constructor.
Like structures, we can initialize them using the list initialization method:
Struct Thing {char * pn; int m;}; Thing th = {"hello", 23}
But the class is not, because the member variables in the structure are public, and the class is often private. This means that we can't access the data members directly with the program, we need to design them as functions.
In C++, the function used to construct a class is called a class constructor. The prototype and header of the constructor have an obvious feature-although it does not return a value, it is not declared as a void type. In fact, the constructor does not declare a type.
For example, we are still the previous class:
Class Stock {private: std::string company; long shares; double share_val; double total_val; void set_tot () {total_val = shares * share_val;} public: void accquire (const std::string & co, long n, double pr); void buy (long num, double price); void sell (long num, double price); void update (double price); void show ();}; # endif
Now we are going to add the constructor, first by adding a declaration to the class:
Class Stock {... Stock (const string & co, long nasty 0, double pr=0.0);}
Notice that when we implement the definition, the function has no return type:
Stock::Stock (const string & co, long n, double pr) {company = co; if (n < 0) {std::cerr
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.