The difference between struct and class

Source: Internet
Author: User

The struct in C + + expands on the struct, which is no longer just a data structure that contains a different datatype, and it has acquired too much functionality.
Can struct include member functions? Yes.
struct can inherit it. Yes..
Can struct achieve polymorphism? Yes...
Since all this can be achieved, what difference does it make with class?
One of the most essential differences is the default access control:
Default inherited access rights
struct is public, class is private.
You can write the following code:
struct A
{
Char A;
};
struct B:A
{
Char b;
};
This time B is public to inherit a.
If you change the above struct to class, B is the private inheritance of a. This is the default inherited access permission.
So we usually write this when we write the class inheritance:
Class B:public A
is to indicate public inheritance, not the default private inheritance.

Of course, whether the default is public inheritance or private inheritance depends on the subclass rather than the base class.
I mean, struct can inherit class, the same class can inherit struct, then the default inheritance access is to see whether the subclass is using struct or class. As follows:

struct A{};class b:a{}; Private inheritance
struct c:b{};//public inheritance

struct as the implementation of the data structure, its default access control is public, and class as the object's implementation body, its default member variable access control is private

I still emphasize that struct is an implementation of a data structure, although it can be used like class. I still call the variables in the struct, and the variables within class are called members, though they are no different.

In the end is using struct or class, completely see personal preferences, you can all the classes in the program to replace all the struct, it can still very normal operation. But the best advice I give you is: When you think you're going to do more like a data structure, use struct, and if you want to do something more like an object, use class.
Of course, what I want to emphasize here is that access control should be clearly stated in the program, rather than relying on default, which is a good habit and makes your code more readable.
Here, a lot of people who know may think the topic is over, because they know that the "only" difference between struct and class is access control. This distinction is indeed mentioned in many documents.
But I did not use "the only", but the "most essential", that is because they do have another difference, although that difference may be rarely involved in our usual.
That is: the keyword "class" is also used to define template parameters, just like "TypeName". But the keyword "struct" is not used to define template parameters. This is in Stanley B.lippman written inside the C + + Object model has been described.
Discussion of the problem here, basically should be able to end. But someone once said that he has found other "differences", so let's see if this is another difference. or above, the struct in C + + is the expansion of the struct in C, since it is expanded, then it will be compatible with the past C struct should have all the characteristics. For example you can write this:
struct A//define a struct
{
Char C1;
int n2;
Double db3;
};
A a={' P ', 7, 3.1415926}; Direct assignment when defined
That is to say, struct can assign an initial value with {} at the time of definition. So the question is, class can't do it. Change the above struct to class, try it. Error. Oh ~ So the man jumped out and said, he found another difference. Let's take a closer look, is this really another difference?
You try to add a constructor (or virtual function) to the struct above and you will find something.
Yes, struct can't assign an initial value with {}
It is true that the initial value is given in {}, and the data is initialized sequentially with an initialization list, as above if written a a={' P ', 7}; then c1,n2 is initialized, and DB3 does not. Such a simple copy operation can only happen on a simple data structure, not on an object. Adding a constructor or a virtual function makes struct more characteristic of an object, and makes this {} operation no longer valid.
In fact, because of the addition of such a function, the internal structure of the class has changed. Instead of adding a normal member function. You will find that {} is still available. You can actually interpret a common function as an algorithm for data structures that doesn't break the properties of its data structure.
So, see here, we find that even if struct wants to use {} to assign an initial value, it must also satisfy a lot of constraints, which in fact make struct more of a data mechanism than a class attribute.
Then why do we just change struct to class,{in the top?
In fact, the problem happens to be what we talked about-access control. You see, we forget something. Yes, change the struct into class, access control from public to private, which of course can not use {} to assign an initial value. Plus a public, you will find that class is also able to use {}, and struct no difference ...
To make a summary, from the above differences, we can see that struct more suitable as a data structure of the implementation body, class more suitable as an object of the implementation of the body.

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.