C + + Learning the difference between class 13 classes and struct structs

Source: Internet
Author: User

C + + retains the struct of the language and expands it. In the C language, a struct can contain only data members and cannot contain member functions. In C + +, a struct is similar to class, and can contain both a data member and a member function.

In C + +, the C language of the struct keyword is not discarded, its significance lies in the C language Program developers have a sense of belonging, and can make the C + + compiler compatible with the previous development of the C language project.

Examples of C + + structs:

#include <iostream>using namespacestd;structstudent{Char*name; floatscore; Student (Char*,float); voidsay ();}; Student::student (Char*name,floatscore) {     This->name =name;  This->score =score;}voidStudent::say () {cout<<name<<"the results are"<<score<<Endl;}intMain () {Student stu ("Xiao Ming",95.5f);    Stu.say (); return 0;}

The program compiles and runs normally, stating that the struct's default member is the public property, otherwise the member function cannot be accessed through the object. If you replace the struct keyword with a class, an error is compiled.

C + + Learning the difference between class 13 classes and struct structs

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.