learning to code

4 respuestas [Último envío]
eric23
Desconectado/a
se unió: 06/30/2017

I have been trying to learn to code programs for more than 2 decades. Currently I am reading within Emacs the An Introduction to Programming in Emacs Lisp book. I figure I should try an introductory book as I never got any real programs off the ground as something requiring more than 2 files. One obstacle is that I find that I misunderstand what the excercises actually want and I usually find it as a deterrant to actually wanting to write the answer.

I understand that programming is actually a discipline that takes time (you cannot learn it in a weekend), but it seems that other having racing past me. I usually am learning it solo, although twice I went to school for it.

I can code a little bit in C and python, but object oriented programming has been more than confusing to me over the years.

Avron

I am a translator!

Desconectado/a
se unió: 08/18/2020

I never looked at that book before but since I learned Lisp already (even though it was 30 years ago), it feels rather easy to read for me. On the opposite, I tried reading a book on Python (that I never learned), I felt miserable there and gave up.

I am also confused with object-oriented programming. For example, I wonder how different a class is from a C structure and associated functions, and a list of preambles of functions provided as interface to the structure.

Magic Banana

I am a member!

I am a translator!

Desconectado/a
se unió: 07/24/2010

In C++, a class only differs from a struct by the fact that its members (variables and functions) are private by default. In a struct, they are public by default. In pure C, can a struct have functions?

Avron

I am a translator!

Desconectado/a
se unió: 08/18/2020

I don't think a struct can have functions in pure C but there could be one or a set of .c files defining functions acting on one or multiple instances of a struct, provided by argument as pointers and a .h file defining the struct, including prototypes of the functions expected to be used elsewhere in the code, and comments describing the use of these functions, mentioning that only those should be used and in which .c should any new function acting on instances of struct should be added.

Perhaps the class is a way to do the same without the need for comments? Still, I like to have comments that explain design principles and when I was writting C I was normally writing such comments (and man pages).

Magic Banana

I am a member!

I am a translator!

Desconectado/a
se unió: 07/24/2010

The point is more to encapsulate, to hide the internals: the public functions are exposed to the "exterior", i.e., it is visible to the code only using the class. Those public functions can call private functions and access private variables, but those are invisible to the exterior. In this way, the class represents a type you ideally interact with in an abstract way, with a minimal interface, totally ignoring how things are implemented underneath. For instance, does the class store the data in an array or in a list? The exterior needs not know such a "detail", which can even be changed without affecting the interface, i.e., the signatures of the public functions.

There are other features to object-oriented programming, of course. Inheritance, for instance. It aims to create specializations of a class, sub-types. For those, it may make sense to access some of the internals of the parent class (to avoid code redundancy, because the specialization does not change that). That is why, at least in C++, besides "public" and "private", there is also the "protected" visibility. The derived classes can access the members (variables and functions) declared in the public and protected sections of the parent class. They cannot access its private section.

And once you have inheritance, you can do polymorphism, i.e., call public functions in a parent class that will actually run different codes in the different derived classes. In this way, you can have a collection of "shapes" (the parent class), call a function "area" on each of them but the actual way the area is computed depends on the specific type of shape (for instance a "rectangle", a "disk", etc.), which derives from "shape" and can implement an area function that overrides that in the parent class (if any; if there is no way to compute the area of a generic shape, then the class "shape" is said "abstract"; only objects of the derived classes can be constructed).