C++ study diary and experience (continuously updated)

* No parentheses are allowed when creating an object with a no-argument constructor

   Circle circle1 (); is wrong

  CIrcle circle1 ; is correct

* Avoid multiple includes

  If the header file of a class is defined multiple times in head files or source files , the following methods can be used:

  #ifndef CIRCLE_H

  #define CIRCLE_H

    {

  }

  #endif

* Inline functions improve function execution efficiency (for short functions)

  inline int number;

  Or write the function definition directly in the class class

*get and set functions are to ensure data field encapsulation

  The Get function is an accessor:

  double getRadius();

  When the return type is bool : Naming convention: bool isPropertyName ();

  The Set function is a mutator

  void setRadius(double);

* Variables can be initialized at declaration time, but a class member cannot be initialized at declaration time

*string s("welcome to c++); Equivalent to string s = "welcome to c++"; Equivalent to char s1[] = "welcome to c++";//The former is more efficient

*string s;// Create an empty string

* When the parameter is an object, it is actually passed by value, and the content of the object is copied to the parameter of the function

   Pass-by-reference actually means: the parameter of the function is an alias of the object, it is better to use pass-by-reference

The *static keyword means that no matter which object of the class can be called, and all call the same member

  Creating a circle1 object increments NumberOfObject by one, and creating a circle2 object increments NumberOfObject by one

  Static variables and static functions can be accessed without creating an object

  // You should use ClassName::functionName(arguments) to call static functions, and use ClassName::staticVariable to access static variables, which can improve program readability

  When to declare static: variables and functions should be declared static if they do not depend on any class object

*const keyword: means read-only member function,

  Syntax: put the const keyword at the end of the function header: int getNumber()const;

When defining, add constA   after the function header

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325120137&siteId=291194637