Friday, January 10, 2014

Basic part-6 | Classes & Objects

Hello... friends, Today we are going to discus about classes & objects in the python programming. So before going to start let me clear out that the python is a object oriented programming ( OOP ) so here i am not explaining much about oop concept what actually oop means is so in sort if i explain you that we can create our own class , we can define class variables, we can inherit the characteristics from one class to other class bla bla ....

Object:
So here is total concept of object, Actually we need to know what an object is basically an object is an entity defined by a class containing attributes and methods which can be inherited and also inherit functionality from others in various scopes. Object simply writing reusable code so that we can make the development process faster and more efficient.

first how to create a class, In python we just create a class just by the keyword class & than class name so let's take an example to know about class construction in python

 in this example we can see we create a class with name Calculator & inside this class we first of all need to define class constructor, now as we create as in our previous tut create a function & then simply need to call it outside the function . Now let's see how this program executed

 in our script as we can see we want to add & multiply two numbers as we executing our script we can see the output as shown.

Now let's see how inheritance works in class. Now we are going to take an example of Scientific Calculator which is using the value of from the class calculator. and we want the power using this scientific calculator then let's see our script how it works in a script

 we need to just defined a class & call this previous class inside this newly created class & in this class we can call the functions of the class. Now inside this we just need to call the function simply as shown we create a function power inside our class scientific & now simply call this class method. So let's see what is the output of our script.

as we can see we want the 2 to the power 3 & we get the output 8 here .

Exercise : 
1. Create a class with the name employee & take output as record like name of employee , Salary , id numbers etc.  very simple code using class.
2. Create a inheritance class with name friends & simply call the two other classes in same class named contact & email . here create contact & email as two different class.

No comments:

Post a Comment