10. Objects and Classes

Source: Internet
Author: User

10-Objects and classes
  1. Programmers use private member functions to handle implementation details that are not part of the public interface
  2. inline function definition section plus inline, declaration part does not need
  3. In OOP, the call member function is called the send message "send messages"
  4. The parameter name of the custom constructor cannot be the same as the class member name, in order to make the parameter name more intuitive, usually prefix the data member name m_, or add suffix _, to avoid confusion
  5. The compiler automatically provides a default constructor when no constructor is defined, but once a non-default constructor is defined, a default constructor must be defined manually
  6. There are two ways to define a default constructor, one with no arguments and one with arguments, but all parameters have default values. In general, all parameters should be initialized with default values, regardless of parameters. The default constructor that is not a parameter should also give the data member default value within the function body. Two definition methods cannot be used at the same time

             //构造函数隐式与显示调用         class food = class( "Ok",30,4.76 );         class food( "Ok",30,4.76 );         class *ptr = new class( "Ok",30,4.76 );         //默认构造函数隐式与显示调用         class good;         class good = class( );         class *pre = new class;
  7. Destructors without parameters
  8. If a constructor uses new, the destructor must call the delete
  9. As long as the class method does not modify the calling object, it should be declared as const

             void show( ) const;
  10. This pointer

            const Stock & Stock :: topval ( const Stock & s ) const        {            if (s.total_val > total_val)                return s;            else                return *this;         }    //第一个const表示返回类型为const引用    //第二个const表示函数不能修改显示调用的对象    //第三个const表示函数不能修改隐式调用的对象
  11. Object array

            const int STKS = 3;        Stock food [STKS] = {            Stock("NanoSmart", 12, 20.0),            Stock( ),            Stock("Monolithic Obelisks", 130, 3.25),        };        //访问        food[0], food[1], food[2]
  12. To use a constant scoped to a class, that is, constants are shared by all objects.
    Defining a const variable directly in a data member is not feasible and is not instantiated at this time
    There are two ways, one: enumeration, and two: static data types

            private:            enum{ ok = 100 };        private:            static const int ok = 100;

10. Objects and Classes

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.