Home > Back-end >  1. The struct and class difference
1. The struct and class difference

Time:11-02

1. The struct and class difference is
(1) C struct and C + + class distinction,
(2) in c + + struct and class difference,

(1) the struct is a data type definition, cannot define functions,
(2) in c + + struct and class difference: for member access and inheritance mode, the class is the default in private, and in the struct is public, the class can also be used for the presentation template type, struct is not,

Class private variable calls can be through the mutual function to call
A friend function is to make in addition to a class member function of the function can access to the private members of a class,
A friend relationship can not be inherited
Friends don't have any inheritance, applies only to the current class

What is the Class
The class is to define the class keyword

2. Linux usage
Grep text search tool, it can use regular expressions to search text, and the matching line printed,
Commonly used format for the grep command: grep "patterns" [file] [option]

Search for a word in the text
The grep match_pattern file_name

3. A friend in a class is defined in function and what is the difference between a friend function outside of class?
Use a friend function can data sharing, improve efficiency, so that the two class share the same function,
(1) a friend function is not a class member function, defined within the class or classes there is no difference,
(2) with a friend of friend keywords statement only permission statement, is not a formal function declaration, therefore, before using it also need a formal function declarations,
How to create threads and processes

4. The difference between threads and processes:
1. Each process has its independent memory space, and the thread is not independent of memory space;
2. The thread must run within a process, in other words, no process, there will be no thread;
3. The thread can share the process of memory space;


5. The destructor with pure destructor

6. The static data members use
Static data members are treated as a member of the class, to a part of the class definition, static data members are common to all objects of that class, static data members only allocate memory at a time,
Static data members can be a private member, and global variables, you can't,

7. About static member functions, can be summarized as the following:
In class in vitro function definition cannot specify the keyword static;
Can visit each other between static members, including static member function to access static data members and access a static member function;
Non-static member function can randomly access a static member function and static data members;
Static member function cannot access non-static member function and non-static data members;
Static decorate a function, this function can only be invoked in this document and other documents cannot be invoked,

Global and static variables are stored in the static storage area, life span and the procedure, but the difference is a global variable is the whole project, the scope of the global and static variables are the program files, static local variables is the current function in vivo
Static local variables can be initialized only once
A static global variable can be initialized multiple
: =


8. C + + base class destructor why use virtual virtual destructors
Base class pointer to a derived class object, when the delete will only destructor superclass destructor, no destructor subclass destructor, because virtual function is the dynamic binding, now the destructor is not virtual, thus dynamic binding is not going to happen, but static binding, the static type of pointer to a base class pointer, so when the delete will only call the base class destructor,

8. C + + virtual functions and what is the difference between a pure virtual function?
Virtual functions and pure virtual function can be rewritten in their subclass, the difference is:
(1) pure virtual function definition, is only does not implement; The virtual functions are defined and also has the implementation code,
(2) contains a pure virtual function class doesn't define the object, while containing virtual functions can,

C + + virtual function role is largely "run-time polymorphism", provide the realization of the virtual function in the parent class, provide the default function for the subclass,
Define a function as the virtual function, doesn't mean the function is not implemented, just in order to to achieve polymorphism,
Define a function for the pure virtual function, said function has not been realized, define it in order to implement an interface, a formal role, inherited abstract class derived classes to implement this function...


9. C + + reference vs pointer
Reference easily confused with pointer, there are three main differences between them:
? There is no null reference, references must be connected to a legal memory,
? Once a reference is initialized to an object, cannot be pointing to another object, Pointers can at any time to another object,
? Reference must be initialized with the create, Pointers can be initialized at any time,
References are normally used for function parameter list and function return value

10. Inline function
Inline function refers to the function, the modification with the inline keyword within the class definition of function is the default into an inline function,
When the compiler found a piece of code in the call an inline function, it is not to call the function, but the function of the code, the whole period of inserted into the current position,

11. A function template
A function template is actually to set up a general function, the function type and parameter type is not specified, use a virtual type to represent, the general function is called a function template,
Use a function template can only be used for function parameters have the same number and types of different situations, if the parameter number is different, is not allowed to use a function template,
Template

12. Class template
A class template (also known as a generic class or classes generated) allows the user to a pattern for the class definition, makes some of the data members of the class, the default member function parameters, some member function return values, to be able to take any type (including the system of predefined and user-defined),

13. Const
Is decorated const variable is a constant, this number can't be change

14. The delete and delete [] distinction for Class
Class A
{
};
A * A=new A, [10].
//all just released a pointer to memory space but just call a [0] the rest of the object's destructor from a [1] to [9] the nine users to assign a m_cBuffer corresponding memory space will not be able to release the memory leak causing
Delete a;

//calls using the object's destructor releases the user's own allocated memory space and release all of a pointer to memory space
The delete [] a;

15. In c + +, three objects need to be copied, this time the copy constructor is called
1) an object into the body of the function in the form of value transfer
2) an object from a function in the form of value transfer back to
3) an object need to be initialized with another object

16. The virtual function table
Contain virtual function class will have a virtual function table, virtual function table essence is a pointer array, inside put is a function of the virtual function pointer,

17. A variety of data types in c + + to occupy bytes
One byte is eight
1. Plastic: int, long
32-bit system memory cells is a 32 bit, so it is 4 bytes; A 64 - bit operating system, a memory unit is a 16-bit reason of 8 bytes,

2. The character type: char, would be
Usually occupy a byte, char type for would be types used in the extended character set, need to take two bytes,

3. The Boolean: bool
Bool occupy a byte

4. The floating point type: float, double
Float occupy 4 bytes, double twice as much as the float is 8 bytes

5. Pointer
Pointer to the byte length calculation principle is similar to int, a pointer of digits and figures of the operating system is the same, namely a 32-bit system should be 4 bytes, a 64 - bit system should be 8 bytes

18. The primary pointer and secondary pointer
Level of Pointers: we usually say a pointer, is the memory address;
The secondary pointer: pointer to a pointer, is the address in the address;
With the secondary pointer as a function parameter, there are two kinds of typical case: 1. The need to pass level 1 pointer array is 2. Need to modify the incoming level of pointer

19. The VIM basic pattern
Normal mode
Insert mode
Visual model
Command line mode

20. Under the Linux interprocess communication contact thread in the middle of the
Process is the thread of the container, process resource, memory, threads in the process of using resources, memory code execution, multithreading is a process containing multiple threads, they share the process inside resources and memory space, so multiple threads can communicate through global variables,
Multi-process communication must produce process Shared memory system call, also can use the file, can through the network and other methods,

21. In c + + inheritance and combination difference between using
If logically B is A "A" (A kind of), allows B inherits A function,
If on the logic of A is B "part" (A part of), are not allowed to B inherits A function, but to use A combination and other things out B,

22. In c + +, the vector is a very useful container,
Function: it can be like a container to store various types of objects, in a nutshell, the vector is an array to store any type of dynamic, can increase and compressed data,
Containers (Containers) is used to manage a collection of one type of object, c + + provides a variety of different types of Containers, such as a deque, list, vector, map, etc.,
Algorithm (Algorithms) algorithm is applied to the container, they provide a perform various operations, including the container contents to perform initialization, sorting, search and conversion operations, such as
Iterator (iterators) iteration is used to traverse the data structure in the class, these collections may be a container, may also is a subset of the set of containers,

23. How to choose which one of the three containers, should be according to your need, specific can follow the following principles:
1. If you need efficient random access, and don't care about the efficiency of the insert and delete, using the vector
2. If you need a lot of insertion and deletion, and don't care about random access, should use the list
3. If you need a random access, and concerned with both ends insertion and deletion of data, you should use a deque,
The list is a linear two-way chain table structure, its data consists of a number of nodes, each node including a piece of information (that is, the actual storage of data), a precursor pointer and a pointer, after flooding

24. What is a polymorphic
This because the subclass to rewrite the superclass method, and then use the parent class reference point to subclass object, a method is called when the will to carry on the dynamic binding, this is a polymorphic,
Subclasses override the parent class, the method of the pointer to the parent of the subclass, when calling function will be called subclass functions rather than a parent class parent function before add the keyword virtual

25. The function overloading
Function of the same name in the form of a parameter (refers to the number of parameters, type or order) must be different
Commonly used to implement the function similar to the processing of the data type of the different problems
Number of parameters of the overloaded function parameter type, the parameter order, at least one different in all three, to function overloading, because calling overloaded function is to distinguish according to the specific call what function, the function return value can be the same can be different,
26. The function overloading
The constructor can be overloaded, the destructor is overloaded, can't because there can be multiple and constructor can take arguments,

27. The data abstraction
Data abstraction means that only provide key information to the outside world, and to hide the background of the implementation details, which is only the necessary information and not present details,

28. Interface
C + + interface is to use an abstract class to implement the
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related