The basic of C/O program (vi) Object-oriented

Source: Internet
Author: User

  1. Properties of the class
    • Abstraction, encapsulation , inheritance , polymorphism , overloading
  2. Class and struct differences
    • A struct in C can only contain data members and cannot have member functions, which belong to complex data structures.
    • struct member access rights in C + + default to public, inheritance defaults to public, and class can be used in template declarations. Template <class/typename t>
  3. Initialization list of constructors
    • the order and member declarations are in the same order .
    • the scenario that must be applied : Initialize the const, reference, or call the non-default constructor of the parent class. B (int x,int y): A (x)
  4.  class   a{ public  : A ( int  k): J (k), I (j) //  I first. J Follow   { 
         
          // 
           code here  
          }
           private  :  int     I;       int   J; The difference between  
         

      static data members and global variables:

    • namespace differs to avoid conflicts. The
    • supports information encapsulation, which belongs to member variables and can be set to private.
    • Note: static and const data members are initialized outside the class, and static constants data members are initialized within the class or out-of-class. Static constants must be in the initialization list. Non-static very much in the initialization list or constructor.
  5. member functions for empty classes
    • Default constructors and copy constructors
    • Destructors (can not be overloaded, without parameters)
    • Assignment function (=)
    • Value Arithmetic (&)
  6. Explicit-Modified constructors
    • An implicit conversion is supported for constructors with only one parameter (the rest is the default parameter).  Test (int i) {...}; Test a = 1;
    • Explicit can turn off this implicit conversion. Only Test A (1) can be passed;
  7. The relationship between destructors, constructors and virtual functions
    • Constructors cannot be written as virtual functions, virtual functions require virtual pointers and virtual tables, and these are the constructors that are responsible for implementing them.
    • The destructor needs to be written as a virtual function, which guarantees the normal invocation of the sub-class destructor. base* a = new Derived (); Delete A;
  8. Copy constructor
    • Concept: A special constructor that builds and initializes new objects based on other objects of the same kind.
    • Call Scenario: pass by value, return by value, initialize with object (Test B; Test A = B;).
    • Default shallow copy . Deep copy vs Shallow Copy, when the object references external content, if the new old points to the same content as shallow copy, otherwise deep copy.
    • Handling of inheriting classes: Call the copy constructor of the base class.
    • Differences from assignment functions: 1) Replication is the initialization object, and the assignment is to modify the original object. 2) replication is initialized, the assignment first checks whether two objects are consistent, returns consistently, is inconsistent, releases the memory of the Lvalue object, and re-constructs it.
  9. //copy ConstructorA; A b=A;//Assignment FunctionA; A B;a=b;//An example of an assignment functionstring&string::operator= (Const string&Other ) {  //Check if consistent  if( This= = &Other )return* This; //Freeing Memory  Delete[] m_string; //ReassignM_string =New Char[Strlen (other.m_string) +1];  strcpy (m_string, other.m_string); return* This; }

    Temporary objects

      • Scenario: Parameters are passed by value, and return values are passed by value.
  10. C + + function overloading

    • Why it works: C + + naming conventions, including the number of parameters and parameter type information.
    • Note: The return value is different, and the const is not sufficient to differentiate the function.
    • The difference between overloading and overriding: 1) overloading different versions of a function implementation, with the same method name and a different argument list " compile-time ." 2) overrides are subclasses that override the virtual or abstract functions of the parent class, the same method name and argument list, and the return value " runtime ."
    • Note: Hide (redefine): Subclasses redefine a function with the same name as the parent class (the argument list can be different), hiding a function with the same name as the parent class.

The basic of C/O program (vi) Object-oriented

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.