OOP with C++ and Java
As
a student, I tend to stay in line with what topics and methods that were first
taught to me at a young age. As I grew, learning new things from different
sources of information, the quirk of curiosity grew in my ever-expanding
arsenal of traits and identity. Going into the field of software development,
the majority of what I thought revolved around using the programming language
we all are familiar with, named Java.
All
of us students of SAIT have come learn a lot about Java, either adoring or hating
it at the end. However, there is a plethora of other programming languages such
as Python and JavaScript. C++ is the language that has peeked my interest as
its predecessors in the C family have existed since the 70s. Out of curiosity,
I wondered how different will making object oriented programs be between Java
and C++. Allow me to share my journey in quelling said curious thought.
If
you are using Visual Studio Code as your IDE to code with C++, make sure to
have a compiler like MinGW installed in your system and have extensions
installed in VSC to compile a C++ file. Would recommend watching this video in
how to do it:
Link:
(https://www.youtube.com/watch?v=9VE7p-he4fA)
How I first started coding C++ is
watching a video on a beginner’s course to C++. Out of the numerous tutorial
videos on YouTube, I chose to watch this:
This
video showed me how different C++ makes their main driver class, or main
function as it was called in the video. It starts with an import of a library
called <iostream> using the #include keyword. This library is a header
file library that lets me work with input and output of objects. The next like
is the data type and the name of the main function of the program. As seen in
the picture below:
We can at least be relieved that the way coding blocks are formatted is very similar to Java’s and most other static programming languages. One thing majorly different is not having a class name be the same as the file name. Having a different name does not affect the compiling and running of the file.
The
first line of code I learned was what every coder learns in their first ever
experience with a programming language, printing out “Hello World” into a
console or terminal window. Here is how we first learned to do that exercise in
Java:
For
C++, the difference is apparent on how it looks. It confused me as to why it
was structured this way, seeing as I was used to the Java coding structure and
format. This is how one writes the code to print “Hello World” to the console:
To
break it down: There is a thing called namespace which is a declarative region
that provides a scope to the identifiers inside it. The ‘std’ means standard,
which refers to the C++ standard library. The double colon (::) is a scope
operator that tells the compiler which class/namespace to look in for an
identifier. The ‘cout’ syntax means character output, which is used along with
the insertion operator (<<) to display a stream of characters. Though
writing std on every comment line for printing a statement is kind of a pain
and not clean looking for the code. A trick I learned is writing this below the
import iostream line so you don’t need to put std for every print line:
Now
that I had a grasp of the naming convention, format and syntaxes that C++ uses,
I tried to look into how this programming language makes object oriented
programs and comparing it to how Java makes it. Having read through simple
guides to start C++ OOP and watching through many tutorial videos, this one
taught and explained the topic the best in my opinion:
Link: (https://www.youtube.com/watch?v=wN0x9eZLix4)
Like
Java, C++ also has 3 access modifiers: Public, Private and Protected. The
difference though between Java and C++ regarding these modifiers is how they
are implemented within a class or method. Instead of putting the modifier along
with the name of the class, one must put those modifiers in a certain area
within the body of the class or method. As seen in this example:
I
actually liked this method more than specifically putting the access modifiers
to each variable or method because it is easier to see which are allowed for
public use, which ones are private and should only be used within a class, and
which ones are protected and can only be viewed by any following subclasses of
the superclass.
Encapsulation
and Inheritance works the same Java, such as one can privatize variables and
set getters and setters to retrieve the values of said variables, although the
formatting for how inheritance works is a bit different compared to how Java
does it. To have a class inherit to a parent class, one just needs to put a
colon then the name of the parent class.
The
added public access modifier next to the parent class in the image above allows
the child class, named the Teacher class, to be able to access methods that is
within the parent class named Employee. The addition is necessary because when
a class is created, it is private by default. One also needs to put an
inheritance line next to the child class’s constructor to use the parent
class’s constructor.
The program I followed in making
through the example that the programmer in the video made does show what C++
can do using OOP and how one can use it. While the formatting and the way of
syntax is quite different, along with its naming conventions, the overall
functions of OOP is very similar to Java, so one who wants to get into C++
using OOP would be able to pick this up quick.
I have only scratched the surface of
the potential of C++. Only in OOP but its many features that may not be in
Java. The faster processing and the simplicity of code format has also left me
wanting to learn more about the programming language, and I would recommend for
any reader of this blog to do the same. Don’t be intimidated by any of the more
advance features of C++, but take it slow and have fun while you are trying to
learn all that you can. Thank you reader and I hope you can share my curious
experience.
Comments
Post a Comment