C + + Learning 8.2-Class and class members

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.