diff options
| author | Paul Buetow <paul@buetow.org> | 2023-04-07 00:49:19 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-04-07 00:49:19 +0300 |
| commit | 30fc4e8f74315a92aaec36dfb9a8d4efa0a21791 (patch) | |
| tree | 97ef2b26fd54a366d72f5289ba11ea311f8a0173 /gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.html | |
| parent | 5c9e3886de0f9041e4a6fb6fea9917e29a8fbd76 (diff) | |
exact style
Diffstat (limited to 'gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.html')
| -rw-r--r-- | gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.html | 65 |
1 files changed, 45 insertions, 20 deletions
diff --git a/gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.html b/gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.html index e13f8ea8..bf0cd556 100644 --- a/gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.html +++ b/gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.html @@ -8,8 +8,10 @@ <link rel="stylesheet" href="style-override.css" /> </head> <body> -<h1>Object oriented programming with ANSI C</h1> -<p class="quote"><i>Published at 2016-11-20T22:10:57+00:00; Updated at 2022-01-29</i></p> +<h1 style='display: inline'>Object oriented programming with ANSI C</h1><br /> +<br /> +<span class=quote>Published at 2016-11-20T22:10:57+00:00; Updated at 2022-01-29</span><br /> +<br /> <pre> ___ ___ ____ ____ / _ \ / _ \| _ \ / ___| @@ -18,9 +20,13 @@ \___/ \___/|_| \____| </pre> -<p>You can do a little of object-oriented programming in the C Programming Language. However, that is, in my humble opinion, limited. It's easier to use a different programming language than C for OOP. But still it's an interesting exercise to try using C for this.</p> -<h2>Function pointers</h2> -<p>Let's have a look at the following sample program. All you have to do is to add a function pointer such as "calculate" to the definition of struct "something_s". Later, during the struct initialization, assign a function address to that function pointer:</p> +<br /> +<span>You can do a little of object-oriented programming in the C Programming Language. However, that is, in my humble opinion, limited. It's easier to use a different programming language than C for OOP. But still it's an interesting exercise to try using C for this.</span><br /> +<br /> +<h2 style='display: inline'>Function pointers</h2><br /> +<br /> +<span>Let's have a look at the following sample program. All you have to do is to add a function pointer such as "calculate" to the definition of struct "something_s". Later, during the struct initialization, assign a function address to that function pointer:</span><br /> +<br /> <pre> #include <stdio.h> @@ -54,38 +60,57 @@ int main(void) { printf("%s(%f, %f) => %f\n", div.name, a, b, div.calculate(a,b)); } </pre> -<p>As you can see, you can call the function (pointed by the function pointer) with the same syntax as in C++ or Java:</p> +<br /> +<span>As you can see, you can call the function (pointed by the function pointer) with the same syntax as in C++ or Java:</span><br /> +<br /> <pre> printf("%s(%f, %f) => %f\n", mult.name, a, b, mult.calculate(a,b)); printf("%s(%f, %f) => %f\n", div.name, a, b, div.calculate(a,b)); </pre> -<p>However, that's just syntactic sugar for:</p> +<br /> +<span>However, that's just syntactic sugar for:</span><br /> +<br /> <pre> printf("%s(%f, %f) => %f\n", mult.name, a, b, (*mult.calculate)(a,b)); printf("%s(%f, %f) => %f\n", div.name, a, b, (*div.calculate)(a,b)); </pre> -<p>Output:</p> +<br /> +<span>Output:</span><br /> +<br /> <pre> pbuetow ~/git/blog/source [38268]% gcc oop-c-example.c -o oop-c-example pbuetow ~/git/blog/source [38269]% ./oop-c-example Multiplication(3.000000, 2.000000) => 6.000000 Division(3.000000, 2.000000) => 1.500000 </pre> -<p>Not complicated at all, but nice to know and helps to make the code easier to read!</p> -<h2>That's not OOP, though</h2> -<p>However, that's not really how it works in object-oriented languages such as Java and C++. The method call in this example is not a method call as "mult" and "div" in this example are not "message receivers". I mean that the functions can not access the state of the "mult" and "div" struct objects. In C, you would need to do something like this instead if you wanted to access the state of "mult" from within the calculate function, you would have to pass it as an argument:</p> +<br /> +<span>Not complicated at all, but nice to know and helps to make the code easier to read!</span><br /> +<br /> +<h2 style='display: inline'>That's not OOP, though</h2><br /> +<br /> +<span>However, that's not really how it works in object-oriented languages such as Java and C++. The method call in this example is not a method call as "mult" and "div" in this example are not "message receivers". I mean that the functions can not access the state of the "mult" and "div" struct objects. In C, you would need to do something like this instead if you wanted to access the state of "mult" from within the calculate function, you would have to pass it as an argument:</span><br /> +<br /> <pre> mult.calculate(mult,a,b)); </pre> -<h2>Real object oriented programming with C</h2> -<p>If you want to take it further, hit "Object-Oriented Programming with ANSI-C" into your favourite internet search engine or follow the link below. It goes as far as writing a C preprocessor in AWK, which takes some object-oriented pseudo-C and transforms it to plain C so that the C compiler can compile it to machine code. This is similar to how the C++ language had its origins.</p> -<a class="textlink" href="https://www.cs.rit.edu/~ats/books/ooc.pdf">https://www.cs.rit.edu/~ats/books/ooc.pdf</a><br /> -<h2>OOP design patterns in the Linux Kernel</h2> -<p>Big C software projects, like Linux, also follow some OOP techniques:</p> -<a class="textlink" href="https://lwn.net/Articles/444910/">https://lwn.net/Articles/444910/</a><br /> -<p>C is a very old programming language with it's quirks. This might be one of the reasons why Linux will also let Rust code in.</p> -<p>E-Mail your comments to hi@paul.cyou :-)</p> -<a class="textlink" href="../">Back to the main site</a><br /> +<br /> +<h2 style='display: inline'>Real object oriented programming with C</h2><br /> +<br /> +<span>If you want to take it further, hit "Object-Oriented Programming with ANSI-C" into your favourite internet search engine or follow the link below. It goes as far as writing a C preprocessor in AWK, which takes some object-oriented pseudo-C and transforms it to plain C so that the C compiler can compile it to machine code. This is similar to how the C++ language had its origins.</span><br /> +<br /> +<a class=textlink href='https://www.cs.rit.edu/~ats/books/ooc.pdf'>https://www.cs.rit.edu/~ats/books/ooc.pdf</a><br /> +<br /> +<h2 style='display: inline'>OOP design patterns in the Linux Kernel</h2><br /> +<br /> +<span>Big C software projects, like Linux, also follow some OOP techniques:</span><br /> +<br /> +<a class=textlink href='https://lwn.net/Articles/444910/'>https://lwn.net/Articles/444910/</a><br /> +<br /> +<span>C is a very old programming language with it's quirks. This might be one of the reasons why Linux will also let Rust code in.</span><br /> +<br /> +<span>E-Mail your comments to hi@paul.cyou :-)</span><br /> +<br /> +<a class=textlink href='../'>Back to the main site</a><br /> <p class="footer"> Generated with <a href="https://codeberg.org/snonux/gemtexter">Gemtexter</a> | served by <a href="https://www.OpenBSD.org">OpenBSD</a>/<a href="https://man.openbsd.org/httpd.8">httpd(8)</a> | |
