Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Definition, declaration and initialization case analysis of C++

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the definition of C++, declaration and initialization case analysis of the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading the definition of this C++, declaration and initialization case analysis articles will have a harvest, let's take a look.

Define

The definition of a variable is used to allocate storage space for a variable and to specify an initial value for the variable.

Int units_sold;double sales_price, avg_price;std::string title;Sales_item curr_book; / / class Sales_item initialization

C++ supports two forms of initialization variables: copy initialization and direct initialization. Copy initialization syntax with the equal sign (=), direct initialization is to put the initializer in parentheses.

Int ival (1024); / / direct-initializationint ival = 1024; / / copy-initialization

Initialization is not an assignment. Initialization means creating a variable and assigning it an initial value, while an assignment erases the current value of the object and replaces it with a new value.

When defining variables without initializers, the system sometimes initializes variables for us.

1. Built-in type variable

(Built-in Types, i.e. int,float,double,void,char,bool, etc.) Note that string is a type defined by the standard library, not a built-in type)

The variables defined outside the function are initialized to 0, and the built-in type variables defined in the function body are not initialized automatically.

two。 Class

A class controls the initialization of a class object by defining one or more constructors. When you create a new object of a class type, you execute a constructor to ensure that the data member of each object has the appropriate initial value.

The constructor can contain a list of constructor initializers, starting with a colon, followed by a comma-separated list of data members, each followed by an initializer in parentheses. Like any member function, the constructor can be defined inside or outside the class.

Constructor initialization is specified only in the definition of the constructor, not in the declaration.

/ / initialize isbn members to the values of the book parameter, and initialize units_sold and revenue to 0. Sales_item::Sales_item (const string & book): isbn (book), units_sold (0), revenue (0.0) {}

If no initializer is provided, the default constructor is used. If a class has a default constructor, you can define variables for that class without explicitly initializing them. For example, the string class defines a default constructor to initialize the string variable as an empty string.

String aittcout

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report