Difference between struct in C and C ++

Source: Internet
Author: User

There are two differences.
(1) The difference between the struct of C and the class of C ++.
(2) The difference between struct and class in C ++.
In the first case, struct differs significantly from class. C is a procedural language. struct is only defined as a complex data type. struct can only define member variables, but cannot define member functions (in pure C language, struct cannot define member functions, but can only define variables ). For example, the following C code snippet:
Struct point
{
Int X; // valid
Int y; // valid
Void print ()
{
Printf ("Point Print \ n"); // compilation Error
};
} 9;

Here, a compilation error occurs in the 7th line, and the following error message is displayed: "The function cannot be a member of the Point struct ". Therefore, we can see that in the first case, struct is only a data type and does not use object-oriented programming.

Now let's look at the second case. First, see the following code:

# Include <iostream>
Using namespace STD;
Class cpoint
{
Int X; // The default value is private.
Int y; // The default value is private.
Void print () // The default value is private.
{
Cout <"cpoint: (" <x <"," <Y <")" <Endl;
}
Public:
Cpoint (int x, int y) // constructor, specified as public
{
This-> X = X;
This-> Y = y;
}
Void print1 () // public
{
Cout <"cpoint: (" <x <"," <Y <")" <Endl;
}
};

Struct spoint
{
Int X; // The default value is public.
Int y; // The default value is public.
Void print () // The default value is public.
{
Cout <"spoint: (" <x <"," <Y <")" <Endl;
}
Spoint (int x, int y) // constructor. The default value is public.
{
This-> X = X;
This-> Y = y;
}
PRIVATE:
Void print1 () // Private member function
{
Cout <"spoint: (" <x <"," <Y <")" <Endl;
}
};

Int main (void)
{
Cpoint CPT (1, 2); // call the cpoint constructor with Parameters
Spoint SPT (3, 4); // call the spoint constructor with Parameters

Cout <Cpt. x <"" <Cpt. Y <Endl; // compilation Error
CPT. Print (); // compilation Error
CPT. print1 (); // valid

SPT. Print (); // valid
SPT. print1 (); // compilation Error
Cout <SPT. x <"" <SPT. Y <Endl; // valid

Return 0;
}
In the above program, struct also has constructor and member function. In fact, it also has other features of class, such as inheritance and virtual functions. Therefore, struct in C ++ expands the struct function of C. What are their differences?
All compilation errors in the main function are caused by access to private members. Therefore, we can see thatThe default access permission of members in class is private, while that of struct is public.. What is the difference between struct and class in the inheritance of classes? See the following program:
# Include <iostream>
Using namespace STD;
Class cbase
{
Public:
Void print () // public member function
{
Cout <"cbase: Print ()..." <Endl;
}
};
Class cderived1: cbase // default private inheritance
{
};

Class cderived2: Public cbase // specify public inheritance
{
};

Struct sderived1: cbase // default public inheritance
{
};

Struct sderived2: Private cbase // specify public inheritance
{
};

Int main ()
{
Cderived1 CD1;
Cderived2 CD2;
Sderived1 sd1;
Sderived2 SD2;

Cd1.print (); // compilation Error
Cd2.print ();
Sd1.print ();
Sd2.print (); // compilation Error

Return 0;
}
As you can see, subclass objects that inherit the parent class in private mode cannot access the public members of the parent class. By default, class inheritance is private inheritance, while struct inheritance is public inheritance by default. In addition, in the C ++ 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.
In fact, the keywords reserved by struct in C ++ are to make the C ++ compiler compatible with programs developed by C.
Answer:
The two situations are as follows.
The difference between the struct of C and the class of C ++: struct is defined as a complex data type and cannot be used in object-oriented programming.
The difference between struct and class in C ++: For the access permissions and inheritance methods of members, the default class is private, while the struct is public. Class can also be used to indicate the template type, but not struct.

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.