Object orientied programming can be done in any language, even machine language or assembly language. It can be done quite conveniently in any language with structures and type definitions.
The decision to use a particular language should not be because "it is object-oriented". It should be based on practical considerations such a portability, performance, and needs dictated by libraries to be used. For example, Eigen is a C++ library for linear algebra. If you need to use Eigen, then you need to use C++. If you can use BLAS and LAPACK instead, then you can choose between C, C++, and Fortran. Other languages may have interfaces to BLAS and LAPACK as well, such as NumPy for Python.
#include <iostream> // cin, cout, cerr #include <sysexits.h> // Standardized exit codes EX_OK, EX_USAGE, etc. using namespace std; class animal_t { private: double weight; public: animal_t(void); double get_weight(void); void set_weight(double); void print(void); }; // Constructor animal_t :: animal_t(void) { weight = 0.0; } // Accessor double animal_t :: get_weight(void) { return weight; } // Mutator void animal_t :: set_weight(double new_weight) { weight = new_weight; // Or this->weight = new_weight; } void animal_t :: print(void) { cout << weight << '\n'; } int main() { animal_t hedgehog; hedgehog.print(); hedgehog.set_weight(2.4); hedgehog.print(); cout << hedgehog.get_weight() << '\n'; return EX_OK; }
#include <stdio.h> #include <sysexits.h> typedef struct { double weight; } animal_t; // Constructor void animal_init(animal_t *animal) { animal->weight = 0.0; } // Accessor double animal_get_weight(animal_t *animal) { return animal->weight; } // Mutator void animal_set_weight(animal_t *animal, double weight) { animal->weight = weight; } void animal_print(animal_t *animal) { printf("%f\n", animal->weight); } int main(int argc,char *argv[]) { animal_t hedgehog; // Constructor must be called explicitly, unlike C++ animal_init(&hedgehog); animal_print(&hedgehog); animal_set_weight(&hedgehog, 2.4); animal_print(&hedgehog); printf("%f\n", animal_get_weight(&hedgehog)); return EX_OK; }
The syntax object.function(arg);
is nothing more than
a different kind of abstraction. The variable object
is an argument to function
, but this object-oriented
syntax highlights the fact that it is an object of the class to
which function
belongs. To the compiler, this syntax
has exactly the same meaning as function(&object, arg);
.
It's a common misconception that choosing a so-called object-oriented language will result in an object-oriented program. This is false. An object-oriented design can be implemented in any language, and there are many programs written in object-oriented languages that do not follow object-oriented design methods at all.
Consider the C program and the Java program below. While C is not considered an object-oriented language, and Java is considered among the strictest of object-oriented languages, neither of these programs follows object-oriented design. The C program is a classic example of what we call spaghetti code, i.e. code without structural organization. The Java program is simply spaghetti code wrapped in a class. As this Java code is written, the public class variables are essentially global variables.
#include <stdio.h> #include <sysexits.h> void sideEffect(void); int global_variable1, global_variable2, global_variable3; int main() { global_variable1 = 1; global_variable2 = 2; sideEffect(); printf("%d\n", global_variable3); return EX_OK; } void sideEffect() { global_variable3 = global_variable1 + global_variable2; return; }
import java.io.*; import java.util.Scanner; class spaghetti { static int global_variable1, global_variable2, global_variable3; public static void main(String args[]) { global_variable1 = 1; global_variable2 = 2; sideEffect(); System.out.printf("%d\n", global_variable3); return; } public static void sideEffect() { global_variable3 = global_variable1 + global_variable2; return; } }