C Plus Plus Classes (CloudMonk.io)

C++ Classes



Return to Discovering Modern C Plus Plus, 2nd Edition | Discovering Modern C++, C plus plus DevOps | C++ DevOps, C plus plus books | C++ books, C plus plus courses | C++ courses, C plus plus topics | C++ topics, C plus plus | C++


Chapter 2

Classes

Computer science is no more about computers than wp>astronomy is about [wp>telescopes.” — Edsger W. Dijkstra

"And computer science is more than drilling on programming language details. That said, this chapter will not only provide information on declaring C plus plus classes but also give you an idea of how we can make the best use of them, how they best serve our needs. Or even better: how a C plus plus class can be used conveniently and efficiently in a broad spectrum of situations. We see classes not only as a way of bundling data but primarily as instruments to establish new C plus plus abstractions in our C plus plus software."

2.1 Program for Universal Meaning, Not Technical Details
Writing leading-edge engineering or scientific software with a mere focus on performance details is very painful and likely to fail. The most important tasks in scientific and engineering programming are:

Identifying the mathematical abstractions that are important in the domain, and

Representing these abstractions comprehensively and efficiently in software.

Or for short:

Advice

Use the right abstractions! If they do not exist, implement them.

Focusing on finding the right representation for domain-specific software is so important that this approach evolved into a programming paradigm: Domain-Driven Design (DDD). The core idea is that software developers regularly talk with the domain experts about how software components should be named and behave so that the resulting software is as intuitive as possible (not only for the programmer but even more so for the user). The paradigm is not thoroughly discussed in this book. We rather refer to other literature like [72].

Common abstractions that appear in almost every scientific application are vector spaces and linear operators. The latter project from one vector space into another. When incorporating them into software, we should first decide how to represent these abstractions best.

Let v be an element of a vector space and L a linear operator. Then C++ allows us to express the application of L on v as:

L(v)
Or as:

L * v
Which one is better suited in general is not so easy to say. However, it is obvious that both notations are much better than

Click here to view code image

apply_symm_blk2x2_rowmajor_dnsvec_multhr_athlon(L.data_addr ,
L.nrows, L.ncols, L.ldim, L.blksch, v.data_addr, v.size);
which exposes lots of technical details and distracts from the principal tasks. Developing software in that style is far from being fun. It wastes so much energy of the programmer. Even getting the function calls right is much more work than with a simple and clear interface. Slight modifications of the program—like using another data structure for some object—can cause a cascade of modifications that must be meticulously applied. Remember that the person who implements the linear projection wants to do science, actually.

The Purpose of Scientific Software

Scientists do research. Engineers create new technology.

Excellent scientific and engineering software is expressed only in mathematical and domain-specific operations without any technical detail exposed.

At this abstraction level, scientists can focus on models and algorithms, thus being much more productive and advancing scientific discovery.

The cardinal error of scientific software providing low-level interfaces like that of the last example (and sadly, we have seen even worse than that) is to commit to too many technical details in the user interface. The reason lies partly in the usage of simpler programming languages such as C and Fortran 77 or in the effort to interoperate with software written in one of those languages. If you ever are forced to write software that interoperates with C or Fortran, write your software first with a concise and intuitive interface in C++ for yourself and other C++ programmers and encapsulate the interface to the C and Fortran libraries so that it is not exposed to the developers.

It is admittedly easier to call a C or Fortran function from a C++ application than the other way around. Nonetheless, developing large projects in those languages is so much more inefficient that the extra effort for calling C++ functions from C or Fortran is absolutely justified. Stefanus Du Toit demonstrated in his Hourglass API an example of how to interface programs in C++ and other languages through a thin C API [12].

The elegant way of writing scientific software is to provide the best abstraction. A good implementation reduces the user interface to the essential behavior and omits all unnecessary commitments to technical details. Applications with a concise and intuitive interface can be as efficient as their ugly and detail-obsessed counterparts.

Our abstractions here are linear operators and vector spaces. What is important for the developer is how these abstractions are used, in our case, how a linear operator is applied on a vector. Let’s say the application is denoted by the symbol * as in L * v or A * x. Evidently, we expect that the result of this operation yields an object of a vector type (thus, the statement w= L * v; should compile) and that the mathematical properties of linearity hold. That is all that developers need to know for using a linear operator.

How the linear operator is stored internally is irrelevant for the correctness of the program—as long as the operation meets all mathematical requirements and the implementation has no accidental side effect like overwriting other objects’ memory. Therefore, two different implementations that provide the necessary interface and semantic behavior are interchangeable; i.e., the program still compiles and yields the same results. The different implementations can of course vary dramatically in their performance. For that reason, it is important that choosing the best implementation for a target platform or a specific application can be achieved with little (or no) program modifications on the application level.

This is why the most important benefit of classes in C++ for us is not the inheritance mechanisms (Chapter 6) but the ability to establish new abstractions and to provide alternative realizations for them. This chapter will lay the foundations for it, and we will elaborate on this programming style in the subsequent chapters with more advanced techniques.



Fair Use Sources


Fair Use Sources:
* B09HTJRJ3V, DMdrCpp, 2021


C Plus Plus | C++: Effective CPP | Effective C++, C Plus Plus Best Practices | C++ Best Practices, CPP Core Guidelines (CG) by Bjarne Stroustrup and Herb Sutter | C++ Core Guidelines (CG) by Bjarne Stroustrup and Herb Sutter, C Plus Plus Fundamentals | C++ Fundamentals, C Plus Plus Inventor | C++ Inventor - C Plus Plus Language Designer | C++ Language Designer: Bjarne Stroustrup in 1985; C Plus Plus Keywords | C++ Keywords, CPP Built-In Data Types | C++ Built-In Data Types, C Plus Plus Data Structures | C++ Data Structures (CPP Containers) - C Plus Plus Algorithms | C++ Algorithms, C Plus Plus Syntax | C++ Syntax, C Plus Plus OOP | C++ OOP - C Plus Plus Design Patterns | C++ Design Patterns, Clean C Plus Plus | Clean C++ - C Plus Plus Style Guide | C++ Style Guide - C Plus Plus BDD | C++ BDD, C Plus Plus Standards | C++ Standards (C Plus Plus 23 | C++ 23, C Plus Plus 20 | C++ 20, C Plus Plus 17 | C++ 17, C Plus Plus 14 | C++ 14, C Plus Plus 11 | C++ 11, C Plus Plus 03 | C++ 03, C Plus Plus 98 | C++ 98), Bjarne Stroustrup's C Plus Plus Glossary | Bjarne Stroustrup's C++ Glossary - Glossaire de CCP - French, CppReference.com, CPlusPlus.com, ISOcpp.org, C Plus Plus Compilers | C++ Compilers (Compiler Explorer, MinGW), C Plus Plus IDEs | C++ IDEs, C Plus Plus Development Tools | C++ Development Tools, C Plus Plus Linter | C++ Linter, C Plus Plus Debugging | C++ Debugging, C Plus Plus Modules | C++ Modules (C Plus Plus 20 | C++20), C Plus Plus Packages | C++ Packages, C Plus Plus Package Manager | C++ Package Manager (Conan - the C/C Plus Plus Package Manager | Conan - the C/C++ Package Manager), C Plus Plus Standard Library | C++ Standard Library, C Plus Plus Libraries | C++ Libraries, C Plus Plus Frameworks | C++ Frameworks, C Plus Plus DevOps | C++ DevOps - C Plus Plus SRE | C++ SRE, C Plus Plus CI/CD | C++ CI/CD (C Plus Plus Build Pipeline | C++ Build Pipeline), C Plus Plus Data Science | C++ Data Science - C Plus Plus DataOps | C++ DataOps, C Plus Plus Machine Learning | C++ Machine Learning, C Plus Plus Deep Learning | C++ Deep Learning, Functional C Plus Plus | Functional C++, C Plus Plus Concurrency | C++ Concurrency, C Plus Plus History | C++ History, C Plus Plus Topics | C++ Topics, C Plus Plus Bibliography | C++ Bibliography, Manning CPP Series | Manning C++ Series, C Plus Plus Courses | C++ Courses, CppCon, C Plus Plus Research | C++ Research, C Plus Plus GitHub | C++ GitHub, Written in C Plus Plus | Written in C++, C Plus Plus Popularity | C++ Popularity, C Plus Plus Awesome | C++ Awesome , C Plus Plus Versions | C++ Versions. (navbar_cplusplus -- see also navbar_cpp_containers, navbar_cppcon, navbar_cpp_core_guidelines, navbar_cpp23, navbar_cpp20, navbar_cpp17, navbar_cpp14, navbar_cpp11)

----



Cloud Monk is Retired (impermanence | for now). Buddha with you. Copyright | © Beginningless Time - Present Moment - Three Times: The Buddhas or Fair Use. Disclaimers



SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.



----