Some interview questions ZZ

Source: Internet
Author: User
Tags set socket
1. Is there a pure virtual constructor in C ++?
2. Does a class in C ++ declare a static member variable?
3. Is it useful to declare a static member function in a class of C ++?
4. How to implement a non-blocking socket?
5. Setsockopt and IOCTL can both set socket attributes. What are their differences?
6. What is the difference between a process and a thread?
7. What is the meaning of multicast and broadcast?
8. What protocol is used for multicast?
9. In C ++, what is the role of pure virtual destructor? For example.
10. Programming, please implement a function similar to atoi in C language (the input may contain non-numbers and spaces)

1. Is there a pure virtual constructor in C ++?
2. Does a class in C ++ declare a static member variable?
3. Is it useful to declare a static member function in a class of C ++?
4. How to implement a non-blocking socket?
5. Setsockopt and IOCTL can both set socket attributes. What are their differences?
6. What is the difference between a process and a thread?
7. What is the meaning of multicast and broadcast?
8. What protocol is used for multicast?
9. In C ++, what is the role of pure virtual destructor? For example.
10. Programming, please implement a function similar to atoi in C language (the input may contain non-numbers and spaces)

My answer

1. Virtual constructor is not supported in C ++, and pure virtual constructor is also not supported.
2. Useful. static member variables do not belong to a certain object but belong to a class. For example, to use the circumference rate in a class, you can declare it as static.
3. Useful for accessing static member variables
4. Use setsockopt or IOCTL
5. IOCTL changes the behavior of the system kernel. setsockopt changes the behavior of the application socket (no relevant information is found)
6. A process is created to replicate all resources of the parent process and can run independently. The thread itself does not copy resources. It shares the resources of the parent process with other threads and cannot run independently. If a thread has a problem, it will affect other threads.
7. Multicast sends data to a Class D address, such as 224.1.1.1. To receive the data packet, the client must use the IGMP protocol to join the group and notify the router. Broadcast sends data packets to a sub-network segment, for example, 192.168.3.255. The host in this segment can directly receive data, and the protocol used varies with the network.
8. multicast adopts IGMP and network group management protocol.
9. Sub-class resources can be released.
10. See upstairs

-------------------------------
Try to answer:
1. Is there a pure virtual constructor in C ++?
9. In C ++, what is the role of pure virtual destructor? For example.
Yarco: It seems that the existence of pure virtual destructor cannot be achieved based on the idea of OO. however, the GCC compilation failed last night. c ++ does not support pure virtual constructor. because you have seen virtual destructor, you know that the virtual destructor are used to release resources for sub-classes. for example, in a function:
Void use (basecls * P)
{
Delete P;
}
I can pass a pointer to a subclass instance and release its resources normally.

2. Does a class in C ++ declare a static member variable?
3. Is it useful to declare a static member function in a class of C ++?
Yarco: useful. In smalltalk, their concepts are class variables and class (member) functions. They are obviously responsible for the entire class. Useful areas include:
Counts the number of instantiated objects of a class/subclass to obtain an instantiated object (CLS & OBJ = CLS: getinst ()

4. How to implement a non-blocking socket?
5. Setsockopt and IOCTL can both set socket attributes. What are their differences?
6. What is the difference between a process and a thread?
7. What is the meaning of multicast and broadcast?
8. What protocol is used for multicast?
Yarco: This part does not belong to C ++. it is more like Linux C socket programming. check books. however, Question 8 does not seem rigorous. because the Protocol itself is layered. it seems irrational to answer all the protocols.

10. Programming, please implement a function similar to atoi in C language (the input may contain non-numbers and spaces)
Yarco: Write steps.
Int atoi (const char *);

// Char * P points to the first character string, int Total = 0, int flag = 1;
// For Loop skipping Spaces
// Judgment +/-symbol, + Skip,-set flag =-1;
// While Judge * P> = '0' & * P <= '9'
// Loop Total = total * 10 + * P-'0 ';
// Returns total * flag;

10.
/* 10. Atoi implementation converts a string to a number */

# Include <stdio. h>
# Include <stdlib. h>
# Include <strings. h>

Int t_atoi (const char * Str)
{
Char * S1, * S2, * S3;
Char arr [200];
Int t_int;

S1 = (char *) malloc (sizeof (STR ));
Strcpy (S1, STR );
S2 = S1;
S3 = arr;
Printf ("S1 = % s \ n", S1 );
While (* S1! = '\ 0 ')
{
If (* S1 <48 | * S1> 57)
{
Printf ("entry error \ n ");
Return-1;
}
Else
{
* S3 = * S1;
}
S1 ++;
S3 ++;

}
Free (S2 );
* S3 = '\ 0 ';
Sscanf (ARR, "% d", & t_int );
Return t_int;
}

Int main (INT argc, char * argv [])
{
Char s [200];
Int t_ I;
Printf ("Please entry a number: \ n ");
Scanf ("% s", S );
T_ I = t_atoi (s );
Printf ("t = % d \ n", t_ I );
Return 0;
}

Bytes --------------------------------------------------------------------------------------
# Include <stdio. h>
# Include <stdlib. h>
# Include <limits. h>
Int myatoi (const char * s)
{
Int A, B, C;
Int flag;
Char * J;
A = B = C = 0;
Flag = 1;
J = (char *) S;
If (null = s)
Return 0;
If (* j = '-')
{
Flag =-1;
J ++;
}
While (isdigit (A = * j ++ ))
{
B = C;
A = a-0x30;
C = 10 * B +;
If (C <0) // handle overflow, so that the behavior is consistent with that of atoi
Return flag * int_max;
}
Return flag * C;
}
Int main ()
{
Char * num = "-153.78abc ";
Printf ("% d \ n", myatoi (Num), atoi (Num ));
Printf ("% d \ n", myatoi ("7788"), atoi ("7788 "));
Printf ("% d \ n", myatoi ("-7788"), atoi ("-7788 "));
Printf ("% d \ n", myatoi ("abcd7788"), atoi ("abcd7788 "));
Printf ("% d \ n", myatoi ("7788 ABCD"), atoi ("7788 ABCD "));
Printf ("% d \ n", myatoi ("9876543210123"), atoi ("9876543210123 "));
Printf ("% d \ n", myatoi ("-9876543210123"), atoi ("-9876543210123 "));
Printf ("% d \ n", myatoi (null ));
Printf ("% d \ n", int_max );
Int I;
Printf ("% d \ n", (sscanf (Num, "% d", & I), I ));
Float T;
Printf ("% F \ n", (sscanf (Num, "% F", & F), f ));
// Sscanf can replace atoi and atof directly.
Return 0;
}

Bytes --------------------------------------------------------------------------------------

Code:
Virtual Constructor )"?

A usage that allows you to do something that is not directly supported by C ++.

You may use the virtual function virtual clone () (For the copy constructor) or virtual function virtual create () (For the default constructor) to obtain the effect of the virtual constructor.

Class shape {
Public:
Virtual ~ SHAPE () {}// virtual destructor
Virtual void draw () = 0; // pure virtual function
Virtual void move () = 0;
//...
Virtual shape * clone () const = 0; // use the copy constructor
Virtual shape * Create () const = 0; // use the default constructor
};

Class circle: Public shape {
Public:
Circle * clone () const {return new circle (* This );}
Circle * Create () const {return new circle ();}
//...
};

In the clone () member function, the Code new circle (* This) calls the copy constructor of circle to copy the status of this to the newly created circle object. In the CREATE () member function, Code new circle () calls the default constructor of circle.

You can use them as "virtual constructor:

Void usercode (shape & S)
{
Shape * S2 = S. Clone ();
Shape * S3 = S. Create ();
//...
Delete S2; // here, you may need virtual destructor
Delete S3;
}

This function will work correctly, regardless of whether the shape is a circle, square, or other types of shape, or even they do not exist.

Note: The returned value type of the member function circle's clone () is intentionally different from that of the member function shape's clone. This feature is called the return type of the covariant, which is not initially part of the language. If your compiler is not allowed to declare the circle * clone () const (such, the prompt "The return type is different" or "The member function's type differs from the base class virtual function by return type alone") indicates that your compiler is outdated, then you must change the return type to shape *.
To redsnow:
This is actually a creation mode. It aims to introduce a 'virtual 'constructor.

If the shape does not exist, then the clone () in shape * S2 = S. Clone (); is a saved virtual function. Is that OK?

It should be noted that S is defined as a bit shape & type, and actually passed in is an actual type inherited from shape. In the derived type, clone and other methods must be better than override.

The so-called "shape does not exist" refers to any class that inherits the shape and override these functions. It can be used for this purpose.

Because this is a recruitment question, I think he wants to test his understanding of the design model,

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.