20211018080620 - Objects in C
We can use C for Object-Oriented Programming (OOP). Although C does not support OOP natively, we can implement the missing features ourselves to support it. # Classes We count with the keyword `struct` in the C language to create custom data types (a.k.a. structures) containing other datatypes as members. In OOP, classes are also custom data types with two types of members: - attributes (or data members, or member variables), and - methods (or member functions). When using C's structures, we can implement attributes as variables and methods as function pointers. ## The `Dog` class Consider that we want to create a `Dog` class that has the following members: - `name`: a string containing the name of the dog. - `play()`: a function that prints on the screen that the dog is playing. - `bark()`: a function that prints on the screen that the dog is barking. ``` .----------------. | Dog | |----------------| | + name:char* | |----------...