C + + Learning series of articles are translated from learncpp.com, a very good C + + learning site, this site let me realize that the original profound truth can also be said so easy to understand, If all the software has a similar site that much good ah, in order to deepen the impression, I decided to translate the site by chapter, Hey, I do not believe I can do it ...
C + + provides some basic data types (for example: char, int, long, float, double, and so on ...). , these types are usually sufficient to handle relatively simple problems, but using these types alone can be difficult to solve complex problems. A common feature of C + + is the ability to customize data types to better correspond to the problems that need to be addressed. In the previous chapters you have seen how to use enumerations and structs to create your own data types.
Here is an example of using a structure to store data:
struct datestruct{ int year ; int month; int Day ;};
Enumeration types and structures that contain only data (only variables in the structure) represent the traditional world of non-object-oriented programming, because they only hold data. In c++11, we can create and initialize the structure as follows:
2020 Ten - // Use Uniform initialization
Now, if we want to print the contents of the data on the screen (what we might do often), we can write a function to do the work. The full code is as follows:
#include <iostream>structdatestruct{intYear ; intmonth; intDay ;};voidPrint (Datestruct &date) {Std::cout<< Date.year <<"/"<< Date.month <<"/"<<Date.day;}intMain () {datestruct Today {2020,Ten, -};//Use Uniform initializationToday.day= -;//Use member selection operator to select a member of the structprint (today); return 0;}
This code will output the following:
2020/10/16
Class
C + + Learning 8.2-Class and class members