1 /*2 class is a user-defined data type,3 and this type of data has a certain behavioral capability,4 that is, the method described in the class. 5 Generally speaking,6 the definition of a class contains two parts of the content,7 The first is the property of the class,8 the other part is the method it has. 9 in the "Human" category,Ten everyone has his or her name, age, date of birth, weight, etc. One as a part of human nature, in addition, people can eat, sleep, walk, talk, etc. belong to human behavior. A eating, sleeping, walking, talking is also called the base class - in humans there are students, white collars, etc., called subclasses or derived classes - */ the#include <iostream> - using namespacestd; - - class Point + { - /* + Public and private are keywords for attribute/method restrictions, A private means that the part is private, at cannot be accessed or invoked externally, - can only be accessed within this category; - while public represents the properties and methods that are exposed, - The outside world can be accessed or invoked directly. - Generally, the attribute members of a class should be set to private, - Public is reserved only for function interfaces that are used by the outside world to invoke. in However, this is not a mandatory requirement and can be adjusted as necessary; - */ to Public://Public + voidSetPoint (intXinty) - { theXpos =x; *Ypos =y; $ }Panax Notoginseng voidPrintpoint () - { thecout <<"x="<< Xpos <<Endl; +cout <<"y="<< Ypos <<Endl; A } the Private://Private + intXpos; - intYpos; $ $};//the end part of the semicolon cannot be omitted. - - intMain () the { - Point A;WuyiA.setpoint (Ten, -); the a.printpoint (); - GetChar (); Wu return 0; -}
defining member functions outside of a class
1 //defining member functions outside of a class is implemented by declaring within a class and then outside the class through the scope operator::2#include <iostream>3 using namespacestd;4 class Point5 {6 Public :7 voidSetPoint (intXinty);8 voidprintpoint ();9 Private:Ten intxpos; One intypos; A};//class End - voidPoint::setpoint (intXintY//Implementing a function through the scope operator ':: ' - { the /* - according to the SetPoint member function, - The form declared within a class is void setpoint (int x, int y); - then the function header should be void Point::setpoint (int x, int y) when it is defined outside the class, + its return type, member function name, and parameter list are all identical to those declared within the class. - */ +Xpos =x; AYpos =y; at } - voidPoint::p rintpoint ()//Implementing a function through the scope operator ':: ' - { -cout <<"x="<< xpos <<Endl; -cout <<"y="<< ypos <<Endl; - } in intMain () - { to Point A; + intA1, B; -CIN >>A1; theCIN >>b; * A.setpoint (A1, b); $ a.printpoint ();Panax Notoginseng GetChar (); - return 0; the}
"C + +" class