Multi-state and object-oriented (on)
No matter how the time is passing, it should be said that everyone's first interview with his career is still new. Not?
After the first two rounds of screening, I pushed the door, carefully sit in front of Andy. The position I apply is C programmer. I have some nervousness. You know, a new college student who can't find a job, how life is full of pressure.
"Can you tell me what is OO?" Andy is the case.
I have a hunch, he will mainly examine my object-oriented programming capabilities. I know that C supports several different programming style, including procedural programming, generic programming, object-based programming style, and object-oriented programming style Object-Oriented Programming. But what is OO? I really know clearly.
"When analyzing the problem, we often treat things as an object, regard objects with the same characteristics as a class ..." I think he is looking forward to this answer.
"Then what explains your OB?" Andy did not express his expression.
Yes, the so-called ob is called the program design style known as ADT (Abstract Data Template), I just said that I did not support it by OB? Always think that you are using object-oriented programming technology, who knows ... Hey, it is really embarrassing.
"Yes, it should be said that OO will give me the deepest or inheritance." Now I can only try to recall the less poor project I have participated in the answer.
"Almost everyone agrees that inherits and polymers are OO concept, but according to what I know, I really understand these two concepts. Time is limited, let's talk about polymorphism. Do you know what methods C supports polymorphism ? "Obviously he didn't want to spend too much time in me.
This problem is not difficult for me. For example, there is the following inheritance system:
Where Rotate is a virtual function, C supports polymorphism. First, via a set of implicit conversion operations, such as a pointer to a derived class pointer to a type pointing to its public base class:
Shape * ps = new circle ();
Second, via virtual mechanism, such as
PS-> rotate ();
Third, via the Dynamic_CAST and TYPEID operators. Such as
IF (Circle * PC = Dynamic_cast
"The main purpose of polymorphology is to affect the type of package via a common interface. This interface is usually defined in an abstract base class. This interface is triggered by the Virtual Function mechanism, which can be realized according to Object during the execution period The type parsing is which function entity is called. "Take the iron, I will definitely, if you can't say a few words, it will be your prehealth.
"What if I write this code?" Andy quickly wrote on a sheet of paper:
Circle P;
Shape s (p);
s.ROTATE ();
Initialize a base class object with a school-based object! This is my first reaction.
"This will happen the so-called object cut, and the polymorphism is no longer rendered." Although I dare to definitely, I always feel that Andy has another meaning.
"Why is the polygon no longer?" His eyes showed a doubt.
"Because the polymorphic features can only be played when using Pointer or Reference." I didn't hesitate to answer. "Well, please tell me about the design." Andy seems to have achieved the purpose. Later I learned that C supported polymorphism through Class's Pointer and Reference, this programming style is the so-called OO.
I remembered a project that had been involved. My main task is to control a peripheral called Yuntai by serial port. In fact, the customer requests to translate the customer into the corresponding string according to the communication protocol, and then send it through the serial port. It looks very simple, is it? However, in order to strive for larger markets, we must support different protocols provided by different manufacturers as much as possible, and can easily join the currently not yet supported protocol in the future. The design at the time was like this:
Class Cptzhal
{
PUBLIC:
CPTZHAL (CSERIAL * PSERIAL, CPROTOCOL * PPROTOCOL);
Void SendCommand (int Command);
Private:
CSERIAL * m_PSERIAL;
Cprotocol * m_pprotocol;
}
The definition of member functions SendCommand is as follows:
Void cptzhal :: Sendcommand (int Command)
{
IF (m_pserial! = null && m_pprotocol! = NULL)
{
m_pserial-> send (m_pprotocol-> getcommandstring (command));
}
}
You see, we use CPTZHAL to accept and process users' requests. CPROTOCOL is our protocol class. The specified request ID should be sent to the serial port by its member function getcommandstring; CSERIAL is actually a serial port class, while the role of cserial :: Send is of course sending the specified string Go out.
"So, how do you guarantee your convenience to add a new agreement?" Andy gently shifted his body.
"As you know, Cprotocol is a base class, and I design it into an abstract base class, all specific protocols will be derived from it." I slowly said, and write the following code:
Class Cprotocol
{
PUBLIC:
Virtual ~ cprotocol () {};
Virtual string getcommandstring (int) = 0;
}
"You see, because it is a base class, its destructor must, of course, to declare to Virtual ..."
"Wait, since the destructor does not do anything, why should I give it an empty function?" Andy began to test me.
"As I just said, I was obliged to declare them. I didn't want to give an implementation, but even if I agree, the compiler will not promise."
"Well, yes." Andy's priority trust me is quite unfortunate.
"As for each specific protocol class, you must implement those pure virtual function declared in CPROTOCOL." I quickly wrote the following code:
Class CintelProtocol: Public Cprotocol
{
PUBLIC:
String getcommandstring (int Command);
}
String CintelProtocol :: getcommandstring (int Command)
{
/ / Return the corresponding string
}
"You see, CintelProtocol is encapsulating Intel's agreement. When there is a new agreement, the practice is like the general, and a new protocol is derived from CPROTOCOL." From Andy's expression, he also Not satisfied. As a customer of the protocol, CPTZHAL does not need to know that this specific agreement is Intel or Samsung, saying that it is provided by a certain manufacturer in the future. But what is this? No matter how this change is changed, the cptzhal should not change. This is the benefits of the polymorphism.
Did I end with Andy? No, not yet. This is just as I want to talk to you about OOP, will continue. (Fail)
Reference book:
1. STANLEY B.LIPPMAN. Depth Exploration C Object Model
2. Scott Meyers. Effective C
3. Gang of four. Design mode