Differences between C ++ struct and class

Source: Internet
Author: User

In C ++, there are only two main differences between class and struct:

  • The default permission is inherited. By default, the class is inherited by private, while the struct is inherited by public.
  • The default access permission of the member. By default, class members are private and struct members are public by default.

Other features, such as struct and class, are basically the same:

01 // An uncommon example. You can also compile struct directly into a class.
02 // The compiling environment is GCC 4.4.1.
03 #include <iostream>
04 #include <string>
05 using namespace std;
06  
07 struct bar
08 {
09     private: // Access permission Modifier
10         int y;
11     public:
12         bar(){}; // No parameter Constructor
13         bar(int a){ y = a;}// Constructor with Parameters
14         ~bar(); // Fictitious Function
15         void say();
16         virtual void func1() = 0; // Pure virtual function
17 };
18  
19 structfoo: protected bar // Inherit
20 {
21     private:
22           int x;
23     public:
24          foo(){};
25          void say(string msg) {cout<<msg<<endl;}
26          virtual int func2();// Virtual functions
27  
28 };
29  
30 int main() {
31     return 0;
32 }

You can see:

  • All can have member functions: struct can contain the same constructor, destructor, overload operators, youyuan class, youyuan structure, youyuan function, virtual function, pure virtual functions, static functions;
  • Although the default access permissions are different, they can all have public/private/protected modifiers;
  • Can carry out complex inheritance and multi-inheritance, a struct can inherit from one or more classes, and vice versa.
  • Note that it is different from the C language. struct in C is essentially a syntax mechanism for packaging data.

The Google C ++ programming style guide also states:

Use struct only when the operator has data, and use class for others.

In C ++, the keywords struct and class have almost the same meaning. We add semantics to them so that we can reasonably choose which keyword to use for the defined data type.

Struct is used to include only passive objects (passive objects). It may include associated constants, but does not provide function functions outside the data member, the access function is implemented through direct access without calling a method. The method mentioned here is only used for data members, such as constructors, destructor, initialize (), reset (), validate ().

If more function functions are required, the class is more suitable. If you are not sure, use the class directly.

Without STL combination, you can use struct instead of class for functions and features.

Although some C ++ experts claim that they can no longer use the struct keyword, they can always use class {public:} instead. But in fact, struct is still widely used in code. Developers often use struct (partly due to the impact of the C language) to indicate a lightweight record that does not require strict encapsulation. For example, struct is often used to declare a record written to a file or database table structure, while class is mainly used for object-oriented programming.

In general, the main reason struct structures must still be used is:

  • Develop and maintain legacy systems.
  • Communication with traditional APIS is required.

Of course, sometimes using struct can make the Code look more concise:

1 struct Compare { bool operator() { ... } };
2 std::sort(collection.begin(), collection.end(), Compare());

There are too many details about the advanced features and syntax in C ++, so it is necessary to follow certain programming specifications.

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.