C Language’s New Year Dilemma: Alone at Home

During the New Year, C Language reunites with many friends.

Everyone is working hard in different places, and it’s rare to meet each other. When they gather for a meal, everyone is very happy.

C Language's New Year Dilemma: Alone at Home

C Language's New Year Dilemma: Alone at Home

When Java mentions TIOBE, C Language, who is drinking, becomes excited.

It has been ranked second there for many years, and humans have indeed written a lot of programs in it! However, they are all at the bottom level, in system-level programming, such as operating systems, databases, compilers…

Compared to the bustling scene at the application layer, it seems a bit lonely.

C Language's New Year Dilemma: Alone at Home

Python approaches C Language with a drink in hand.

C Language's New Year Dilemma: Alone at Home

C Language's New Year Dilemma: Alone at Home

The atmosphere at the dinner table suddenly becomes a bit awkward, and Python, who caused the trouble, quickly encourages everyone to continue drinking.

After finally enduring until the end of the meal, C Language gloomily returns to its cold and lonely home.

C Language's New Year Dilemma: Alone at Home

C Language suddenly remembers its neighbor, Ken Thompson, who is Dennis Ritchie’s “good buddy”. Together, they created the great Unix operating system and received the highest award in the computer world: the Turing Award.

C Language's New Year Dilemma: Alone at Home

C Language's New Year Dilemma: Alone at Home

C Language's New Year Dilemma: Alone at Home

Seeing C Language’s dejected face, Ken Thompson is greatly surprised!

C Language's New Year Dilemma: Alone at Home

Ken Thompson immediately understands that C Language feels it does not support object-oriented programming.

C Language's New Year Dilemma: Alone at Home

C Language's New Year Dilemma: Alone at Home

Seeing C Language so surprised, Ken Thompson decides to explain it well to him.

He truly is an experienced driver, quickly writing a piece of code.

C Language's New Year Dilemma: Alone at Home

C Language is very smart and quickly understands.

Here, a structure called Shape is defined, and the outside world can only operate on this Shape through related functions, such as creating (Shape_create), moving (Shape_move), etc., and cannot directly access the internal data structure of Shape.

Although there is no keyword like class, the data structure and related operations are written separately, which may not look perfect, but it indeed achieves encapsulation.

C Language's New Year Dilemma: Alone at Home

Ken Thompson remains silent and continues to write code.

It seems that the style of great programmers is similar: stop talking nonsense and just show me the code.

C Language's New Year Dilemma: Alone at Home

This time, a structure for a rectangle (Rectangle) is defined, which nests Shape. Does this mean inheritance is achieved?

C is a bit confused.

C Language's New Year Dilemma: Alone at Home

Through this combination method, it can also be considered as achieving inheritance.

C Language's New Year Dilemma: Alone at Home

So easily achieving encapsulation and inheritance makes C Language feel very excited, but how to achieve polymorphism?

At this moment, the doorbell rings again.

C Language's New Year Dilemma: Alone at Home

Linus glances at the code on the wall and immediately understands what is going on.

C Language's New Year Dilemma: Alone at Home

C Language's New Year Dilemma: Alone at Home

C and Go both look puzzled.

C Language's New Year Dilemma: Alone at Home

Now C begins to understand that whether it is the Rectangle object or the Square object, when calling the Shape_area method, it needs to find the area method in the virtual function table through the vptr pointer.

For Rectangle, it finds the Rectangle_area method, and for Square, it finds the Square_area method.

struct Rectangle *r = Rectangle_create(5,5,10,10);

Shape_area((struct Shape *) r);

C Language's New Year Dilemma: Alone at Home

C Language's New Year Dilemma: Alone at Home

Leave a Comment