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)