Differences between struct and Class in C ++

Source: Internet
Author: User

Many people do not know much about the difference between struct and Class in C ++. In C ++, struct and Class are essentially the same, but they are also very different. I hope this will help you.

Because C ++ must be backward compatible with C. So C has something in C ++. So we should talk about struct from C. Struct in C: struct is a custom data type. Since it is a data type, the function cannot be defined. C is process-oriented, and it naturally does not have object-oriented things. Struct is in C ++; C ++ Is Object-Oriented. According to the object idea, struct should be equivalent to CLassC without Class ). Here we have introduced a big topic, the difference between process-oriented and object-oriented. Here we only select a small part.

Process-oriented thinking: data and data operations are separated. Of course, process-oriented operations can also be deliberately combined to achieve data and data operations)

Object-oriented means that data and data operations are a whole and should not be separated.

In this way, the object orientation conflicts with the process orientation. This conflict actually caused the difference between the C and C ++ of struct. In C ++, struct can contain functions, constructor, and destructor can inherit from each other. At this time .. Many people will be confused? Is struct and CLASS the same? There is no big difference between struct and class. There is little difference in usage. The only difference in usage is that the default access type is different. struct is private by default, and class is public by default. Some may ask, but we usually don't rely on the default. Different compilers may use different default methods. It is no different to think about it. I am talking about a lot of difference. In fact, it is aimed at thinking rather than using it.

First, struct inherits from C but must be suitable for Object-Oriented applications. So struct is neither lost nor redundant.

Second, struct in C ++ has been expanded and is no longer in the C Era

So what is the biggest ideological difference? I cannot make it clear. In my opinion, the thought depends on perception. The thought that may come out of my mouth may be in your eyes, or even a bunch of bad eggs. So next I will talk about the struct idea in C ++.

In the era of object orientation. I believe everyone has a deep understanding of several object-oriented words, objects, classes, interfaces, inheritance, and so on.

But you may find that there is no "interface" in C ++. When talking about interfaces, you cannot talk about JAVA. in JAVA, many inheritance operations are not allowed, but multiple interfaces can be inherited. However, C ++ allows multi-inheritance. If the interface appears in C ++, the final result will become redundant. However, the idea of interfaces has gained a high degree of development in object-oriented systems. Even the formation of Aspect-Oriented Programming AOP is actually a deepening of interface programming ).

Therefore, I think "interfaces" have actually formed a culture in object-oriented systems. However, C ++, a programming leader, does not have an interface. Human Thinking is powerful. For example, the MS approach is to use struct as an interface. Therefore, I think the difference between struct and class in C ++ lies not in usage, not in syntactic sugar, but in thinking, framework conventions, and other aspects.

  1. Something you have to say about new and delete
  2. Author of Java programming ideas: C ++ is not spam, but Java is arrogant.
  3. Differences between Java and C ++ in scope
  4. C/C ++ uses multiple methods to obtain the file size code
  5. Reload, overwrite, and hide C ++ class member functions

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.