C ++ class and struct

Source: Internet
Author: User
Tags modifiers
Old problems. I have summarized some omissions and errors.

About using braces for initialization:
Class and struct cannot be initialized using braces if constructors are defined.
If no constructor is defined, struct can be initialized with braces.
If no constructor is defined and all member variables are public, you can use braces to initialize them.

About default access permissions
The default access permission for members in class is private, while that for struct is public.

Inheritance Methods
By default, class inheritance is private inheritance, while struct inheritance is public inheritance by default.
Check the following code (check the error message provided by the compiler ):
Class T1
{
Public:
Void F ()
{
Cout <"T1: F ()" <Endl;
}
Int X, Y;
};
Struct T2
{
Int X;
Void f () {cout <"T2: f ()" <endl ;}
};
Struct TT1: T1
{
};
Class TT2: T2
{
};
Int main ()
{
TT1 t1;
TT2 t2;
T1.f ();
T2.f ();
}
About templates
In the template, class or typename can be used before the type parameter. If struct is used, the meaning is different. struct is followed by "non-type template parameter ", the class or typename follows the type parameter. Template <struct X>
Void f (X x)
{
}
// Error message: d: codecpptestcpptestcpptest. cpp (33): error C2065: 'X': undeclared identifier

Http://computer.info-man.net/zs_4421.html

Differences between class and struct

This topic may be very simple. Both are user-defined data types, but we all know that when a struct is declared, if we do not define an access modifier in struct, all the members (data and functions) in struct are public access modifiers by default. If no access modifier is defined in the class, all the members (data and functions) in the class) by default, all are private access modifiers. This is the only difference between them.

If you are a language designer, would you design two types like this? I don't think so, because there is no orthogonal between them, such a design is redundant. So the question is, why does struct exist in C ++? Many people will think that this is to make C ++ and C compatible, so the question is how to use struct in C ++ to be compatible with C.

Declare the data that C ++ wants to exchange to C programs into a struct in C ++ program, and then inherit or aggregate the struct according to the meaning of the program in C ++, then, the struct can be encapsulated and passed to C or from C to C ++. (Note: We recommend that you use aggregation instead of inheritance in an in-depth exploration of the C ++ object model because it is compatible with the spatial distribution of C, because virtual functions are introduced in C ++, virtual inheritance leads to the space of the derived class, including the space of struct, vptr, and vbtl. The Compiler determines where the vptr and vbtl are stored, which is not defined in the C ++ standard ).

A simple example from C ++ to C is provided:

// C ++ header file modulea. h

# Ifndef module_a_h

# Define module_a_h

Struct person
{
Int age;
Char name [20];
};

Class male
{
Private:
Int wife;
Struct person per;

Public:
Int getAge ()
{
Return per. age;
}

};

Extern "C" void print (person );

# Endif

// Implementation file of C ++

# Include "moduleA. h"

# Include <stdio. h>

Void print (person per)
{
Printf ("Age: % d,/N", Per. Age );
Return;
}

/* C language files to be processed */

# Include <stdio. h>

Extern "C"
{
# Include "modulea. H" // or compile
}

Int main (void)
{
Person per;
Per. Age = 18;
Print (PER );
Return 0;

}

Http://www.blog.edu.cn/user2/49769/archives/2006/1544422.shtml

Let's discuss the difference between the structure (struct) and the class. What do you think is the role of the structure (struct)? Can it be completely replaced by the class ??
---------------------------------------------------------------

First, we should only discuss this question in terms of syntax. If we discuss the differences in programming styles between different people, there is no answer to this question. After all, different people have different preferences.

In terms of syntax, in C ++ (only in C ++ ). There are only two differences between class and struct in defining the type:
(1) permissions are inherited by default. If it is not explicitly specified, the inheritance from class is handled by private inheritance, and the inheritance from struct is handled by public inheritance;
(2) default access permissions of members. By default, class members are private and struct members are public by default.
Apart from these two points, class and struct are basically one thing. There is no syntax difference.

It is not because I have learned C and I always think that there is a big difference between struct and class in C ++. The descriptions listed below may be boring, Because struct and class are basically the same thing, needless to say. However, these instructions may help clarify some common misunderstandings about struct and class:
(1) There can be member functions, including constructor, destructor, overload operators, youyuan class, youyuan structure, youyuan function, virtual function, pure virtual function, static functions;
(2) There can be a lot of public/private/protected modifiers in it;
(3) Although this style is no longer promoted, both syntactically can be initialized using braces: A a = {1, 2, 3 }; whether A is A struct or A class, the premise is that the class/structure is simple enough. For example, all members are public and all Members are simple types, no explicitly declared constructor.
(4) A struct can inherit from one class, And a struct can inherit from five classes and five struct at the same time, although this is not good.
(5) If you need to pay attention to the OO principles and style in the design of the class, there is no reason to say that you do not need to pay attention to the design of struct.
(6) Again, all the above statements refer to the C ++ language. As for the situation in C, there is no "class" in C ", c's struct is basically just a syntactic mechanism for packaging data.
---------------------------------------------------------------

Finally, as the two keywords of the language, apart from the above differences in definition types, there is also a little bit: the "class" keyword is also used to define template parameters, just like "typename ". However, the keyword "struct" is not used to define template parameters.

Source: csdn.net

 

Related Article

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.