Difference between Struct and Class: difference between StructClass

Source: Internet
Author: User

Difference between Struct and Class: difference between StructClass

The struct in C ++ expands the struct in C. It is no longer just a data structure that contains different data types. It has obtained too many functions.
Can struct contain member functions? Yes!
Can struct be inherited? Yes !!
Can struct realize polymorphism? Yes !!!
 

Since all these functions can be implemented, what is the difference between them and the class?

One essential difference is the default access control:

Default inherited Access Permissions

Struct is public and class is private.
You can write the following code:
Struct
{
Char;
};
Struct B:
{
Char B;
};

In this case, B inherits A from public.

If all the above struct values are changed to class, B inherits A from private. This is the default inherited access permission.

Therefore, when writing class inheritance, we usually write as follows:
Class B: public

It is to indicate that it is a public inheritance, rather than a default private inheritance.

 

Of course, the default value isPublic or private inheritance,Depends on the subclass rather than the base class.

I mean,Struct can inherit class, The sameClass can also inherit structThe default inherited access permission is to check whether the Sub-class is struct or class. As follows:

 

Struct A {}; class B: A {}; // private inheritance
Struct C: B {}; // public inheritance

 

Struct is the implementation body of the data structure. The default data access control is public, while the class is the implementation body of the object. The default access control of member variables is private.

 

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


Whether it is struct or class, depending on your preferences, you can replace all the classes in the program with struct, and it can still run normally. But the best advice I give is: When you think you want to do something more like a data structure, use struct. If you want to do something more like an object, use class.

Of course, what I want to emphasize here is that for access control, it should be clearly stated in the program, rather than relying on default, which is a good habit, it also makes your code more readable.

Speaking of this, many people may think this topic is over, because they know that access control is the only difference between struct and class. Many documents only mention this difference.

But I didn't use the "uniqueness", but said "the most essential", because they do have another difference, although that difference is rarely involved.

That is: the keyword "class" is also used to define template parameters, just like "typename ". However, the keyword "struct" is not used to define template parameters. This is illustrated in the Inside the C ++ Object Model written by Stanley B. Lippman.

The problem is discussed here, and it should be okay. But someone once said that he also found other "differences". Let's see if this is another difference. As mentioned above, struct in C ++ is an extension of struct in C. Since it is an extension, it must be compatible with all the features that struct should have in the past. For example, you can write:

Struct A // define A struct
{
Char c1;
Int n2;
Double db3;
};
A a = {'P', 7, 3.1415926}; // value assigned directly when defining

That is to say, struct can use {} to assign an initial value during definition. So the question is, can't the class work? Change struct to class. Try it. Error! Oh ~ So the man jumps out and finds another difference. Let's take a closer look. Is this really another difference?

You try to add a constructor (or virtual function) to the above struct. what do you find?
Yes, struct cannot use {} to assign an initial value.
Indeed, the initial value is assigned in the form of {}, but an initialization list is used to initialize the data in order, as shown in the preceding example if A = {'P', 7 }; then c1 and n2 are initialized, while db3 does not. Such a simple copy operation can only occur in a simple data structure, rather than an object. Adding a constructor or a virtual function will make struct more reflect the characteristics of an object, and this {} operation is no longer valid.

In fact, it is because such a function is added that the internal structure of the class has changed. What about adding a common member function? You will find that {} is still available. In fact, you can understand a common function as an algorithm for data structure, which does not break the characteristics of its data structure.

Then, we can see that, even if struct wants to use {} to assign an initial value, it must satisfy many constraints, in fact, these conditions allow struct to better reflect the characteristics of a data organization rather than a class.

So why can't we just change struct to class and {} It's useless?

In fact, the problem happened to be what we mentioned earlier -- access control! What did we forget? Yes. When struct is changed to class, the access control is changed from public to private, so {} cannot be used to assign the initial value. After adding a public object, you will find that the class can also be used {}, which is no different from struct !!!

To sum up, we can see from the above differences that struct is more suitable to be regarded as an implementation body of a data structure, and class is more suitable to be regarded as an object implementation 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.