What is object-oriented programming with examples?

What is object-oriented programming with examples?

Technology
Deep Learning God Yann LeCun

Let’s look at 2 programming languages: C++ and Scala. 

In the C++ part, virtual function (for dynamic binding), static member variable, encapsulation, const keyword, and object construction and destruction will become discussed. This part is mainly focusing on the part of C++ OOP that I think is prone to error. Some special features of scala (different from C++) become introduced in the scala part. 

C++ 

Virtual function 

Virtual functions are made for dynamic binding during run time, when the virtual keyword is added to an overridden function of a derived class, the function that will be implemented while being called is dependent on the actual type of the object, not the declared type. 

Static member variable 

Static member variables, together with static member functions, become shared across all instances of the class. A special use of the static member variable is for counting. To be specific, it can become instantiated to 0. When the first object constructed and applied “++” every time a new instance becomes created.

Encapsulation 

The biggest difference between structs and classes is that classes have public, private and protected keywords. In other words, struct is a “public” class. The properties of the class guarantee the protection of privacy:

Namely, other programmers can utilize the class without knowing its structure or the algorithm implemented.

In real-world applications, we often see functions like ”getXX()” to access member variables, and “setXX()” to modify member variables. 

Const keyword 

Const keyword is effective in protecting objects from undesirable modification. For OOP, it can become used for passing by reference to parameters and constant functions. (where no member variables modified). Constant objects can only call constant functions. In contrast, passing by reference without const can serve as a modification tool (for example, swapping values) 

Constructors and destructors 

Let’s discuss 3 things here: self-defined copy constructors and destructors, member initialization list. 

Copy constructors that are the constructor only take a constant reference of the same class. It uses “=” to construct an instance. Problems would occur when there are pointer member variables. Moreover, as default constructor implements member wise assignment. So, self-defined copy constructors should become created to implement deep copy for pointer member variables. 

At the same time, a destructor should be defined to delete all the pointer members so that memory leaks can be avoided, it is worth noticing that when there is a pointer member pointing to itself, a linked list may be created, in this case, a for loop can be applied to iteratively delete the whole list. 

Member variables are actually defined before the constructors’ function body(by default or member initialization list), if the member initialization list is not used. Errors occur. When member variables(objects) don’t have a default constructor or operator= is by default (as it may lead to memberwise shallow copy). 

Scala 

Scala is a pure functional language (which means that everything including functions and primitive types in scala is an object). Different from C++, the subclasses of which are usually completely separate, linking two subclasses together through “mutually recursive definition”. The meaning of “mutually recursive definition” can become illustrated in an example commonly used in the topic of programming languages:

The example consists of an abstract base class Nat(natural numbers) and two subclasses, Zero and Succ(n : Nat) (some natural numbers’ successor) → 4, for instance, is succ(succ(succ(succ(zero)))) 

In conclusion from my perspective, programming languages are a way for human beings to communicate with computers. OOP is thus, a “computerized” representation of the real-world object. 

What is object-oriented programming with examples?

Technology