Study Notes --C ++

C-section

  

A pointer and secondary pointer, the function

    When the pointer function parameters, as well as the specific meaning of the pointer, and the pointer ++ - Problem

    Two pointer as noted problems when modifying a variable, and the type of matching pointer

    Application of the pointer in the string

    Open up and destroy pointer memory space

    The difference between an array of pointers to the pointer array

    Function pointer (not quite the test, you can understand)

  The basic flow control statements such as if, while, do-while, switch, for, etc.

  Structures, arrays, two-dimensional array traversal and storage, and a two-dimensional array of indicators showing how instead of two-dimensional array pointer

C ++ part

  namespace

  const strengthen

    const int * p pointer to the contents of memory space can not be modified, but the pointers can be modified

    int * const p pointer contents can be modified, a pointer can not be modified

    const int * const * p pointer and a pointer to the content can not be modified

    const int & p is the same as above a meaning, just different wording

  Quote

    Use references, references significance as a function of the parameters, references (to understand), const reference

    Int * & p meaning of

  Overloading, rewrite and redefine the difference

  Classes and Objects

    Encapsulation and access control

      public

      private

      protected

    The constructor and destructor

      The constructor and destructor call time

      Deep copy and shallow copy

      Copy constructor (by far the most important! There are four kinds of copy constructor condition occurs)

      In succession, the sequence calling the constructor and destructor (love test)

        Construction order: Fathers class, then variable, then himself

        Destructor sequence: their first, and then the variables, the parent class

    Establish and release objects (new and delete, malloc and free)

    Static variables and use a static member function (static)

    this pointer (Learn)

    Friend (Learn, was more widely used in heavy duty operator)

    Operator overloading (write programs important questions, read the program title is not important)

      Monocular

      Binocular

      Input and output streams

    Inheritance and derivation

      Inheritance: private, public, protected (third base with less than)

      C ++ inheritance static in the process of (often test calculation)

      Inherited members of the same name and function approach

      Bring multiple inheritance ambiguity problems, and solutions (virtual virtual inheritance)

    Polymorphism (important)

      Polymorphic three conditions

        Have inherited (inherited between parent class to subclass)

        Have a virtual rewrite function (there are keyword virtual)

        Have a parent pointer (parent class reference) point subclass object

    Exception Handling

    Bits and pieces of non-priority issues

      Between the control input of the output stream, such as the output to several decimal places, reserve several significant figures, the left and right alignment (can be used to replace printf)

      Read and write files

Common questions

  Read program about eight kinds of problems certainly

  Then processing the digital input and output between the two-dimensional array array traversal

  String processing involving pointer array or char type, there are several common standard functions such as strcpy, strcat, strstr, strchr, strcmp etc.

  A class with subclasses of the parent class, and then outputs it to call you configured performed destructor

  Static will not be reset to between some of the classes and functions, belonging to a global shared, let you back static variable output value

  Copy constructor, there are four cases will be triggered

  Exception Handling (throws), relatively few exam

  Quote

  Polymorphism

  Write a program will increase the overload problem, friend, STL


Global variables (a departure from the scope of the concept of point of view):

  Variables defined outside the function. Scope is defined from the position of source file to start over. When the same name and local variables, local variables in the function range of the global variable is masked. There is a global variable static storage area, during program execution always occupy storage unit, not open unit only when needed, release the finished space.

With a static local variable declared static:

  Does not disappear at the end of the function call, to retain the original value, the memory cell that is occupied is not released, still retains the value at the end of the last call when the call to the next. Static local variables are stored in static memory, the program is not released during the entire operation. Given the initial value for the static local variable is done at compile time, initial value only once, after each call functions are no longer re-initial value leaving only the value of the function at the end of the first call. If local static variable is not defined initial value, automatically assigned an initial value of 0 or a null character compile time. Other functions can not reference a static local variable, that it is not visible in other functions.

Array initialization:

  Only to part of the element can be assigned as: a [10] = {1,2,3,4,5}. The remaining elements defaults to 0

  When the assignment to all elements, may not be specified array length, such as: a [] = {1,2,3,4,5}, the system automatically defines the length of the array 5 a.

  a [2] [3] = {{1,2,3}, {4,5,6}} assignment rows or a [2] [3] = {1,2,3,4,5,6} automatically in sequential order initial value of each element.

  a [2] [3] = {{1}, {4}} assign only the first column, the default is 0 the other elements.

  a [2] [3] = {{1}, {2,3}}, only the first row, first column, second row of the second element of the three initial value, 0 is other default.

  a [2] [3] = {{1}, {}}, a line may be omitted.

  a [] [3] = {1,2,3,4,5,6}, the number of rows can not be omitted, the number of columns thereof is omitted.

  One-dimensional array as a parameter, with the array [] array is represented in the form of such a one-dimensional array were to accept the transmitted address arguments, [] no practical effect, the number of elements can not write. However, when the two-dimensional array as a parameter, at least for a specified number of columns. Such as: int a [2] [3] and int a [] [3] is legal.

String variable definition:

  string s1="hello";    //合法

  char str [10]; str = "hello"; // not legal

  String constants '\ 0' as a terminator, but stored in a string variable will contain '\ 0' the

Class fragmentary knowledge:

  Private members of a class member functions can only be referenced in this class, the class can not call outside (except friend), both members of the public can be invoked in this class member function can also be called outside the class function. Protected members can not be accessed outside the class, but can be accessed by member functions of the derived class.

  The difference between classes and structures that, when there is no statement shows that members of the class defaults to private, and structure members default to public.

  Private member functions can only be called by other member functions of this class can not be invoked outside the class. Member function can access any member of the class (including private members), valid data can be referenced in the present scope. Member function calls need to be outside of designated public, they are kind of external interfaces.

  Class member functions are usually declared in the class, defined outside the class, and the statement must also be very short if the function is defined in the class before you define. When outside the class defined function to add :: scope qualifier, if not, it indicates that the function does not belong to any class, ordinary functions of a non-member function.

  Reference to object members there are three ways: through the object name and the access member operator; access pointer by the pointing object; accessed through object reference variables.

  display () method, stu.display (); a message, with the message activation method.

  If the method is called class data members did not give initial value, then their values ​​are unpredictable.

  When the class is set to the default parameters, with default parameters declaration statement prevail, even if the definition of default design parameters again does not work. The void set_time (Time &, int hour = 0, int minute = 0, ine sec = 0); / * This is a declaration statement * / In addition, when the object is artificial to the class of the initial value, the default parameters covered, but was there are only covered by the data member is assigned the value of data members are still not assigned default parameters.

  Since the class is not an entity, but an abstract type, does not occupy storage space, it is not defined in the class data is initialized, no place to accommodate such data. If the data members are public, data members can be initialized when the object is defined. But if the data member is private or protected, can not be used this way, data can only be accessed indirectly, members of member functions of the class, and its assignment.

  Initialization, constructors C ++ to handle the object with the constructor is a special member function, users do not need to call it, but automatically executed when creating objects. The class constructor must be the same name as the name can not be named by the user, in order to compile the system recognizes it as a constructor. Type and it does not have any return value. Constructor function defined by the user, the user initialization function according to design parameters of body function requirements.

class Time
{
    public:
        Time()                //定义构造成员函数,函数名与类名同名
        {
            hour=0;            //利用构造函数对对象中的数据成员赋初值
            minute=0;
            sec=0;
        }
        void set_time();    //成员函数声明
        void show_time();
    private:
        int hour;        //私有数据成员
        int minute;
        int sec;
};
void Time::set_time()
{
    cin>>hour;
    cin>>minute;
    cin>>sec;
}
void Time::show_time()
{
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main()
{
    Time t1;    //建立对象t1,同时调用构造函数t1.Time()
    t1.set_time();    //对t1的数据成员赋值
    t1.show_time();
    Time t2;    //建立对象t2,同时调用构造函数t2.Time()
    t2.show_time();    //显示t2的数据成员
    return 0;
}

Since the object execution mutator set_time t1 (), and therefore has the initial value when calling the constructor is covered, and t2 is still obtained when the initial value of calling the constructor. 

  Defined outside the class constructor to add the same scope qualifier. Time :: Time () {hour = 0; minute = 0; sec = 0;}

  Call the constructor of the class when the object enters its scope.

  The constructor has no return value, because do not need to declare the type, it is only an object is initialized.

  Constructor does not require a user can not be invoked, it calls automatically by the system, and call only once.

  If the user does not define their own constructor, the system automatically generates a constructor, this constructor function body but is empty, no parameters, the initialization operation is not performed.

  To achieve different initial values ​​assigned to different objects, you must use a constructor arguments, when calling the constructor different objects, the different data is transmitted from the outside to the constructor, to achieve different initialization. Argument is given in the definition of the object.

class Box
{
    public:
        Box(int,int,int);    //声明带参数的构造函数
        int volume();
    private:
        int height;
        int width;
        int length;
};
Box::Box(int h,int w,int l)    //在类外定义带参数的构造函数
{
    height=h;
    width=w;
    length=l;
}
int Box::volume()
{
    return height*width*length;
}
int main()
{
    Box box1(12,25,30);    //建立对象box1,并制定box1长宽高
    cout<<box1.volume();
    Box box2(15,30,21);    //建立对象box2,并制定box1长宽高
    cout<<box2.volume();
    return 0;
}

  Constructor parameters in the parameter, the corresponding argument in the given definition object. It may be different for different objects initialized by this method.

  Defined outside the class constructor parameters may also be written as: Box :: Box (int h, int w, int l): height (h), width (w), length (l) {}

  Constructors have the same name, or a number of different parameters of different parameter types, become overloaded constructor. The system automatically determines a corresponding function according to the form of the function call. An object parameter is not given, it calls the corresponding non-argument constructor, several parameters are given object, it calls the constructors of several parameter. When you define an object with no parameters, not parentheses. Such as: Box box1 instead Box box1 ()

  Also you can set the default value to the constructor, remember that it is specified in the declaration. The Box (int h = 10; int w = 10; int l = 10); without specifying when defining default parameters. When the object is not defined to the argument, the system invokes a default constructor, the value of each parameter are default values. All parameters specifies a default constructor values ​​can be considered a default constructor, because it takes no arguments. In this case, no-argument constructor can not exist, because a class can have a default constructor.

  A class can have many constructors, but only a destructor.

  After destructor first configuration, the first configuration of the destruction, the sequence corresponding to the stack.

  Object (i.e., objects defined outside of any function) defined in the global scope its constructor called before any function is executed. When the main function is finished or exit is called, calls the destructor. If the definition is automatically a local object, its constructor is called when creating an object, if the function is called multiple times, each time you create an object should call its constructor. Function call ends, the first call the destructor release objects. If the definition of a local static object function, only when the first call to this function is called when the object constructor once established, is not released after the end of the call, and therefore does not call the destructor, or only at the end of the main function calls when the exit function, it calls the destructor.

  this pointer points to the class object of the present, its value is the start address of the current object member function to be called is located.

int Box::volume()
{
    return (height*width*length);
}
//C++中处理为
int Box::(Box *this)
{
    return (this->height*this->width*this->length);
}
//则其实是以这种方式调用的
a.volume(&a);

  Often object must have initial values, and only the parameter is initialized by the constructor initializes its table, and the subsequent operations can not be modified, because the data members often are also often subject data members, not in its constructor assignment, can only be initialized in the initialization parameter table. Const class name defined ways of object name [(argument list)], or the class name of the object name const [(argument list)]

  If the object is a regular object, you can not call a non-const member functions of the object type, which is to prevent these functions to make changes to the data members often object. If you want to reference the data members often object, only the member function is declared const type can be. Such as, void get_time () const; Chang member functions can access the data members of objects often, but still not allowed to modify the value of data members often object.

Data members Non-const member functions const member functions
Non-const data member Can refer to, you can change the value You can refer to, but can not change the value of
const data member You can refer to, but can not change the value of You can refer to, but can not change the value of
Const object's data members Not allowed You can refer to, but can not change the value of

  If all the data members of the object can not be modified, the object will be declared as a constant object, and then reference the data with the const member functions member, played a double role as insurance, to ensure that the data members are not modified. Often the object data members are often only ensure data members, member functions are not guaranteed to function normally.

  Often pointing object pointer, Time * const ptr; which can not be modified to point, point to cell content may be modified. Often the object point pointer variable, const Time * ptr; points which can be modified, pointed cell content can not be modified.

  Ruoyi variable is declared as a constant variable, it can only point to a pointer variable with a constant, it can not point to a general pointer variable.

  If a variable is a constant variable pointer to a not declared const it is accessible, but not modify its value, because during the constant argument a pointer to the variable nature often variable and can not be modified, the rest is not often pointed to when the pointer variable is still common variables can be modified.

  Often used as a pointer and function argument often cited, it can guarantee the data can not be freely modified, time and do not make a copy of the function call arguments, to improve process efficiency.

form meaning
Time const t1; t1 is a constant object whose value is not change in any case
void Time::func()const Time is often fun class member functions, can be cited, but can not modify the data members of the class
Time * const p; p is a pointer to the object Time constant pointer, the value of p (i.e., the point p) can not be changed
const Time * p Time p is a pointer to the class object often, not by the value of the pointer which points to the class object is changed
Time &t1=t; Time t1 is a reference to the class object, both point to the same section of memory space

  new operator dynamically allocated memory, the return value of a pointer to the new object, usually a pointer to the class object of the present variable to receive the address.

  Constructor is a copy constructor, but it is only one parameter, the parameter is an object of this class. Box :: Box {height = b.height; width = b.width; length = b.length;} (const Box & b) when calling: Box box2 (box1); if the user's own copy constructor is not defined, the compiler system It will automatically provide a default copy constructor. It can also be used Box box2 = box1; manner replication objects. Copy and assignment of different objects, the former is to create a new object from scratch, and it is identical with an existing object, which is assigned to the existing object, you must first define the object being assigned to carry out assignment.

  Replication constructor is called: ① the establishment of a new object and initializes it with the existing class object. ② When the object parameters as a function of the class, in order to complete the arguments passed to the object parameter, a need to make a copy of the argument. ③ The return value is a function of when the object class.

  Static variables used to achieve data sharing between objects. All objects can reference it. If you change its value, and that value in all subjects also changed. Static data is not released with the revocation of the space occupied by the object, because it is shared by all objects, and only released when the program ends.

  Static variables can be initialized in the class body. Such as: int Box :: height = 10; (do not add static), if no static data members initial value, the default is 0.

  Static data members of the object name references available, can also be referenced class name.

  This is not a static member function pointers. It can directly reference the static data members of the class.

  If defined elsewhere outside of a function in this class, friend be declared in the class system in China, this is the function of this class friend function. Friend function can access private members of the class.

class Time
{
    friend void display(Time &);
}
void display(Time& t)
{
    cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
//因为display函数不是Time类的成员函数,不能默认一弄Time类的数据成员,必须指定要访问的对象

  friend may be not only a function of a general (non-member function), the function can be a member of another class.

class Date;    //对Date类的提前引用声明
class Time
{
    void display(Date &);    //display是成员函数,形参是Date类对象的引用
}
class Date    //声明Date类
{
    friend void Time::display(Date &);    //声明Time类中的display为友元成员函数
}    

  If B is a friend of class A, then B, all functions are Class A friend function, you can access all the members of the A class.

  Friend of the relationship is one-way, and can not pass.

  After the operator is overloaded, so that the original function disappears or does not change, only the expansion of functions, executed in the end as to what functions, determined by the context of the expression, i.e., the operator determines the data type of sides.

  Can not be overloaded operators are: (member access operator) /.* (pointer member access operator) /: :( operator domain) / the sizeof (operator length) / :( conditional operator)?

  Overload can not change the number of operands, operators can not change the priority, the operator can not change the binding, can not have default parameters, must be used with a custom user-defined type of object, which has at least one parameter class object (class or object reference).

  Operator overloading function may be a member function of a class, the class may be friend function. The difference is that if the member function of the class, this class can access the data members freely through this pointer, it is possible to write a function of few parameters, but must be requested first parameter calculation expression is a class object, and the operational the same type of operator functions. The binary operator is overloaded friend function must have a function in the parameter list of two parameters, any can be omitted, the order parameter, the first parameter is not required to have an object class, eleven in use but must be correspond.

/*运算符重载函数作为成员函数*/
class Complex
{
    Complex operator + (Complex &c2);
};
Complex Complex::operator + (Complex &c2)
{
    Complex c;
    c.real=real+c2.real;    //real==this->real
    c.imag=imag+c2.imag;
    return c;
} 
/*运算符重载函数作为友元函数*/
class Complex
{
    friend Complex operator + (Complex &c1,Complex &c2);
};
Complex operator + (Complex &c1,Complex &c2)
{
    return Complex(c1.real+c2.real,c1.imag+c2.imag);
}

  Private member can only reference the base class outside the class, private member can only be referenced through the public base class member function in the base class.

Public base class members Private member Public member Protected Members
Access to public property in the derived class Inaccessible public protection
Private base class members Private member Public member Protected Members
Access to private property in the derived class Inaccessible private private
Protecting the base class members Private member Public member Protected Members
Access the protection of property in the derived class private protection protection

  Since it declared as private inheritance, it means that the original members of the external references can be hidden, do not let outside references, thus protecting members of the public and private members of the base class of naturally become private members of the derived class. Private members of a private base class member functions can only be referenced base class rule, outside the base class (ie derived classes) of course, you can not access them.

  All members of the class of protecting groups in a derived class are protected, can not be accessed outside the class, its members and the protection of members of the public member functions can be accessed its descendants (children can open)

  Derived class members visit four different properties: ① outside public, within the derived class and derived class can access. ② protected, access to the derived class, the derived class can not be accessed outside, the next layer ③ derived classes can access private, accessible within a derived class, the derived class can not be accessed outside the inaccessible ④, the derived classes and classes outside are inaccessible.

  Either access properties, all derived classes can not access private members of the base class. Private members can only be accessed by member functions of this class.

  The base class constructor can not be inherited, and therefore have to bear for the inherited base class member initialization by the constructor of a derived class. The specific approach is to call the base class constructor of the implementation of the derived class constructor. Only new derived class data members initialized in the body of the function.

  Derived class constructor tasks include: ① object data to initialize the base class data members ② ③ pair member initialization initializes the data members of the derived classes

  The derived class constructor sequentially performed: ① the base class constructor call, call the child object constructor initializes the base class data members ②, ③ sub-object data members are initialized itself performs the derived class constructor, initialization of the derived class data members.

  The multilayer derived simply call the constructor layer of a derived class (immediate base class) to the constructor.

  If not defined in the base class constructor or a constructor with no parameters is defined, then when defining the derived class constructor can not write the base class constructor. When you call the derived class constructor, the system will first call the default constructor for the base class.

  If the constructor parameters defined in the base class or sub-object type declaration, it is necessary to explicitly define the derived class constructor.

  If the same name is present in the base class data members, when referring to avoid ambiguity, a domain qualifier may indicate which is the base class data members, such as: Teacher :: name

  If the base and derived classes have members with the same name, the access is a member of the derived class, the base class member is shielded. In this case, to access the base class members, with the need to specify a domain qualifier.

  When before the derived class declaration to a corresponding virtual inheritance, after this statement, when a path through a plurality of the derived classes inherited a group derived class, the derived class inherits the base class only once, i.e. only the base class members are reserved once.

class A    //定义基类A
{
    A(int i) {}    //基类构造函数,有一个参数
};
class B virtual public A    //A作为B的虚基类
{
    B(int n):A(n){}    //B类构造函数,在初始化表中对虚基类初始化
};
class C virtual public A    //A作为C的虚基类
{
    C(int n):A(n){}    //C类构造函数,在初始化表中对虚基类初始化
}
class D :public B,public C    //类D的构造函数,在初始化表中对所有基类初始化
{
    D(int n):A(n),B(n),C(n){}
};

  In the final provisions of the derived class is not only responsible to initialize its base class directly, but also responsible for the virtual base class initialization.

class Graduate:public Teacher,public Student    //Teacher和Student为直接基类
{
    public:
    Graduate(string nam,char s,int a,string t,float sco,float w):
    Person(nam,s,a),Teacher(nam,s,a,t),Student(nam,s,a,sco),wage(w){}
    private:
    float wage;
}
//要用最后一个派生类对所有直接基类和虚基类进行初始化

  By pointing to the base class object pointer, the base class members can only access the derived class, the derived class can not access additional members.

  Derived class object can be assigned to a base class object.

  Alternatively the base may be derived object class object reference to the base class object initialization or assignment.

  If the parameter is a function of the base or base class object reference to the object class corresponding arguments can subclass object.

  Address derived class object pointer variable can be assigned to a base class object, i.e. to a base class object pointer variable point may be derived class objects.

  Static polymorphism is achieved by overloaded function. Dynamic polymorphism is determined that the object was moving operation for which the program is running. Also known as run-time polymorphism. Dynamic polymorphism is achieved through virtual functions.

  In the base class with a virtual function is a virtual function declared member. So that you can redefine this function in a derived class, gives it new capabilities, and can be called easy.

  This function is redefined in a derived class, the required function name, the function type, number and type of all the function parameters with the same base class virtual function, and to redefine the function of the derived class member as required.

  Define a pointer variable of the base class, and to point to a subject in need of the same class calls the function in the family.

  This call through the virtual function pointer variable is a pointer variable to point to objects of the same name and function call at this time.

  Pure virtual function is the base class, but do not need the derived class may be used, the function name reserved for the base class, which specific function is left as necessary to define a derived class. The general form of the statement is a pure virtual function: virtual function type function name (parameter list) = 0;

  If you declare a pure virtual function in a class, but in a derived class is not the function definition, the function in the derived class is still a pure virtual function.

  If the establishment of the new operator with a temporary object, and the base class destructor, and a pointer to the definition of the base class variable in a program delete operator revoking an object, only the base class destructor performed without fictional function performs the derived class. If you want to execute the destructor derived class may be declared destructor for the virtual base class destructor. Preferably the destructor declared as virtual base class, automatically become destructor virtual function of the base class will be derived class, the derived timely destructor destructor base class name of the class is not the same.

  There are several classes are defined sole purpose is to use it as a base class to derived class establishment, which provides to the user as a basic type, user-defined according to their different functions required of a derived class on this basis. Use these to build a derived class object. This is called an abstract class. Any containing pure virtual functions are abstract classes. Can not be called pure virtual function, so the object can not be established abstract class, an abstract class is acting as a common base class to Group, provides a common interface to a class family.

  Although not an abstract class definition object, but may define a pointer to the abstract class variable data, when the derived class is a concrete class can use this pointer to the class object through the virtual function call pointer, to achieve polymorphism operation.

  Throw after throw an exception information, processes immediately leave this function, go to its previous level of function. It will not perform the function behind the current statement.

  immediately after the catch must try, can not be used alone, can not be inserted between two other statements.

  throw exception went to what type of matching with the type of catch block.

  In a try-catch configuration may only try block without catch block. I.e., this function is not only the check processing, processing in the catch other functions.

  try and catch must be enclosed in braces, it can not be omitted.

  A try-catch only one try block structure, but can have a plurality of catch blocks with different exception information matches.

Published 35 original articles · won praise 2 · Views 1384

Guess you like

Origin blog.csdn.net/weixin_41001497/article/details/102593946