Header-File Design and Usage Guidelines

Header-File Design and Usage Guidelines As is well known, compared with object-oriented C++, procedural C has no concept of encapsulation, inheritance, or polymorphism, let alone interfaces. In C programs, using the principle of separate compilation, header files (.h or .hpp) serve to provide declarations and interfaces, while source files (.c) provide implementations, thereby achieving modular engineering design and ensuring high cohesion and low coupling. This is undoubtedly very important. Preface Preface 1. Disclaimer The content of this article is largely derived from “C — Principles, Rules, and Recommendations for Header-File Inclusion”, but that article has poor formatting, so the content has been reformatted and excerpted here. ...

2022-08-09 · 4 mins · 3rd

C++ Polymorphism

C++ Polymorphism Polymorphism, as the name suggests — many forms. In plain terms, it means “one interface, multiple implementations.” (At the time of writing, my knowledge framework was not yet fully developed; please feel free to point out any omissions or errors.) 1. Why Do We Need Polymorphism? In engineering, we frequently encounter ever-changing requirements. If we design a separate interface and logic for every requirement, it would inevitably lead to a large amount of code redundancy and reduced maintainability. ...

2022-07-09 · 10 mins · 3rd

A Few Things About Structs

On Structs A struct is a quintessential example of an abstraction born out of engineering necessity — it was the demand that chose the struct, not the struct that created the demand. 1. What Is a Struct? Wikipedia’s struct article describes it as follows: In C, a struct (structure) is a data structure that falls under the category of composite (aggregate) data types. In plain language, a struct is a composite data type created by combining basic data types as needed. ...

2022-02-25 · 5 mins · 3rd

What Is a Function Pointer?

What Is a Function Pointer? Pointers are one of the most important concepts in C/C++, and also one of the hardest to grasp. Their purpose can be roughly explained with this analogy: just as we can find a friend’s new address after they move by looking up their street number, we can use a pointer to locate and access data at a specific address in memory. We won’t dive deeper into pointers here, though — we’ll focus specifically on function pointers. ...

2022-02-23 · 3 mins · 3rd