class summary

Class is the basis for object-oriented programming to realize information encapsulation, and it is a user-defined type.

one type

1. Definition of a class: A class is an abstract and unified description of a group of objects with the same properties and behaviors . Properties are represented as data, called data members of the class. Behavior is implemented through functions, called member functions.

C++ class definition format:

class class name
{
   public:
           public data members and member functions;
   protected:
           protect data members and member functions;
   private:
           private data members and member functions;
}; //The semicolon cannot be omitted;

   The specific implementation of each member function;

Class member access control list:

public: declares public members. Is the external interface of the class, visible inside and outside the class.

protected: Declare a protected member. Visible in the class and its derived classes.

private: Declare private members. Visible only within the class, not outside the class or in derived classes.

2. Member functions

The implementation of simple member functions can be defined in the class, the complex or general member functions are declared as function prototypes, and the member functions are implemented outside the class.

Member functions are defined outside the class:

Return value type class name::member function name (parameter list) //The scope specifier is composed of two colons, which are used to identify the members of what class;
{
          function body
}

The role of member functions:

  • Manipulate data members, including accessing and modifying data members
  • It is used to cooperate with different objects to operate, which is called passing messages. Member functions cooperate with other objects through parameters.

3. Object:

An object is an active entity of a class, an instance or entity of a class. After declaring objects of a class, the compiler allocates memory for each object's data members. Objects do not have copies of member functions, and class member functions can be called by objects.

The object definition format is as follows:

class name object name 1 , object name 2 , ... , object name n ;

Note: Objects of a class can only be defined after the class has been defined.

4. Access to class members:

Working with an object includes accessing the object's data members and calling member functions.

Access to object members includes:

  • Dot access form: object name. public member
  • Pointer access form: "->"   object pointer variable name -> public member

2. Inline functions

1. The role of inline functions: reduce the time overhead of frequently calling small subroutines,

2. Inline function declaration: inline function prototype

Notice:

Inline functions are declared only once in the function prototype. It is suitable for small functions with only 1 to 5 lines. It cannot contain complex structural control statements such as while and switch, and cannot be called recursively. The .keyword inline must be placed with the body of the function definition to make the function inline, just putting inline before the function declaration has no effect.

inline void Foo(int x, int y); // inline is only placed with function declarations, it cannot be an inline function;
inline int Coord::getx() { return x;} //  inline is placed with the body of the function definition and can be an inline function;

Three: member functions can be overloaded

ØFunction overloading: a set of functions with the same function name but different parameters (different types, or different numbers).
Ø The compiler generates call matching according to the type and number of different parameters
Ø Function overloading is used to handle similar tasks of different data types 
eg:
(1) The number of parameters is the same but the types are different
#include<iostream>
using namespace std ;
int  abs ( int  a ) ;          
double  abs ( double  f ) ;
int main()
  {  cout << abs ( -5 )  << endl ;
     cout << abs ( -7.8 ) << endl ;
  }
int  abs ( int  a )
  {  return  a <  0  ?  -a  :  a ;  }
double  abs ( double  f )
  {  return  f < 0  ?  -f  :  f ;  }

(2) The number of parameters is different

#include<iostream>
using namespace std ;
int  max ( int a ,  int b ) ;
int  max ( int a ,  int b,  int c ) ;
int main()
 { cout << max ( 5, 3 ) << endl ;
   cout << max (4, 8, 2 ) << endl ;
 }
int  max ( int a ,  int  b )
 { return a > b ? a : b ;  }
int  max ( int a ,  int  b,  int  c )
 {  int  t ;
    t = max ( a ,  b ) ;
    return  max ( t ,  c ) ;
 }


Guess you like

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