今天面试同样被问到的一个问题,C与C++的区别.
我的回答如下:
C是面向过程,C++是面向对象。
C++里有函数重载,C中没有函数重载。
C++是面向对象的,有重载 继承 多态三种特性
然后面试官问我多态的用处,坦率的讲我所理解的多态如下:
class animal
{
virtual void speak();
}
class dog:public animal
{
void speak()
{
cout<<"旺旺"<<endl;
}
}
我所理解的多态就是
dog d;
animal *ptr=&d;
ptr->speak()//这个时候输出旺旺
面试时我说多态是比如基类指针指向子类对象,调用基类virtual函数时实际调用子类覆盖的虚函数。
然后面试官问,有没有一种情况下,没有多态的话你就实现不了某种功能的情况呢?
坦率的讲,对于多态我真的只有上面的一点肤浅的理解,我先搜搜相关内容,欢迎各位在此讨论,问题已加粗。
You can find the answer by searching on google: https://www.quora.com/Whats-the-difference-between-C-and-C++
For the second question: If polymorphism refers to technology, the answer is no, because from a technical point of view, object-oriented is just syntax sugar for process-oriented; if it refers to ideas, the answer is yes, there is no idea of polymorphism. Many interface designs will be difficult to implement.
Three basic characteristics of object-oriented: encapsulation, inheritance, and polymorphism.
Polymorphism is the most flexible compared to the first two, and it is also the core part of object-oriented.
But in response to the interviewer’s question: If a certain function cannot be achieved without polymorphism, the answer is no.
Many important systems and applications, considering performance and development process, use more institutional languages (C).
It can only be said that using polymorphism can make the implementation of certain functions look more "elegant".
When the author of C++ persuaded the author of UNIX to write the system in C++, the latter just smiled and refused silently. There's a philosophy to it.
The last question, I guess I want you to use C to simulate polymorphism. The general idea is to use structure + function pointer to simulate a class, use factory function to assign value to function pointer, and finally use structure.members function to trigger the call and complete the polymorphic implementation.
What is the real difference between C and C++? Some differences: C: Write C code, and what is written is C code. C++: When writing C++ code, what is written is still C code. http://www.zhihu.com/question... (Share on Zhihu.com)
You are talking about the difference between process-oriented and object-oriented, not the difference between c and c++. . .
Recommendation: effective c++
Be smart.
There is nothing in the world that cannot be achieved. If so, just *.
There is nothing in the world that pointers cannot achieve. If so, pointer to pointer.
One interface, multiple implementations
@techfellow UNIX philosophy KISS: Keep It Simple, Stupid. That is the "Tao to Simplicity" in Taoism.
Program = Algorithm + Data Structure
In procedural programming, functions are embodied as algorithms. Take "Run, Brother" as an example:
"Brother" as a parameter is the input, and "Run" as a process is the algorithm. .
In C, you can use structures (data structures) to encapsulate parameters and then pass them into functions (algorithms) for operation.
In other words, the procedural emphasis is on the "running" algorithm. The object-oriented emphasis is "Brother", and "running" is just an action.
Three elements of object-oriented: encapsulation, inheritance + polymorphism.
Encapsulation is to "remove global variables".
Inheritance is to "remove code duplication".
Polymorphism is to enable "inheritance" "Established.
Polymorphism is reflected in method rewriting and method overloading.
Method rewriting: The method of the subclass overrides the method of the parent class, requiring the method name and parameters to be the same.
Method overloading: It is the method in the same class. Two or more methods in a class have the same method name,
but different parameters and different method bodies. The most common example of overloading is the constructor of a class.
Q: Is there a situation where you cannot achieve a certain function without polymorphism?
Answer: Under the premise of inheritance, when you need to override or overload methods.
For example, if you have a car class as the parent class, the class has a method to start the engine. Both internal combustion engine cars and fuel cell cars inherit the car class. Because the working principles of the engines are different, it is obviously necessary to rewrite the method of starting the engine. .