C + + class and struct struct differences

Source: Internet
Author: User

Transferred from: http://www.weixueyuan.net/view/6337.html

The C + + language inherits the struct and expands it. In C, a struct can define only data members, not member functions. In C + +, structs are similar to class, where you can define both a data member and a member function.

In C + +, struct and class are basically generic, the only difference is that if you use the Class keyword, the member variables or member functions defined in the class are private properties by default, and the struct keyword is used. A member variable or member function defined in a struct is the public property by default.

In C + +, the C language of the struct keyword is not discarded, its significance lies in the C language Program developers have a sense of belonging, and can make the C + + compiler compatible with the previous development of the C language project.

[Example 1] Examples of C + + structs:

#include <iostream>using namespacestd;structbook{DoublePrice ; Char*title; voiddisplay ();};voidBook ::d isplay () {cout<<title<<", Price:"<<price<<Endl;}intMain () {book Alice; Alice.price=29.9;//It ' s OKAlice.title ="Alice in Wonderland";//It ' s OKAlice.display ();//It ' s OK    return 0;}

In this example, a struct named book is defined, where the member variable title and price are defined, and a function is declared inside a struct that is defined outside the struct body.

The program sees here, it is not difficult to find that the struct and class keyword in C + + its basic syntax is exactly the same. Next, let's take a look at the main function. First, an object Alice is defined by the book structure. With the member selector, the Alice object invokes the variables and functions defined in the book struct in the next three lines of code!

Thus, in a struct declaration, the default property is the public property, which can be accessed arbitrarily outside the struct.

[Example 2] C + + Class Example:

#include <iostream>using namespacestd;classbook{DoublePrice ; Char*title; voiddisplay ();};voidBook ::d isplay () {cout<<title<<", Price:"<<price<<Endl;}intMain () {book Alice; Alice.price=29.9;//Compile ErrorAlice.title ="Alice in Wonderland";//Compile ErrorAlice.display ();//Compile Error    return 0;}

Take another look at example 2, example 2 program relative to Example 1, only changed one place: replace the struct keyword with the class keyword. As a result, after the Alice object is defined in the main function, we attempt to access its internal price, title variable, and display function through the Alice object, at which point the compiler prompts the compilation error and the error indicates that the three are inaccessible.

As we have expected, the member variable or member function defined in class does, and the default property is private.

Example 3:

class book{public:    void setprice (double  a);     Double GetPrice (); Private :     Double Price ;};

Example 4:

struct book{    void setprice (double  a);     Double GetPrice (); Private :     Double Price ;};

Example 5:

struct book{public:    void setprice (double  a);     Double GetPrice (); Private :     Double Price ;};

In the preceding section, we define a class named book as shown in Example 3, and the equivalent struct definition can be as shown in Example 4, which is also possible if we explicitly declare the Setprice and GetPrice member functions as public properties in the struct. As shown in Example 5.

C + + class and struct struct differences

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.