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 that the default access permission for members in the class is private, while that for 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.

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.