C + + Note 12: Extended--struct keyword type enhancement for C + +

Source: Internet
Author: User

The C-language struct defines a set of variables, which the C compiler does not consider to be a new type.

A struct in C + + is a new type of definition declaration.

struct Student

{

Char name[100];

int age;

};

void Main ()

{

Student s1={"Wang", 1};

Student s2={"Wang", 2};

}

Above program we use. c file, compile error.

At this point the C compiler does not consider student to be a new type, and we must add the struct keyword in front of the student!

struct Student

{

Char name[100];

int age;

};

void Main ()

{

struct Student s1={"Wang", 1};

struct Student s2={"Wang", 2};

}

C + + provides enhanced functionality for struct keywords.

We put the same program that cannot be compiled under the C compiler into the. cpp file, and found that it can be compiled through! That is, in C + + it is assumed that a struct defines a new type, and this new type can be used to define a new variable.

#include <iostream>

using namespace Std;

struct Student

{

Char name[100];

int age;

};

void Main ()

{

Student s1={"Wang", 1};

Student s2={"Wang", 2};

System ("pause");

}

In addition, C + + not only for the struct keyword type enhancement, the struct keyword and the class keyword to complete the function is the same, of course, there are different places, the difference is again.

In the structure can also add access to data permissions: public, protected and so on.

#include <iostream>

using namespace Std;

struct Student

{

Public

Char name[100];

int age;

Private

int A;

};

void Main ()

{

struct Student s1

System ("pause");

}

Long Press to unlock

Unlock more Insider Stories

Legal programming

: Lightspeed-tech

Technology-driven Life

C + + Note 12: Extended--struct keyword type enhancement for 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.