Intro to OOP
Hey guys. Today I will be writing a tutorial on Object Oriented Programming(OOP) in C++. This
will hopefully help someone learn the heart of C++ and will help me reinforce my own knowledge.
The three major concepts of OOP are: encapsulation, inheritance, and polymorphisms. There's a
mouthful! This tutorial will explain all three.
Encapsulation
Encapsulation in programming refers to the containment of information. In OOP, you want to
create objects that contain information about something in them. This information should be
visible only to that class. This is the basis of encapsulation. In other words, all information in an
object should ONLY be visible to that object, no other object should be able to see them.
Here is an example of a program that does not demonstrate encapsulation:
#include <iostream>
using namespace std;
class NoEncap{
public:
-----int x;
};
int main(){
------NoEncap ob;
------ob.x = 42; //Valid
------cout<< ob.x <<endl; //Valid
------cin.get();
------return 0;
}
In the above example, the instance variable 'x' is declared public by the public label.The public label
means that all variables after this label (in this case only x), will be openly accessible to all other
objects in your program. This is demonstrated in the example. In main, I can access x from ob using
the dot operator. This is not good because I don't want to be able to change the state of my object
(which is determined by those instance variables), without using the object's methods. This is
encapsulation. In OOP I want to encapsulate all variables of an object so that they can only
modified by that object's methods. Here is an example of the same program above, that follows
encapsulation:
#include <iostream>
using namespace std;
class Encap{
-----int x; /*x is now private, because information in classes are private unless specified otherwise*/
public:
-----int getX() { return x; }
-----void setX(int newX) { x = newX; }
};
int main(){
------Encap ob;
------ob.x = 42; //Invalid. Causes an error!
------cout<< ob.x <<endl; //Invalid. Causes an error!
------ob.setX(42); //Valid, while x is declared private, setX and getX are declared public
------cout<< ob.getX() <<endl; //Also valid. getX() is public and therefore accessable outside itself
------cin.get();
------return 0;
}
Notice, now x is declared as a private variable and the two new methods are public. In this way,
I have access to the methods that alter my object's variables, but I don't have direct access to the
variables themselves. Since the x is declared private, only methods from inside the object can
access it. This is why getX() can access x without a problem. private makes information invisible to
everything outside the class.
In a well developed object, the only public methods are those that the programmer needs to alter
the object. The rest should be private. Take this next example. I will make a simple class that takes
two numbers and adds them.
#include <iostream>
using namespace std;
class EncapPlus{
-----int x;
-----int y;
-----//Both x and y are private variables
-----int calculateSum(){ //This is a private method. It can only be called inside the class.
----------return x + y;
-----}
public:
-----void setXY(int newX, int newY){
----------x = newX;
----------y = newY;
-----}
-----int getSum(){
---------- int theSum = calculateSum(); //Allowed because call is made from inside EncapPlus
----------return theSum;
-----}
};
int main(){
-----EncapPlus obj;
-----obj.setXY(4,16);
-----cout<< obj.calculateSum() <<endl; //This line is an error! calculateSum is private!
-----cout<< obj.getSum() <<endl; //Valid because getSum() is public.
-----cin.get();
-----return 0;
}
Notice that calculateSum is private, and therefore can only be called by methods declared within
EncapPlus. The only public methods in this class, are the ones the programmer will use to
manipulate the class. This is the heart of encapsulation
Encapsulation is important because it lets you create easy to use classes. As a programmer, you do
not need to know how the class does what it does. You simply need to know the interface of
the object. By interface, I mean the public methods the programmer will use to manipulate the
object. For instance, in the above example, I would not need to know how given two numbers,
EncapPlus adds two numbers. I would only need to know that if I call getSum(), I will get the sum.
This seems redundant with addition, but let me give you another example. Say I'm making a game
of asteroids. I need to draw a Ship on the screen. I would make a public method called makeShip(). I
would not need to know how the ship is made, I would only need to know that when I call
that interface method, a ship will be made.
<continued on next post...>