Hi everyone, a friend of mine who teaches asked me to make some reading material regarding object oriented programming for his students. I've seen a few guys seeking help here on PG now and in the past too so I thought that I should write it and post it here as well.
Objective:
The objective of this tutorial is to get the reader to develop a very basic know-how about what object oriented programming is and to develop a foundation of a few basic concepts related to it.
Target readers/Pre-requirements:
This tutorial is intended for or will be more helpful to people who are already familiar with basic programming and can write simple codes on their own. A simple code would be like printing the following pattern
1
12
123
1234
12345 etc
Programming language:
I won't restrict this tutorial to any specific programming language but the example which I will use will somewhat resemble the syntax of C and JAVA (more like JAVA).
Get ready:
OOP = Object Oriented Programming
C, JAVA = well known programming languages (of course you'll know)
{C} = specific to C
{J} = specific to JAVA
method{J} = function{C} i may use them interchangeably so dont get confused
(some professionals may disagree with the way I introduce objects and classes but this is meant for absolute beginners and as a starting point it is just fine.)
Introduction:
I expect you to be familiar with writing simple code like the pattern I talked about above. A similar example would be to display the multiplication table of a user-input number; and a better example would be to manage report cards of a class examination. We achieve all these using a few variables, loops of our choice and a few conditional statements.
BUT
in some cases, a need arises where we would like to have very a customized way of handling and manipulating data. For example:
- creating a variable type of my own. There are pre-defined variable types like int, short, string, float etc but what if i would like to have a variable which should be able to store a string value and two int values? A simple solution which comes to mind is to somehow group them together (like make a structure {C}).
- I would also like to perform various tasks of my own on my user-defined variable type. The solution of this comes as defining a function which can manipulate the user-defined variable.
This customized or user-defined way of programming seems pretty much straight-forward till now; and, it actually is straight-forward as well.
Now after understanding the above 2 bullet points; lets move on to what is OOP.
Object Oriented Programming:
The whole concept of OOP revolves around two words:
- Class
- Object
Let's just not get worried by the words; let's see what they actually are. I would like you to keep this line in mind and think about it for a while
"A class is a custom-defined variable type; and an object is the actual variable of that custom-defined type."
OMG what did that mean? It's simple:
In a Class, we define how our custom-defined variable should be like i.e. what values should it hold, what functions may be performed on it etc. The Object on the other hand is the actual custom-defined variable which will be holding those values and through it various functions may be performed on those values.
So in short, a Class defines the basis of an Object. How? we will see that shortly.
Caution:
Please guys note that I have referred to an object as a custom-defined variable only for your quick and better understanding. There are some differences between a normal variable and an Object so don't go in front of your teachers calling Objects and variables the same thing.
Short Review:
Till this point I hope that the very basic idea of what a Class and an Object are might have settled down in your minds. Go through the introduction once again and then lets see a little bit more detail about a Class.
Class:
A class is the very definition, the complete foundation of an object. It defines what type of information may be stored in the object and what will be the interfaces of that information with the outside world.
The basic structure of a class is as follows
Code:
Class Name
Variables
Constructor
Functions/Methods
- Class Name : Every class in the working environment will have a unique name of your choice.
- Variables : These are the variables/data/information which the consequent object may be able to hold.
- Constructor : The outside world creates objects using the constructor. The constructor has the same name as the class name(how? we'll see shortly)
- Functions: These are the interfaces via which the outside world may interact with the object data. (how? we'll see shortly)
Now you will be thinking that is alot of information but still how can I do all this. So lets go to the example now.
Example:
In this example I will create a simple class and perform a few functions. Lets take a generic example of an xy-coordinate system. Let's make a class which can handle and manipulate points on the xy-plane.
We all know that every point has an x-coordinate and a y-coordinate and certain things can be done with these coordinates. So lets make a class now
This is just a pseudo code and it may resemble JAVA at certain places.
Code:
//Point is the unique class name here
Class Point
{
//lets write down the required variables to define a point.
int x_coordinate;
int y_coordinate;
//coming to the constructor. Whenever a Point object will be created this will be used; so we will give it 2 parameters to assign them to the desired
//coordinates
//note that the constructor does not have a return type{JAVA}
Point (int var1, var2)
{
x_coordinate = var1;
y_coordinate = var2;
}
//now let's write a function to find the distance between our point and the origin (0,0)
float distanceFromOrigin()
{
float distance = 0;
distance = (x_coordinate - 0)^2 + (y_coordinate - 0)^2; //it was not necessary to subtract 0 here but just for clarity :)
distance = square_root (distance) //this line will not work as it is. you need to replace it with the square-root function/routine/method
//of the respective programming language
return distance;
}
//note how i have no parameters in this method. whenever an object will call this method, the method will use the x and y coordinates
//of that object by default.
}
Now i would like to create objects of the above class and play with them a bit.
Code:
Class Main
{
public static void main (String [] args) //{JAVA}!!!!!! just the void main(void) in C dont get scared
{
//lets create 2 objects of the Point class
Point point1 = new Point (2, 3);
Point point2 = new Point (11, 13)
//i want to find their distance from the origin
float distance1 = point1.distanceFromOrigin();
float distance2 = point2.distanceFromOrigin(); //you can print these too if u want to
//another thing what I can do is
print (point1.x_coordinate); //i can refer to the x coordinate individually :)
}
}
this was a very simple example to demonstrate how to make a class and use objects. As an exercise how about making a function to find the distance between 2 points?
This tutorial may have certain errors and confusions or points which may need further clarification, so just tell me and i will update it.
hope this helped.