Documente Academic
Documente Profesional
Documente Cultură
OBJECT-ORIENTED PROGRAMMING
Author : Calico_Cat
Overview
Object-Oriented Programming is the main feature of many modern programming languages.
This includes, but not limited to : C++, Python, Java, Visual Basic and PHP. It is, needless to say,
crucial for handling larger programs.
What makes this subject in particular challenging to learn is the influx of Information you’ll
receive during the first 10-15 hours of learning it. You’ll learn new items like classes, methods
and members, new programming concepts like Polymorphism, Inheritance and Encapsulation,
and the most challenging part of it, it will take you beyond the scope of what you can do in just
a single programming file.
Also, I would like to put this up front first : There is no simple way of explaining Object-
Oriented Programming. Lectures usually use confusing analogies of real-life objects to explain
the concept to you, which depending on how well you take it, might confuse you even more
and sparks more question than what it answers. However, like I said, there is no simple way of
explaining it, and they’re probably doing their best for you to understand the concept.
We can all probably agree on one thing, the first question that you ask to yourself is “How do i
actually implement this?”.
Note that this guide will only cover the important basics of Object-Oriented Programming and
its interactions with basic programming functions (Input, Output, Decisions), since the full
scope of OOP is way beyond what a few pages of an e-book can contain.
What You Need.
Here are some requirements to follow this guide up until the end :
FAQ
Before going into the actual guide, there are probably some questions regarding
the guide or OOP itself, which the Author will try to answer in this section :
If you have more questions that are not answered here, consider messaging the
author.
What is a Class?
The simplest way to put it is that Classes are a blueprint of an object. They contain
the members and methods necessary to build an Object in your program.
Members and Methods are the data and process contained within a class,
respectively. A Member can be of any primitive or modern data types, but in this
guide we’ll only cover the primitive data types (string, integer, float, double, etc.).
A Method defines the behavior of an object, a procedure, to put it simply.
Methods are bound to Members, thus it also bounds to the Class it is associated
with.
And that’s pretty much all the concepts you need to understand to advance to the
next section, which is the Implementation.
IMPLEMENTATION
Before we get down and dirty programming, to get everyone on the same page,
we’ll use a simple program, which is a two-way conversion of Celcius and
Fahrenheit.
Header File : The declaration of all members and method of the class goes here.
CPP File : The implementation of all members and method of the class goes
here.
Since we need to declare something before using them (this rule also applies to
variables, functions, and modules), we’ll start with the header file first. If you
create it from scratch like I did, it should be an empty file at first, now lets add the
necessary codes to make it functional :
Note that it’s nearly the same as the main program except for the #ifndef and
#define syntax. This should always be included and it should end with
FILENAME_H, in this case, since my file name is Implementation, it ends with
IMPLEMENTATION_H.
Now let’s create our first Class. Note that you can name it anything you want, but
just to make things simple, I’m going to name mine the same as my header file
name :
Note the public and private syntax. That’s what we call Encapsulation. This has a
lot to do and is more relevant when you start learning about Inheritance, but
that’s beyond the scope of this guide. For now, let’s just go over all 3
Encapsulation type :
Public means that anything in the program (including separate files) can access
the method or members that are set to Public.
Private means that nothing in the program can access the method or members
that are set to Private.
Protected means that only class that Inherits from the source of the method or
members can access it.
Since Protected is not going to be used in this program, we’ll settle with the first
two.
Next, we should define the Members of the class. These Members will relate to
the variables stated in the main program- for me it’s Celcius and Fahrenheit.
Again, you can name these Members anything you want, but to keep everything
trackable, I highly suggest following the standard naming format of
newVariableName :
Next, we’ll make our first Constructors and Destructors. What are Constructors
and Destructors, first of all? Well…
Like so :
Done? Now we’ll create our Accessor and Mutator methods. And by this point
you’ve probably seen why so many people find this subject to be really confusing,
considering just how much Information you’re forced to swallow, and we’re not
even half done yet.
You might ask, “Wait a minute, if Mutators are just a smaller Overload
Constructor, what’s the point of using them?”. Well, good question. What I didn’t
tell you is that the value of a Member needs to be updated each time the variable
that’s bound to the Member changes in value. Now, this might not seem like a
problem IF the variable is only used once, but once you get into repetition
structures, Mutators are way more useful since they’re easier to squeeze into
your codes.
After creating your header file, this is probably the first thing you should do. Add
the #include “headerfilename.h” to both your main and class .cpp file.
The things we need to create first are of course, our Constructor and Destructor
files. Remember, Constructors should set your Members to their default null
state, while Overload Constructors should set your Members to the value of their
equivalent Variables. Destructor is basically just an empty function.
Now we can use the Accessors as the substitute for the variables, and
additionally, we can declare a method on the header and .cpp file that will handle
the conversions…
And now, to implement them in our main program, along with the usual decision
structure :
And if you made it this far, congrats! Your program is now complete. All that
remains is to check the output. It should look like this :
That’s it, that’s all she wrote.
APPENDIX
And hey, if you want to try making a program for yourself, I have a couple of
programming exercises you can try :
1. Make a program that prompts the user to input a name and 3 scores :
Assigment, Midtest and Final Test score. Use Object-Oriented Programming
to process the data and get the average score.
2. Make a program that prompts the user to input 5 names and their ID
numbers. The program then should display the results in pairs through
proper Accessor and Mutator methods.