October 27, 2013

Interface and Implementation in OOP

Interface is a set of functions of an object that he wants to expose to other objects. Interface of an object provides us the list of available functions.
As we discussed previously that data and behavior of each object is hidden in that object itself so we have to use the concept of interface of the object to expose its behavior to outer word objects.
· Different objects may need different functions of an object so interface of an object may be different for different objects.
· Interfaces are necessary for object communication. Each object provides interface/s (operations) to other objects through these interfaces other objects communicate with this object.
Example – Interface of a Car
· Steer Wheels
· Accelerate
· Change Gear
· Apply Brakes
· Turn Lights On/Off
Example – Interface of a Phone
· Input Number
· Place Call
· Disconnect Call
· Add number to address book
· Remove number
· Update number

It is actual implementation of the behavior of the object in any Object Oriented language.
It has two parts,
· Internal data structures to hold an object state that will be hidden from us it will store values for an object data members.
· Functionality in the form of member functions to provide required behavior.
Examples of Implementation
  1. Gear Box in car system
Consider object Gear Box in car system it has a certain structure and functionality. When this object will be implemented it will have two things,
· Physical structure of the gear box
· Functionality implemented in this structure to change gear.
Both these things are part of implementation.
So it has,
· Data Structure in the form of Mechanical structure of gear box
· Functionality mechanism to change gear

  1. Address Book in a Phone
Similarly take the example of contact details saved in the SIM of a phone, In that case we can say physical structure of SIM card as Data Structure
And Read/write operations provided by the phone as Functionality.
 
As discussed earlier we only show interface of an object to outside world and hide actual implementation from outside world. The benefit of using this approach is that our object interface to outside word becomes independent from inside implementation of that interface. An object may have more than one interface.
This is achieved through the concepts of encapsulation and information hiding. Interface and implementation are separated from each other to achieve Information Hiding.
 
Real Life example of separation of interface and implementations
Driver has a standard interface to drive a car and using that interface he drive can drive any car regardless of its model or type whatever engine type it has or whatever type of fuel it is using.
There is another important phenomena in OOP , i.e. ‘messages’.  
Objects communicate through messages they send messages (stimuli) by invoking appropriate operations on the target object. The number and kind of messages that can be sent to an object depends upon its interface. Objects communicate with each other using messages.
Examples – Messages
A Person sends message (stimulus) “stop” to a Car by applying brakes
A Person sends message “place call” to a Phone by pressing appropriate button



Object-Orientation

Object oriented programming is an essential part of our today’s programming methodology. It is being extensively used in all programming languages due to its key aspects and benefits which will be discussed later on. It is a technique in which we visualize our programming problems in the form of objects and their interactions as happens in real life.

Examples:
We have different objects around us in our real life that interact with each other to perform different operations For example, Human beings, cars, tree, house etc.
Take an example of a person lives in a house. He drives a car. All these are objects and have their own unique characteristics. Take another example of a School; the objects in a school are student, teacher, books, pen ,school bag, classroom, parents, playground etc.

Suppose we want to develop a fee collection system for a school for this we will need to find out related objects and their interactions as happens in real life. In this way we can say that object orientation makes it easier for us to solve our real world problems by thinking solution of the problem in terms of real world objects.
So we can say that in our daily life everything can be taken as an object that behaves in a certain way and has certain attributes. In object orientation we move our concentration to objects in contrast to procedural paradigm in which we simply write our code in functions and call them in our main program.

What is a Model?
A model is an abstraction of something real or conceptual. We need models to understand an aspect of reality.

Model Examples
Highway maps
Architectural models
Mechanical models

OO Models:

In the context of programming models are used to understand the problem before starting developing it. We make Object Oriented models showing several interacting objects to understand a system given to us for implementation.
Object Oriented Model

Objects
Ali, Car, House, Tree


Interactions
Ali lives in the house
Ali drives the car

clip_image025
Object Oriented Model (A School Model)
Objects
Teacher, Student, School Bag, Pen, Book Playground
 
Interactions
Teacher teaches Student.
Student has School Bag, Book and Pen

clip_image029

Object-Orientation - Advantages

As Object Oriented Models map directly to reality as we have seen in examples above therefore,
We can easily develop an object oriented model for a problem.
Everyone can easily understand an object oriented model.
We can easily implement an object oriented model for a problem using any object oriented language like c++ using its features like classes, inheritance, virtual functions etc.

What is an Object?
An object is,
  1. Something tangible (Human being, School, House, Car).
  2. Something conceptual (that can be apprehended intellectually for example time, date and so on…).
An object has,
  1. State (attributes)
  2. Well-defined behavior (operations)
  3. Unique identity

Tangible and Intangible Objects

Examples of Tangible Objects:
Ali is a tangible object, having some characteristics (attributes) and behavior as given below,
Characteristics (attributes)
Name
Age
Behaviour (operations)


Walks
Eats


We will identify Ali using his name.
Car is also a tangible object having some characteristics (attributes) and behavior given below,

State (attributes)

Color
Model

Behavior (operations)
Accelerate
Start Car
Change Gear


We can identify Car using its registration number.

Examples of Intangible Objects (also called as conceptual objects):

Time is an intangible (conceptual) object

State (attributes)

Hours
Seconds
Minutes


Behavior (operations)
Set/Get Hours
Set/Get Seconds
Set/Get Minutes
We will assign our own generated unique ID in the model for Time object. 


Date is also an intangible (conceptual) object

State (attributes)
Year
Day
Month

Behavior (operations)
Set/Get Year
Set/Get Day
Set/Get Month
We will assign our own generated unique ID in the model for Date object.

Let us look at some of the key aspects of object orientation.

Key Aspects:
  • Model is the abstraction of some real word scenario. It helps us to understand that scenario.
  • Object oriented model of any scenario (problem) describes that scenario (problem) in the form of interacting objects.
  • We use Object Orientation because it helps us in mapping real world problem in a programming language.
  • Object Orientation is achieved using objects and their relationships.
  • Properties of an object are described using its data members and behavior of an object is described using its functions.
  • Objects may be tangible (physical) or intangible (also called conceptual or virtual).
  • Generally when we have given a certain problem description, nouns in that problem description are candidates for becoming objects of our system.
  • There may be more than one aspects of an object
  • It is not necessary that every object has a specific role in implementation of a problem there may be some objects without any role, like school parking in our school.
  • It is easier to develop programs using Object Oriented Programming because it is closer to real life.

October 26, 2013

Windows 8.1 Password Resolved

In daily use, we do not really prefer to always type a long password that we have for our live account. Instead, what we can do is, we can very easily alter it or you can say bypass it by applying the following procedure.

Click on the search in the right panel as shown in the figure below.

 

1

 

After you click on search, the following panel will open. Type password. Here you see, as highlighted in the figure below. Click on “Sign-in options”.

 

 2

 

Now, here you have different options, the most suitable and easier way is to have a four digit pin.

Click, on the change button in the “PIN” option which is the third one here.

 

3

 

First it will prompt you to enter your Hotmail account password which you are using to log into your windows.

Enter your password. As i did mine in the following diagram.

 

 4

 

Now, enter your four digit pin number.

 5

 

Like this.

 

 6

 

And here you go, now when you want to log into your windows you will just have to enter the pin and it logs in.

 

October 10, 2013

C program to print different Star Formations using For Loop


Here is the code to print different star formations using for loop.

#include "stdio.h"
int main()
{
    int num,i,j,k,m,n,o,p;
     printf("Please enter a number to print the star formations \n");


     scanf("%d", &num);   
   
for(i=0;i<num;i++)
    {
    for(j=0;j<num;j++)
        {
                printf("*");
        }
                printf("\n");
    }

printf("\n");

j=0,i=0,k=0;
// Hollow Star formation
    for(i=0;i<num;i++)
    {
    for(j=0;j<num;j++)
        {
            if(i==0 || i==num-1)
            {
                printf("*");   
            }
            else if(j==0 || j==num-1)
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }


     // downward descending formation
     printf("\n");
     j=0,i=0,k=0;
     for(i=num;i>0;i--)
        {
            for(j=i;j>0;j--)
            {
                printf("*");
            }
            printf("\n");
        }

printf("\n");
j=0,i=0,k=0;
 
    for(i=num;i>=0;i--)
        {
            for(k=i;k<num;k++)
            {
            printf(" ");
            }
            for(j=0;j<=i-1;j++)
            {
                printf("*");
            }
            printf("\n");
        }


printf("\n");
j=0,i=0,k=0;
// Star formation right angle triangle.
        for(i=num;i>0;i--)
        {
            for(j=0;j<i-1;j++)
            {
                printf(" ");
            }
            for(k=j;k<num;k++)
            {
            printf("*");
            }
            printf("\n");
        }


printf("\n");
j=0,i=0,k=0;
    for(i=0;i<num;i++)
    {
    for(j=0;j<=i;j++)
        {
                printf("*");
        }
                printf("\n");
    }

     return 0;
}











The output of the above code is following:

st

C program to print the hollow stars square

Here is the code for the C program to print the star formation of a hollow square. The program will input an integer value from the user and print the hollow star formations.
Here is the code.

#include “stdio.h”
int main()
{
int num,i,j;
printf("Please enter a number to print the hollow star formation \n");
scanf("%d", &num);
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if(i==0 || i==num-1)
{
printf("*");
}
else if(j==0 || j==num-1)
{
printf("*");
}
else
{
printf(" "); // space is printed ..
}
}
printf("\n");
}
return 0;
}


























Here is the sample output.
star

As can be seen in the output above, for the user input, the hollow star square will be formed. The key to understand the logic of the code is as follows.
The outerloop controls the number of  lines and inner loop controls the number of stars.

October 8, 2013

C program to display integers in Ascending without using Loop

This program run sorts and display the integers in ascending order. It finds the largest and smallest integer values respectively.



#include "stdio.h"
#include "conio.h"


int main()



{


    int a=0, b=0, c=0,d=0,e=0, f=0, grade;
    printf("Enter four numbers\n");
   
    scanf("%d",&a);
    scanf("%d",&b);
    scanf("%d",&c);
    scanf("%d",&d);
   
    if(a>b && a>c && a>d)
    {
           printf("1st value that you entered is largest\n");
           e=1;
    }
    else if(b>a && b>c && b>d)
    {
           printf("2nd value that you entered is largest\n");
           e=2;
    }
    else if(c>a && c>b && c>d)
    {
           printf("3rd value that you entered is largest\n");   
           e=3;
    }
    else if(d>a && d>b && d>c)
    {
           printf("4th value that you entered is largest\n");
           e=4;
    }

// For finding smallest value

     if(a
    {
           printf("1st value that you entered is smallest\n");
           f=1;
    }
    else if(b
    {
           printf("2nd value that you entered is smallest\n");
           f=2;
    }
    else if(c
    {
           printf("3rd value that you entered is smallest\n");
           f=3;
    }
    else if(d
    {
           printf("4th value that you entered is smallest\n");
           f=4;
    }

//Sorting //

    if(e==1 && f==2)
    {
            if(c
            {
                   printf("Sorted order is \n %d %d %d %d", b,c , d, a);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", b,d , c, a);
            }
    }
    else
    if(e==1 && f==3)
    {
            if(b
            {
                   printf("Sorted order is \n %d %d %d %d", c,b , d, a);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", c,d , b, a);
            }
    }
    else
    if(e==1 && f==4)
    {
             if(b
            {
                   printf("Sorted order is \n %d %d %d %d", d,b , c, a);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,c , b, a);
            }
    }
   
    // for e=2
   
        if(e==2 && f==1) // a is smallest, b is largest
    {
            if(c
            {
                   printf("Sorted order is \n %d %d %d %d",a, c , d, b);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", a,d , c, b);
            }
    }
    else
    if(e==2 && f==3)// c is smallest
    {
            if(a
            {
                   printf("Sorted order is \n %d %d %d %d", c,a , d, b);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", c,d , a, b);
            }
    }
    else
    if(e==2 && f==4)  // d is smallest
    {
             if(a
            {
                   printf("Sorted order is \n %d %d %d %d", d,a , c, b);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,c , a, b);
            }
    }
   
    // for c is largest i.e.. e=3
            if(e==3 && f==1) // a is smallest, c is largest
    {
            if(b
            {
                   printf("Sorted order is \n %d %d %d %d",a, b , d, c);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", a,d , b, c);
            }
    }
    else
    if(e==3 && f==2)// b is smallest
    {
            if(a
            {
                   printf("Sorted order is \n %d %d %d %d", b,a , d, c);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", b,d , a, c);
            }
    }
    else
    if(e==3 && f==4)  // d is smallest
    {
             if(a
            {
                   printf("Sorted order is \n %d %d %d %d", d,a , b, c);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,b , a, c);
            }
    }   
   
       
    // for d is largest i.e.. e=4
   
   
   
            if(e==4 && f==1) // a is smallest
    {
            if(b
            {
                   printf("Sorted order is \n %d %d %d %d",a, b , c, d);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", a,c , b, d);
            }
    }
    else
    if(e==4 && f==2)// b is smallest
    {
            if(a
            {
                   printf("Sorted order is \n %d %d %d %d", b,a , c, d);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", b,c , a, d);
            }
    }
    else
    if(e==4 && f==3)  // d is smallest
    {
             if(a
            {
                   printf("Sorted order is \n %d %d %d %d", d,a , b, d);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,b , a, d);
            }
    }       

    getch();
    return 0;
   
   
}


Here is the output.


 

September 25, 2013

Ubuntu Installation using Windows Installer

There is an absolute easy way to install  ubuntu. Let us see step by step procedure to install ubuntu by using ubuntu windows installer.
First download the windows installer by writing following in google and open the first link.
1
Now, click on the Get the installer button.

C program to Read From a File

#include <stdio.h> #include <stdlib.h> void main() {     FILE *fptr;     char filename[15];     char ch;   ...