Monday 3 November 2014

Objects in java

  • Object may be defined as real time entity or run time entity.
  • An object is an instance or variable of type class.
  • Using objects, we can access the functions and variables of the class.
  • Every object can have data and functions.
Creating objects:
              Syntax:  class_name obj_name=new class_name();
              Example: Program p=new Program();
where Program is the name of the class and p is object.

If the function is declared as static,it can be accessed without using an object

Introduction to JAVA

Java is an purely object oriented language initially developed by Sun microsystems in 1991.Java is called purely object oriented language,because the main function resides inside the class, whereas C++ is called as partially object oriented language or middle level language.

Features of Java:

  •     Simple
  •     Secure
  •     Architecture neutral
  •     Platform independent
  •     Object oriented
  •     Robustness
  •     Dynamic

Conventions followed by JAVA:
     In java,name of the class in which the main function resides should be same as the file name and it is case-sensitive.
    There is no need of IDE to compile and run java programs.It is possible to compile and run a java program using Command prompt itself.But one can use certain IDE(Integrated Development Environment) to compile and run the java program.
Structure of java program:
    Packages, if required
    Class declaration
    Main function
Printing on the console screen:
       In java,to display some messages on console screen, System.out.println(); or System.out.print(); methods is used.
Example:
              System.out.println("Welcome");
              System.out.print("HI");
Sample java program:
Class Hello
{
         public static void main(String args[])
         {
                System.out.println("HELLO WORLD!!!!!!");
         }
}


  •                           The main function of JAVA is defined as static.The keyword static enables a programmer to call the function without using objects.
  •                           The java program can be typed in a notepad can be saved as "Filename.java".It can be compiled as javac Filename.java.To run the java program, java Filename command is used.

javac compiler:
                      The javac compiler is used to compile the java program.Once the java program is compiled,the byte code and class file is generated which can be used to run the java program under different platforms.

 
 

Friday 3 January 2014

Operator overloading

The ability to create new meaning to an existing operator is called operator overloading.Various operators involved in operator overloading are:

  • Mathematical operators like +,-,*,/,% 
  • Relational operators like <,>,<=,>=,!=
  • Logical operators like &&,||,!
  • Access operator  like [,],->
  • Assignment operator  =
  • Stream I/O operator <<,>>
Operators that can't be overloaded:
  1. Scope resolution operator
  2. Sizeof operator
  3. Conditional operator
  4. Dereference operator
Syntax for operator overloading:

Return_type class_name :: operator operator_to_be_overloaded(argument list)

Example: void sample::operator ++()
{
         //statement;
}

Operator overloading can be classified into two types:

  • Unary operator overloading
  • Binary operator overloading
Program for Unary operator overloading:
#include<iostream.h> 
          #include<conio.h>
         class sample
         {
              private:
                        int val1,val2;
              public:
                       sample(int x,int y);
                       void operator ++();
                       void operator ++ (int x);
                       void display();
         };
        sample::sample(int x,int y)
        {
              val1=x;
              val2=y;
        }
        void sample::operator ++()
        {
             val1++;
            val2++;
         }
         void sample::operator ++(int x)
        {
             val1++;
             val2++;
        }
        void sample::display()
       {
            cout<<"\n value1"<<val1;
            cout<<"\nvalue2"<<val2;
        }
        void main()
        {
            sample s(10,20);
            cout<<"\nbefore overloading";
            s.display();
           ++s;
           cout<<"after overloading prefix form";
           s.display();
          s++;
          cout<<"after overloading postfix form";
          s.display();
      }

Destructor

Destructor:

Destructor is a special function used to remove the memory allocated by an constructor to an object.It gets executed ,when the object gets deleted.

Syntax:

~Class_name()
{
        //body of destructor
}

Example:
~sample()
{
    delete v;
}

Program to illustrate the use of destructor:

#include<iostream.h>
#include<conio.h>
class sample()
{
   private:
         int a;
   public:
         sample()
         {
                     i=1;
                     cout<<"Object<<i<<created"
                     i++;
         }
          ~sample()
         {
                  cout<<"object gets deleted";
                  i--;
          }
};
void main()
{
      sample s;
      getch(0;
}
    

How to start and save a cpp program

A normal cpp program can  be written in either turbo c or c++ editor.To download Turbo c or c++ editor click the link below

Download turbo c++

To create a new file in c++ editor, Click on file->new.
To open a file click on file->open or press F3.

To save a cpp file click on file->save or press F2.A cpp file must be saved with the extension .cpp

Example: sample.cpp.