struct and class in C and C + +

Source: Internet
Author: User
Tags object model
The struct of C + + can be used as class, and the only difference between him and class in C + + is that the Member in class is private by default, and the struct member defaults to public.

c struct can only be a collection of variables, can encapsulate data but can not hide data , and members can not be functions .

About initializing class and struct with curly braces if you define a constructor, you cannot initialize it with curly braces. Struct can be initialized with braces if no constructor is defined. If you do not define a constructor and all member variables are public, you can initialize them with curly braces.

For default access permissions, the default member access in class is private, and struct is public.

With respect to inheritance,class inheritance defaults to private inheritance, and struct inheritance defaults to public inheritance.

Why there are struct in C + +. Many people would think that this is to make C + + and C compatible, then the problem is how to use C + + struct and C compatibility. C + + to exchange the data to the program in the struct in C + + program declared as a, and then according to the semantics of C + + program to inherit or aggregate this struct, and then in the use of this struct can be encapsulated to pass to C or from C to C + +. (Note: In the in-depth exploration of the C + + object model, aggregations, not inheritance, are recommended for use in the spatial distribution of C language because virtual functions are introduced in C + +, and virtual inheritance causes the space of the derived class to include struct space and vptr, VBTL, and Vptr, Where the VBTL is stored is determined by the compiler itself and is not defined in the C + + standard.

Give a simple example from C + + to C:

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 per;

Public

int Getage ()

{return per.age;}

};

extern "C"

void print (person);

#endif

Implementation files for C + +

#include "ModuleA.h"

#include void print (person/per) {printf (' Age:%d,/n ', Per.age), return}//* To be processed C language file */#include extern "C" {#include " ModuleA.h "//or as long as this can be done by compiling}

int main (void) {person/per.age = print (per), return 0;}

To discuss the difference between the structure (struct) and Class (class), what do you think the structure (struct) has to do with the class instead?

---------------------------------------------------------------

First of all, the question should be discussed only syntactically, and if there is a discussion of differences in programming styles between different people, there is no answer to this question. After all, different people have different preferences. Syntactically, in C + + (only C + +). Class and struct do type definitions with a difference of two points: (i) Default inherited permissions. If it is not explicitly specified, the inheritance from class is processed by private inheritance, and the inheritance from struct is processed by public inheritance, and (ii) the default access rights for members. The Member of class defaults to private permissions, and struct defaults to public permissions. In addition to these two points, class and struct are basically one thing. There is no other difference in grammar. Can not because of learning C always feel that even C + + struct and class are very different, the following list of instructions may be more boring, because struct and class is basically the same thing, needless to say. However, these instructions may help to clarify some common misconceptions about struct and class:

(1) can have member functions, including various constructors, destructors, overloaded operators, friend class, friend structure, friend function, virtual function, pure virtual function, static function;

(2) can have a large pile of public/private/protected modifiers in the inside;

(3) Although this style is no longer advocated, both grammars can be initialized using curly braces: A A = {1, 2, 3}; Whether a is a struct or a class, if the class/structure is simple enough, for example, all members are public, all members are simple types, Constructors that are not explicitly declared.

(4) Can carry out complex inheritance and even multiple inheritance, a struct can inherit from a class, vice versa; a struct can inherit 5 classes and 5 struct at the same time, although this is not good.

(5) If the class design needs to pay attention to OO principles and style, then there is no reason to say design struct need not pay attention to.

(6) Again, all of the above is referred to in C + + language, as in the case of C, C is not "class", and C struct fundamentally is just a packaging data grammar mechanism. ---------------------------------------------------------------

To sum up: (1) C + + Class Property default access type is private, and C + + struct default access type is public (2) C + + classes can have inheritance, virtual functions, polymorphism, and C + + struct not. C language struct inside can not have function, can only have variable. C + + extends the struct function in C

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.