17 March 2012

Introduction to OOP

So hi to all readers,  I'm Flamur my nickname is Muli
My first post will cover the topic 'Intro to OOP' using C# programming language, C# is used very much today and is fully OOP ( Object Oriented Programming) language, I use Visual Studio 2010 as my IDE, you can use whatever you want. I will start with the explanation of simple classes, some properties and some methods. So before start 'throwing' code here, I will try to explain first what a class is?

Class - A class in OOP is considered one logical entity or object that shows a 'blueprint' of the object itself that has some custom attributes (properties), custom methods (actions), custom events and custom nested types. In short a class is a information entity that can hold custom data, fire (raise) custom events and contain methods in a structural way.

Why to use Classes?
  One simple reason is to keep data structured, assume you have some lines of codes that display to the console (like command prompt) a first and last name of a specific person how would you do it without a class?

....
string firstName = "myFirstName";
string lastName = "myLastName";
....
Console.WriteLine(firstName + " " + lastName);


The code above is a solution of achieving to write a first and last name on the console application, but this applies for one person in this instance for person "myFirstName", imagine it for 30 persons? OK no way to declare 30*2 variables for just 30 persons first and last names (that's more an procedural programming than object oriented), but you can use the same variables (firstName and lastName) and just to change their values, after changing values you could write to the console, but this is still procedural programming rather than object oriented programming, ok it is not a 'must' to use classes but thinking on one question, expect first and last name if you want to add later Age, BirthYear or any other additional attributes how will the code look like? It will grow very much and it would be very difficult to maintain all the static unstructured 'data'.

Class example:

class Person
{
string FirstName {get; set;}
string LastName {get; set;}
}


The class above is very very simple one, it contains just two fields (attributes, properties) the first and last name with a dataType of 'string' (text).

What's next? 
 I will try to explain more about classes  in the next coming posts, I do this only on my free time till then happy days and feel free to comment or share the content.

No comments:

Post a Comment