Click here to Skip to main content
15,891,704 members
Articles / All Topics

OOP Ready for Takeoff

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
5 May 2017CPOL4 min read 5.5K  
OOP Beginners tutorial

Do Not Go Gentle Into That Good Night

As promised, this is my first post for my ongoing programming tutorials. These tutorials are targeting beginners with no specific background. If you’re interested in learning something new, you have come to the right place. Let's build a base by reading and thinking about the concept of programming. I know exactly what you think now… Why do I need to read about some idiotic programming concepts? Let’s start making an awesome 3D game with multiplayer, physics…

I Can See Something That is Green

I was as inpatient as you are right now, but trust me, if you’re new to programming, you’ll understand the development process of games, software, etc. a lot better. Let’s start by looking around you. What do you see? Table, windows, television, laptop, tablet, pictures, grass, etc.
I guess you can see a load of things near you. Most of these things have a specific purpose. For my very simple example, I’ll choose a window as my object of interest.

It’s a Traaaap

We’ve all learned in school that our world is defined by physical constants, limitations, equations, and so on. Maybe you’ve also heard that a computer isn’t as smart as we humans are they can’t think, they don’t have street smarts, but computers are incredible at calculations with 0 and 1. Now, imagine you have to represent our wonderful world with all it’s complexity and the only things you have at hand are the number 0 and 1. Let us all agree this would be impossible and time consuming, so let’s get ready to earn your new superpower which is called OOP. OOP is short for “Object orientated programming”. OOP helps you to represent complex things (objects) so that a computer can translate them to 0s and 1s and use them for all kinds of stuff. OOP is not a language, it’s not bound to a specific language, it is a paradigm.

In the last paragraph, I talked about my window. What is the purpose of a window? You can look through it and observe those cute squirrels or you can open it to refresh your room. Let's say you had a party at your flat, you woke up and realised that somebody probably you... smashed your window. You need to buy a new one. So before we can go to buy a new one, we need more information about that damn window.

What are the dimensions of the window?
What is the color of the frame?
Which kind of type is the window?

So on and so forth.

Let's add to our notes:

We need a window which we can open and look through. The old one has a white frame and the manufacturer was “SkyGlass”. The height and length is 120cm / 60cm. It was 5cms thick.

With this simple phrase, we defined our first object and now we can try to use OOP. You can celebrate, but please not too hard, you can’t afford to break another window. Let's get serious, we need to translate our human readable phrase so that a PC can work with it. The PC needs some sort of plan / blueprint how you want to store the data / properties of our window.

Finally Code…

Because we want to use fancy / correct words like all the other talented programmers out there, let us use “class” instead of plan / blueprint. A class holds all the information we need for our object - let us call them properties (Length, Height, Width, Manufacture… ) and its functions and let's stick with “methods” instead of functions. We will write a basic class together. I chose C# for this tutorial.

C#
public class Window 
{ 
    // object properties 
    public int Length { get; set; } 
    public int Width { get; set; } 
    public int Height { get; set; } 
    public string Manufacturer { get; set; } 
    public Color { get; set; } 

    // object methods public void ObserveSquirrels() 
    { 
       // do something here but don't forget to 
       // observe those damn cute squirrels 
    } 

    public void OpenToRefreshRoom() 
    { 
       // open the window to refresh this room 
    } 
}

For a beginner, there is so much going on above, it doesn’t matter if you do not understand a single line. We will walk through all the code together and I’ll explain everything in the next tutorial. After the next tutorial, the code will be easy cheesy.

If you read some of it or even all of it, I just want to thank you. I’m not your tutor, but you should reward yourself.

If you have any questions or if you have feedback, just leave a comment or write me via my contact page.
Thanks a lot!

Max

This article was originally posted at https://monomax.blog/2017/05/05/oop-ready-for-takeoff

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --