Structure of C + +

Source: Internet
Author: User

Structure:

Struct is a special kind of class, and the only difference from class is that the default access permission for a class is private, and the default access permission for a struct is public. In addition, the main reason for the existence of structure is to maintain compatibility with C language.

When do you use structs instead of classes? Used primarily to save data. And there is no type of operation.

It is often common to have data members of structs, so it is easier to use structs

1#include <iostream>2#include <string>3 using namespacestd;4 5 structstudent{6     stringname;7     LongID;8     stringsex;9     Doublescore;Ten }; One voidMain () { A Student S; -     //accessing data members of the struct body -S.name ="Zhang San"; theS.sex ="male"; -cout << s.name <<"\ n"<<s.sex <<Endl; -     //using pointers to access data members of the struct body -student*SS; +SS = &s; -Ss->score= About; +ss->sex="female"; ASs->name="John Doe"; atcout << Ss->name <<"\ n"<< Ss->sex <<"\ n"<< Ss->score <<Endl; -     //Two variables of the same struct type that can be assigned to each other - Student S1; -S1 =s; -cout << S1.name <<"\ n"<< S1.sex <<Endl; -}

Structure of C + +

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.