OOP Concepts in C++: OOPSIE - Casady-ComSci-Seminar/Seminar-Notes GitHub Wiki

Abstraction:

Abstraction creates a separate set of method stubs without code that can be used in multiple classes, yet altered internally to fit the needs of the class.

Abstraction in C++ is actually much easier to use due to the header files essentially being a form of abstract class, and the #include command allowing for seamless integration of multiple files within one.

Polymorphism:

Polymorphism allows the user to write different code in different methods with the same name.

The lack of rigid definitions of methods in header files can easily allow for polymorphism to occur, though having different call methods present in both the header and the .cpp files is essential, so polymorphism has not seen much usage in our class thus far. (e.g. public int DataScanner::add(int newValue) vs public int DataScanner::add(int newValue, int index))

Inheritance:

Inheritance generally allows the user to take methods from "parent classes" to incorporate into their new classes without needing to type the same code more than once. This also allows the user to take only some of the methods from a class without adding the clutter of all of them, and allows the usage of the methods without having to repurpose them into the current class.

C++ utilizes inheritance in the same way with zero difference from Java.

Encapsulation:

Encapsulation is classically used as a way to wrap up data into a class with methods that can utilize said data, thus preventing the program to have to deal with numerous floating bits of code or a gigantic megafile containing every aspect of the project at once. Also incorporated into this idea is the concept of data hiding, which hides data behind the private wall and only allows it to be accessed via getter and setter methods (further requiring the encapsulation of all such methodology in a unified location separate from the masses, preventing an overload of memory requirements).

In C++ specifically, the user must be consistently creating new objects and deleting that which they do not need using deconstructors to avoid a memory overflow, since C++ does not have automatic garbage collection like Java.

Data Hiding:

Take, for example, these attributes:

public int data private int myData protected int shieldData

Were you to attempt to access them by saying: data = 0; myData = 0; and shieldData = 0; you would receive very different responses from the compiler.

For data, the statement would be perfectly acceptable, regardless of where you said so, be it in the class itself, or another class where data's class is being integrated.

myData would reject any attempts to change it as long as you are not in its original class. For the purposes of C++, this includes transferring from the header file to the cpp file, and thus one would need to either avoid defining the private attribute in the header file, or write in getters and setters to work around the issue.

shieldData could only be manipulated within its original class, unlike data's ability to be universally manipulated, yet still more accessible than myData which cannot be manipulated at all without getters and setters.