Although the principles of structured programming improve the clarity, reliability, and maintainability of programs, they still face challenges when writing large programs. To address these challenges, Object-Oriented Programming (OOP) offers a new approach. Unlike procedural programming, which emphasizes algorithms, OOP emphasizes data. OOP does not attempt to make problems fit the procedural approach of the language; instead, it seeks to make the language meet the requirements of the problem. The idea is to design data formats that correspond to the essential characteristics of the problem.
In C++, a class is a specification that describes this new type of data format, and an object is a specific data structure constructed according to this specification. For example, a class can describe the basic characteristics of a company’s management personnel (name, title, salary, specialties, etc.), while an object represents a specific manager (Guilford Sheepblat, Vice President, $925,000, knows how to restore the Windows registry). Typically, a class specifies what data can be used to represent an object and what operations can be performed on that data. For instance, if a computer graphics program is being developed to draw rectangles, a class describing rectangles can be defined. The defined data part should include the position of the vertices, length and width, colors and styles of the four edges, and the fill color and pattern inside the rectangle; the defined operations can include moving, resizing, rotating, changing colors and patterns, and copying the rectangle to another location. Thus, when using the program to draw a rectangle, it will create an object based on the class definition. This object holds all the data values describing the rectangle, allowing the use of class methods to modify it. If two rectangles are drawn, the program will create two objects, each corresponding to one rectangle.
The OOP programming methodology first designs classes that accurately represent the entities the program will handle. For example, a drawing program might define classes representing rectangles, lines, circles, brushes, and pens. The class definitions describe the operations that can be performed on each class, such as moving a circle or rotating a line. You can then design a program that uses objects of these classes. The process of handling from low-level organization (like classes) to high-level organization (like programs) is called bottom-up programming.
OOP programming is not just about merging data and methods into class definitions. For example, OOP also helps create reusable code, which reduces a significant amount of work. Information hiding can protect data from inappropriate access. Polymorphism allows you to create multiple definitions for operators and functions, determining which definition to use based on the programming context. Inheritance allows you to derive new classes from existing ones. As will be seen later, OOP introduces many new concepts, and the programming methods used differ from procedural programming. It does not focus on tasks but rather on representing concepts. Sometimes, a top-down programming approach is not used; instead, a bottom-up programming approach is employed. This book will help readers understand these points through numerous easy-to-understand examples.
Designing useful and reliable classes is a daunting task; fortunately, OOP languages enable programmers to easily use existing classes in their programming. Many useful class libraries are provided, including those designed to simplify programming in Windows or Mac environments. One of the real advantages of C++ is the ease of reusing and modifying existing, thoroughly tested code.
1.2.1 C++ and Generic Programming
Generic programming is another programming paradigm supported by C++. It shares the same goal as OOP, which is to make code reuse and abstracting common concepts easier. However, OOP emphasizes the data aspect of programming, while generic programming emphasizes the algorithm aspect. Their focuses are different. In C++, generic programming is a powerful programming paradigm that allows you to write generic code that can handle multiple data types. This is primarily achieved through templates, which allow you to write generic functions and classes without specifying concrete data types in advance.
Generic programming is widely used in C++, especially in the Standard Template Library (STL), which includes many generic data structures and algorithms, such as vectors, linked lists, sorting, searching, etc., that can be applied to various data types.
Disclaimer:
Content sourced from Evilrabbit Blog , modified and uploaded by the author of this article
Images (unless otherwise noted) are from Evilrabbit Blog
Reposting this article still requires adherence to the “Evilrabbit” forwarding rules.