C +-Class (Classes)

Source: Internet
Author: User

1. Class Definition: C + + uses the keyword class to define a class with the following basic form:

1 class class name 2     {3         public:4             // common behavior or Properties 5 6         Private : 7             // public behavior or properties 8     };
View Code

Description
①. Class names need to follow the general naming conventions;
②. Public and private is the attribute/method restriction of the keyword, private means that the part of the content is private, cannot be accessed or invoked externally, can only be accessed within this class; Public represents the properties and methods that are exposed and can be accessed or invoked directly by the outside world.
In general, the property members of the class should be set to private, public only to those used by the outside to invoke the function interface, but this is not mandatory, can be adjusted as necessary;
③. The semicolon of the ending section cannot be omitted.

Cases:

#include <iostream>using namespacestd;classpoint{ Public :        voidSetPoint (intXinty) {xpost=x; Ypost=y; }        voidPrintpoint () {cout<<"============================================"<<Endl; cout<<"x ="<< Xpost <<Endl; cout<<"y ="<< Ypost <<Endl; }    Private :        intXpost; intypost;};intMain () {cout<<"============================================"<<Endl;        Point M; //create an object point m with a well-defined classM.setpoint (Ten, -);//set the x, Y value of the M pointM.printpoint ();//Output M-point informationcout <<"============================================"<<Endl;  while(1)    {              }    return 0;}
View Code
#include <iostream>using namespacestd;classpoint{ Public :        voidSetPoint (intXinty); voidPrintpoint (); Private :        intXpost; intypost;};voidPoint::setpoint (intXinty) {Xpost=x; Ypost=y;}voidPoint ::P rintpoint () {cout<<"============================================"<<Endl; cout<<"x ="<< Xpost <<Endl; cout<<"y ="<< Ypost <<Endl;}intMain () {cout<<"============================================"<<Endl;        Point M; //create an object point m with a well-defined classM.setpoint (Ten, -);//set the x, Y value of the M pointM.printpoint ();//Output M-point informationcout <<"============================================"<<Endl;  while(1)    {              }    return 0;}
View Code

C +-Class (Classes)

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.