C ++ day08 Study Notes

Source: Internet
Author: User

1. sorting functions

 
VoidOrder (Int* P1,Int*P2 ){If(* P1> *P2 ){IntTemp = *P1;* P1 = *P2;* P2 =Temp ;}}
  void  sort ( int  * P,  int  Len,  int  perlen,  void  (* porder) ( void  *,  void  * ) {  char  * PT = ( char  * ) P;   for  ( int  I =  0 ; I 
  
   ) { 
    for  (
    int  J = I; j 
   
    ) {porder (PT  + I * perlen, Pt + J * 
     perlen) ;}} 
   
  

(1) enter the input parameter int *-> void * (void * can store any type of address, which can be generic)
(2) indicates the I-th element of the array. You must know the size of each element of the array in advance (input parameters)
I * perlen => when the pointer type is unknown, it indicates the number of bytes that should be crossed in a step.
P + I * perlen => indicates the address of the I-th element.
(3) Specific sorting methods should be available for specific arrays.
Call a function. If you do not know the function name, you can call it through the function pointer.

View code

========================================================== ============ Sort. h ========================================================== ============ Void Sort ( Void * P, Int Len,Int  Perlen,  Void (* Porder )( Void *, Void * )); ========================================================== ============ Sort. CC ========================================================== ============ Void Sort ( Void * P, Int Len, Int  Perlen, Void (* Porder )( Void *, Void * )){  Char * PT = ( Char * ) P;  For ( Int I = 0 ; I <Len; I ++ ){  For ( Int J = I; j <Len; j ++) {Porder (pt + I * perlen, Pt + J * Perlen );}}} 
View code

========================================================== ============ Main. CC ========================================================== ============ # Include <Iostream> # Include  "  Sort. h  "      Using   Namespace STD;  Void Orderint ( Void * P1, Void * P2 ){  Int * T1 = ( Int * ) P1;  Int * T2 = ( Int * ) P2;  If (* T1> * T2 ){  Int Temp = * T1; * T1 = * T2; * T2 = Temp ;}}  Struct  Person {  Char Name [ 40  ];  Int  Age;  Int  ID ;};  Void Orderbyage ( Void * P1, Void * P2) {person * T1 = (person * ) P1; person * T2 = (person * ) P2;  If (T1-> age> T2-> Age) {person t = * T1; * T1 = * T2; * T2 = T ;}}  Void Orderbyid ( Void * P1,Void * P2) {person * T1 = (person * ) P1; person * T2 = (person * ) P2;  If (T1-> ID> T2-> ID) {person t = * T1; * T1 = * T2; * T2 = T ;}}  Void Orderbyname ( Void * P1, Void * P2) {person * T1 = (person * ) P1; person * T2 = (person * ) P2;  If (Strcmp (T1-> name, T2-> name)> 0  ) {Person t = * T1; * T1 = * T2; * T2 = T ;}}  Int  Main (){  Int IA [] = { 3 , 1 , 6 , 3 , 6 , 8 , 3 , 468 , 89  }; Sort (IA,  9 , Sizeof ( Int  ), Orderint ); For ( Int I = 0 ; I < 9 ; I ++ ) {Cout <Ia [I] < "   "  ;} Cout < Endl; person pers [  3  ]; Pers [  0 ]. ID = 1 ; Pers [  0 ]. Age = 29  ; Strcpy (Pers [  0 ]. Name, "  Liucy  "  ); Pers [  1 ]. ID = 2  ; Pers [  1 ]. Age = 28  ; Strcpy (Pers [ 1 ]. Name, "  Huxinzhe  "  ); Pers [  2 ]. ID = 3  ; Pers [  2 ]. Age = 26  ; Strcpy (Pers [  2 ]. Name, "  Xuehailu  " ); Sort (Pers,  3 , Sizeof  (Person), orderbyage );  For ( Int I = 0 ; I < 3 ; I ++ ) {Cout <Pers [I]. Name < "  ,  " <Pers [I]. age < " ,  "  ; Cout <Pers [I]. ID < Endl;} Sort (Pers,  3 , Sizeof  (Person), orderbyid );  For ( Int I = 0 ; I < 3 ; I ++ ) {Cout <Pers [I]. Name < " ,  " <Pers [I]. age < "  ,  "  ; Cout <Pers [I]. ID < Endl;} Sort (Pers,  3 , Sizeof  (Person), orderbyname );  For ( Int I = 0 ; I < 3 ; I ++ ) {Cout <Pers [I]. Name < "  ,  " <Pers [I]. age < "  ,  "  ; Cout <Pers [I]. ID < Endl ;}  Return   0  ;} 

2. Object-oriented
Encapsulation: Object Representation
Inheritance: BetterCodeReuse
Polymorphism

Object composition: attribute member variables
Behavior Functions

Process-oriented representation:
Data and function separation, loose relationship
Encapsulation: encapsulates data and functions to ensure data is dedicated.

Global functions: For functions outside the class, to use member variables, they must be passed in through parameters.
Member functions: You can directly use the member variables of your class within the class.

Initialization of class variables:

Person P; strcpy (P. Name,"Liucy"); P. Age=23; P. Speak ();

To use member variables and member functions, you must use the class object.

Public keyword, indicating that it can be used elsewhere
The default value is private and cannot be used in the main function.

Member variables and member functions depend on class objects (instances)

Type is the description of the object
The object is a type instance.

The object's own member functions access its own member variables

What is a class? What makes a class?
How to use classes? And the relationship between classes and objects?
Who owns the member variables and member functions?

Object-oriented Method WritingProgram
(1) first write a class to describe the object
Use variables to represent attributes and functions to represent Behavior
(2) call a function
Creates a Class Object and calls a function through an object.

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.