Sunteți pe pagina 1din 17

THE QUICK AND DIRTY GUIDE TO

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?”.

Which brings me to the next point…

About this Guide


I wrote this guide from the viewpoint of someone who was, and is still, struggling and learning
more about Object-Oriented Programming. Rather than explaining the concept in details, this
guide will instead explain how to IMPLEMENT said concepts in detail. No confusing analogies,
not a zip zero zilch, just the implementation and details of Object-Oriented Programming.

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 :

- Microsoft Visual Studio.


- Basic Programming Knowledge.
- Understanding of the English Language
- Patience, Perseverance and Willingness to Learn.

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 :

Q : What is this guide all about anyways?


A : The basics of OOP and how to implement it.

Q : What does “Quick and Dirty” means?


A : It means that this guide aims for you to be able to understand and
implement basic OOP after reading this guide with little to no prior knowledge
or the subject.

Q : By basics, do you mean..?


A : The building blocks of OOP, or OOD- Object Oriented Design, if you will. This
includes : Class, Objects, Methods, Members, etc.
Q : And what do you mean by basic programming?
A : Again, the building blocks of a program, which are, but not limited to : Input-
Output, Decision structures, Repetition structures, Data types and Variable
declaration.

If you have more questions that are not answered here, consider messaging the
author.

Without further ado, let’s get busy…

INTRODUCTION TO CLASSES, MEMBERS AND


METHODS

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.

Object is an instance, or an implementation if you will, of a Class.

What are Members and Methods?

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.

Your code should look similar to this :


Done? Now let’s create our first instance of a class. This can be done by clicking
(from the menu bar) : Project -> Add new item. A class needs 2 files to function :

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 :

EDIT : PUT A SEMICOLON AFTER YOUR CLOSING CURLY BRACKET.

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…

A Constructor is a method that runs each time an Object of a Class is created. To


put it simply, it runs whenever you create a new Object. It also puts the
Members of the class into their default null state.
A Destructor is a method that dismantles an Object whenever the process
leaves the function the Object is created in.
Additionally, there’s something called an Overload Constructor, which is a
Constructor that sets the Members of the class to have equal value to whatever
variables you wish to bind them with in the main program. Note that they both
have to have the same data type.

The syntaxes are quite simple, actually. Constructor is ClassName(), an Overload


Constructor is ClassName(data type of the variables linked with the Members),
and Destructor is ~ClassName().

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.

Anyways, back to Accessor and Mutators :

An Accessor is a Method of accessing the value of a Member in a Class.


A Mutator is a mini-Overload Constructor for the Members; It sets their values
to those of the variables independently.

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.

Anyways, your Accessors and Mutators should look like this :


Once you’ve done that, Congratulations! You’ve just made your first header file.
But there’s still more to be done, now we’ll enter the land of the .cpp file…

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, for the


Accessors and
Mutators. Accessors should return the value of its appropriate Member, while
Mutators should set the value to its appropriate Member, much like an Overload
Constructor.
After that’s done, we can go back to our main program and Implement these
Methods there. First you’ll want to create the Object out of the class :

Then declare the Constructor/Overload Constructor. If you use the Overload


Constructor :
If you use the Constructor + Mutators :

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

I’m going to reiterate my statement here : There is no simple way to explain


OOP. The fact that I need 14 pages to explain how to create a simple program
using OOP is the main proof of that. You can’t just write the source code out like
in procedural programs, you need to carefully explain the steps, the items, what
do they do, and how to use them in an effort to make new programmers
understand. Even then, everything I stated here are highly condensed and still far
from detailed. However, I hope the simple explanations help you understand
Object-Oriented Programming better.

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.

|| That’s all folks, good luck!!! ||

S-ar putea să vă placă și