C ++ getting started learning notes - class and object

zhaozj2021-02-11  207

Description of objects and classes in C

I

*** class, how rich artistic words! I want to think that everything in the real world is exciting. Mastering it also masters the true meaning of the program art, and also masters the time and space of the reality and the illusory time! --- Skyala *** Skyala, please remember the amazing similar laws below: people (class) -> parents (objects) -> children (inherit, multiple inheritance) -> children (polymorphism) --- Wow, My god! What perfect curve! Object-Oriented Program Design Summary: Send messages to objects. Can reduce the class established by the parameters during the function transfer process, which is useful! First, the class talk 1. Definition: Class: General description of the properties and behaviors of entities. Skyala Note: Too perfect concept is that the definition is not coming is the basic unit in the program design. It can be said to be a new data type, so there are so many object-oriented programming languages. Too flexible class data section: Data Member, its initialization cannot declare their local initialization in the defined body, and should be initialized by the constructor of the class or to assign a function to the value of the value. It is best to keep all data members of the class all private, provide functions to operate these data, hide the implementation of the class, reduce errors, and improve modifiers. External unable access to private members can only use member functions. Remember MAIN is also a function section of an external class: member function (Member function), accessing the class public member function outside, but also used to access the operator object: a large number of items generated by the class, called objects "Save an area" object is the final instantiation of the class. The object initialization TIME T (23) is in the object name, the category of the crackers before the semicolon is the natural extension of the structure in C. 2. Class Members Access Operators: Polk Dots: Access P-> T4 by the P.T Arrow through the object name or reference to the object: Access P-> T4 by pointer to the object. Constructor and the destructor a. The member function called the class "Constructor", used to initialize the data of the class object. Constructor: No return type - So there is no return value, overloaded to constructor provides the default parameter value even if the parameter value is provided even when the constructor is called, it is ensured that it is initialized by the default parameter. All parameters are also the default constructor. Do not call other functions in the constructor (although this is allowed) but uses a data member before initialization, it may cause an error B. Members with the same name but add "~" (by bit to reflect the operator) Function called "Destructor", system reclaiming memory before memory: no return type - so there is no return value, no overload - so there is only one c in the class. Establishment and The corresponding constructor and destructor are automatically invoked, and the call sequence of the two is exactly the different objects: global scope (program termination), local object (declared object block), static STATIC local object (program termination But before the global) 5. Interfaces and implementations definitions: including declarations of data members and member functions, the membership function declare that the function is also the prototype of the function. To use a semicolon end class definition interface: The method of the data member provided by the class's public function (functions in the access spectrum public), this function is also called the interface to put the statement of the class into a header file. The public interface of the composition is implemented: the defined section of the member function of the class, externally defined to use the binocular operator ":" to use the member function, put the definition of the member function into a source file, thus constitute the class Implementation - Information hidden users can access the interface interface, they cannot access the achievements of the class: As long as the interface is not changed, the implementation has changed as long as the code is used to recompile and do not need to be changed.

** Basic Principles of Software Engineering: Minimum Access Principles: (Of course, in addition to small functions, all member functions should be defined in vitro definitions in addition to small functions, facilitating interfaces and Implement separation. Can encourage independent software vendors (ISV) libraries as goods --- Skyala: All industry has public categories, uniformly stratetable software libraries, how interesting is 6. The internal definition of a member function will automatically become inline function (inline), external to Inline. 7. Preventing multiple items of header file Time.h from head file name, Time is assumed, "_" instead ".", Which uses the pre-processing instruction #ifndef Time_h # define Time_H .. # ENDIF8. Public (private), Private Access Functions and Tools Functions Set Setup Functions to check. GET acquires the status of the function. It not only helps protects the integrity of data, but also hides the customer's implementation to the customer 10. Don't let public member functions return a very quite reference (or pointer) for private data members (or pointers) as follows: public: int & bagset (); int Time :: Badset (INTHH) {Return Hour;} T.badset (12) = 74; / / Returned reference function calls can be left value. 11. "=" Can give a class assignment to another class of the same type, by applying the application by a member copying the default assignment method: leapyear.h, leapyear.cpp Note: This is the calculation of a leap year, but I am not satisfied, it is a bit Honest.

Compilation Environment: Window2000 VC6.0 # IFNDEF LEAPYEAR_H / / Pretreatment instructions prevents multiple identical header files #define leapyear_h // "-" instead "." Class leapyear {public: // can provide the default parameters to constructor, None return value without return type Leepyear (int, int, int); ~ leapyear (); int getYear (); void setMonth (int); int getDay (); void print () const; // constist member function private: int Iyear; intimonth; // int iDay; pay attention to the size of the error Int iDay; int checkday (int); // Tool function}; // Class definition end #ENDIF #include #include #include "Leapyear.h" Using namespace std; // member initialization Leepyear :: Leepyear (int IY, INT IM, INT ID) {IMONTH = IM; IYEAR = IY; // iDay = CheckDay (INT ID); // Function call Format error iday = checkday (ID); cout << "constructor Sucessed!" << endl;} leapyear :: ~ leapyear () {cout << "Object memory release success!" << Endl;} int next leapyear :: getYear () {RETURN IYEAR;} Void Leapyear :: SETMONTH (INT IM) {IMONTH = (IM <12 && IM> 1) IM: 1;} int next leapyear :: getday () {return iDay;} int next leaiar :: Checkday (int TestDay) {static int daypermonth [= {0, 31, 28, 31, 30, 31, 30, 31}; if (IMONTH! = 2) {IF (Testday> 0 && testday <= daypermonth [iMonth]) Return D Aypermonth [IMONTH];} else {// leap year calculation formula INT days = (IYear% 400 == 0 || (IYear% 4 == 0 && Iyear% 100! = 0)? 29: 28); if (TestDay> 0 && testday <= days) Return Days;} Cout << "Today Day" << TestDay << "Invalid Set to Day / N"; Return 1;} void Leepyear :: Print () Const {COUT << iodiear < <'/' << IMONTH << '/' << iDay;} void main () {Leepyear Lyday (1994, 2, 4); // lyday.setday (4); lyday.print (); cout << "Year" << lyday.getyear () << Endl; cout <<

"Month" << lyday.getday () << endl;} ii

*** Renewood, this life does not stay - Skyala *** Skyala, please remember the amazing similar laws below: people (class) -> parents (object) -> children (inherit, multiple inheritance) -> children (Polymorphism) --- Wow, my god! What perfect curve! Object-Oriented Program Design Summary: Send messages to objects. Can reduce the class established by the parameters during the function transfer process, which is useful! From this part of the note, add the declaration function, and add a small program (pseudo code) after each definition section (pseudo code) to explain, convenient. *************************************statement************ ***************** 1. Const object and const function 2. Friends (Friend) class and friend function 3. Static data member, static data function and static Object 4. Module (Template) class 5. Other throops (composite, inclusive, category ...) * Information hide: Definition of Abstract Data Types (ADT): Representation Data and operation C for data emphasizes the importance of data * char * TEMP = New char [stlen (fristname) 1]; askERT (TEMP! = 0); // Check whether memory allocation is successful 6. Application example a Stack Module class (TSTACK.CPP) b. Class Class (Queue Class) C. List class 7. Question: This pointer does not understand why not use const declared static member functions ****** *****************************definition******************** ********* 1. Const object and const function * const objects cannot invoke non-Const functions, can't modify the const object const object and the const variable cannot be assigned, and must be initialized.

In the constructor, you can initialize the initial value of the member. Functions and destructuring functions will definitely modify the object contradiction, do not declare the keyword const, in order to correctly initialize the object, allow these two functions to modify the object * Remember: When a class includes a CONST object, you must give Constructor provides member initialization value class time {time (int = 0, int = 0); Void setTime (int = 0, int = 0, int = 0); // Default constructor int Gethour () const {return hour; } // Const member function private: int hour; int Time const; // const data member, you want to assign a value to initialize} Time :: Time (int S, INT M): Time (M) // Member Initialization value {HOUR = i; time = m; // attempt to assign a value, is an error // data initialization} void main () {const time t (12, 0, 0); // constab to be assigned must initialize T.Gethour (); / / Correctly, only const member functions can access const objects T.SetTime (); // error, non-const function} 2. Friends class and friend functions are defined outside the class scope, but it has the right Accessing the private member and protected members of the class in the class definition, add Friend before the function prototype or class, which can be placed anywhere in the class, generally in the start of the class definition is a "give" relationship, ie B is A friend, B visible data member and member function class count {friend void setx (count &, int); // Solving friend class public: private: int x; // Private data member} // Seeing this, there is no class scope symbol using class, it is a function outside the class, distinguishes Void SetX (Count & Int Val) {cx = val; // Friends function, so you can modify private data } void main () {count c; setx (c, 8); // UFF function sets the value of x, there is no use domain, which is a function outside the class effect} 3. Static data member Objects of functions and objects, usually have separate copies of all data members of this class, in order to share objects A copy introduces a static data member. Save memory. It seems that global variables but only class scope * public static data members can be used to access Time :: Count (preferably without object when there is no object), and can be used to access t.count, also available. Static member functions can only be used in the second way * Private and protected can only be accessed with public static member functions. When a member function does not access a non-static member of the class, declare that the static function is conducive to save memory * Even if there is no class object, static data member, the function also exists and can be used.

Public: static int getCount (); // Static function private: static int count; // static member int Time :: getCount (return count;) 4. Template class module class requires one or more types of parameters, Therefore, the template class also called the parameter data type declaration: Template , the member function outside the template class must start with this form (how long your head - Skyaala) Template int Stack :: POP () {} class name: stack , instantiation: stack floatstack, stack is a template class object for assumptions multiple parameters: Template 5. Composite, Package container Class, Class, Class, and Other * Composition: A class puts another as its own member, member object is a member object established before including their objects, and the constructor is automatically called automatically, but If there is no default constructor, the error * member initialization value can avoid initializing the member object twice (call the default constructor and "set" function) Time :: Time (int Hour, int minute, int 2): Minute Application: Compilation Environment: Window2000 VC6.0A. stack.cpp stack class #1ndef tstack_h # define tstack_htemplate Class Stack {public: stack (int = 10); // Constructor, the stack default size is 10 ~ stack (); // Destructor INT POP (ELMTYPE &); // Fact INT Push (Const ElemType &); // Find ISEMPTY () const {return _top == -1;} // Stack empty, return -1INT isfull () const {return _top == _size - 1;} // stack, return to the top size private: int _size; // Stack energy to accommodate the number of elements INT _TOP; / / Top Element ELE MTYPE * _STACKPTR; / / Pointer pointer to the stack}; template stack :: stack (int S) {_ size = S; _top = -1; // Empty stack _StackPtr = new elemType [_size] } template stack :: ~ stack () {delete [] _stackptr;} // is pressed into an element, such as successful returning 1, failed to return 0Template int stack :: Push (incst elemType & item) {i (! isfull ()) {_ stackptr [ _ TOP] = item; // If the stack is not full, the top is added to add 1 returnit 1; // Success} return 0; // Final Failure} // Find Template Int Stack :: POP (ElemType &

PopValue) {if (! iSempty ()) {popValue = _StackPtr [_top--]; // If the stack does not assign a value after the vacation is reduced by 1 Return 1;} returnite 0;} # Endif #include #include void main () {stack floatstack (5); // template instantiation int FS = 1; cout << "pushing / n"; while (floatstack.push (fs)) {cout < charstack (10); char CS = 'a'; cout << "pushing / n"; while (charstack.push (cs)) {cout <

转载请注明原文地址:https://www.9cbs.com/read-4287.html

New Post(0)