• Amused
  • Angry
  • Bored
  • Busy
  • Cheeky
  • Cold
  • Confused
  • Cool
  • Devilish
  • Fine
  • Hacker
  • Happy
  • In Love
  • Innocent
  • Lonely
  • Nerdy
  • Pensive
  • Roflol
  • Sad
  • Starving
  • Stressed
  • Yeehaw
  • Results 1 to 8 of 8
    Like Tree7Likes
    • 7 Post By Newton

    Thread: [TUTORIAL] Object Oriented Programming - Basics

    1. #1
      PG Xtremist
      I am:
      Pensive
       

      Location
      Lahore, Faisalabad
      Posts
      1,646
      My Consoles
      PC
      My PC Specs
        Monitor: Samsung 2232bw
        CPU: C2D Q6600 @2.4Ghz
        Cooler: Enzotech UltraX
        Motherboard: Asus Maximus Formula
        Memory: 4GB Corsair Dominator 1066Mhz
        GPU: ATI HD 4850 Diamond 500Mhz/750Mhz
        HDD: WD 500GB Black
        Chassis: CM 690
        PSU: CM 600W
        Soundcard: Supreme FX II
        OS: Win 7

      [TUTORIAL] Object Oriented Programming - Basics

      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:
      1. Class
      2. 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.
      Last edited by Newton; 21-02-12 at 09:00 AM.
      shad0w, Shyber, Ottoman and 4 others like this.

    2. #2
      PG Moderator
        Ottoman's Avatar
      Sports Section Moderator
      I am:
      Happy
       

      Location
      Wow Chhowni
      Posts
      5,034
      My Consoles
      PC
      My PC Specs
        Monitor: Samsung 931BW
        CPU: Intel Core 2 Duo E4700@2.66GHz \ Corei5 430M@2.26GHz (notebook)
        Motherboard: Intel DG33FB
        Memory: Kingston DDRII 2x1GB \ 1x2GB
        GPU: XFX Geforce GTS250 1GB (Core Edition)
        HDD: Maxtor 320GB \ 320GB
        Chassis: Cooler Master Gladiator-600
        PSU: Cooler Master 460W
        OS: Windows 7 Ultimate x86
      Jazak'Allah bro. It's an excellent tutorial especially for beginners. I shall be linking anyone, who needs help with OOP, to this thread.
      I highly appreciate your effort. Learnt a few things myself


    3. #3
      LAAM se CHAURHA
      I am:
      Happy
       

      Location
      The Land Of Abaya Babes
      Posts
      6,999
      My Consoles
      PC , PS3
      An excellent and well appreciated effort

      I myself as a programmer will try to contribute in the thread in anyway I can

      On That Day, Mankind Received A Grim Reminder...

    4. #4
      PG Xtremist
      I am:
      Pensive
       

      Location
      Lahore, Faisalabad
      Posts
      1,646
      My Consoles
      PC
      My PC Specs
        Monitor: Samsung 2232bw
        CPU: C2D Q6600 @2.4Ghz
        Cooler: Enzotech UltraX
        Motherboard: Asus Maximus Formula
        Memory: 4GB Corsair Dominator 1066Mhz
        GPU: ATI HD 4850 Diamond 500Mhz/750Mhz
        HDD: WD 500GB Black
        Chassis: CM 690
        PSU: CM 600W
        Soundcard: Supreme FX II
        OS: Win 7
      ^
      i'm not a programmer but it's a hobby

    5. #5
      PG Xtremist
      I am:
      Cool
       

      Location
      Karachi, Pakistan
      Posts
      15,657
      My PC Specs
        Monitor: 40" Samsung 1080p 3D LED (ES6100)
        CPU: Intel Core 2 Quad Q9550
        Motherboard: Intel DP45SG P45 Chipset
        Memory: Kingston 2x2GB DDR3 1333MHz
        GPU: nVidia Geforce GTX570 DF 1280MB GDDR5
        HDD: WD Cavier Black 500GB/32MB + 800GB/64MB
        Chassis: CM Scout
        PSU: Asus 550Watts 3x12V rails
        Soundcard: Onboard (DD Live TOSLINKED to Onkyo HD Audio System)
        OS: Windows 7 Ultimate 64/ WindowsXP SP3
      Very nice job
      Will keep an eye out for...erh...expert advice

      I do OOP for a living





    6. #6
      10th June = Versus - Believe!
        DarkLordMalik's Avatar
      Movies, Music & TV Moderator
      I am:
      Devilish
       

      Location
      Sanghar
      Posts
      8,007
      My Consoles
      PC , XB360 , PS3 , PS Vita , PS2
      A really nice job, i must say. Two thumbs up. I appreciate it as a fellow programmer myself


    7. #7
      Fighting To Succeed
      I am:
      Sad
       

      Location
      New South Wales, Australia
      Posts
      3,209
      My Consoles
      PC , XB360
      Great job.bro. I learned the basics of C++ and OOP. Long time ago and forgot it a little bit. I find this as a user friendly.
      Thanks bro
      follow signature guidelines

    8. #8
      MaN Of LeGeNdS
      I am:
      Stressed
       

      Location
      Transalvania , Valacia..
      Posts
      1,072
      My Consoles
      PC , PS3 , Wii , PS2
      WoW u got me up n runnin...
      [SIGPIC][/SIGPIC]

     

     

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •  
    All times are GMT +5. The time now is 01:36 PM.


    Back to Top Copyright © 2012 PakGamers. All rights reserved.
    Digital Point modules: Sphinx-based search
    A part of Avid Media Network Pvt. Ltd.