Thursday, July 23, 2020

Object Oriented Programming Fundamentals


Properties of Object Oriented Programming Language

The programming model "Object Oriented Programming" is based on the following properties:

  • Class :The abstract definition of a particular entity type(real life), which include encapsulated definition of variables and associated functions. Example: Chair, Table, Football, Student etc.


  • Object :An element defined under a Class and exist in the real world and also can be distinguishable from other objects. A student Ram under the class Student will be treated as a student.


  • Encapsulation :Integration of variables and functions in a single unit.


  • Data Hiding :Control in terms of accessibility of member variables and functions.


  • Inheritance :The way of derivation of existing properties from one class into another class


  • Polymorphism :Multiple view of something or the capacity to perform multiple task by same(named) function.



NOTE:

1. Member Variable: Any Variable under a class.

2. Member Function: Any Function under a class.

Consider the class with following specifications
  1. Name of the class: My_Integer
  2. Member variable: x as an integer
  3. Member functions:
    1. Read_Integer()
    2. Show_Integer()


Java Implementation of class My_Integer.

Implementation 01
Program Reference: Java_Programs/ Day_02/ Prog_01.java
import java.util.Scanner;
import static java.lang.System.out;

class My_Integer
{
  public int x;
};

class Integer_Addition
{
  public static void main(String args[])
  {
    My_Integer a, b;
    a = new My_Integer();
    b = new My_Integer();
  
    Scanner S = new Scanner(System.in);

    out.print("\nPlease Enter an Integer: ");
    a.x = S.nextInt();

    out.print("\nPlease Enter another Integer: ");
    b.x = S.nextInt();

    S.close();

    out.print("\nResult of Addition is: ");
    out.println(a.x+b.x);
    out.print("\n\n");
   
    return;
  }
};


Output:
$ javac Prog_01.java
$ java Integer_Addition

Please Enter an Integer: 12

Please Enter another Integer: 10

Result of Addition is: 22


$

 
Implementation 02
Program Reference: Java_Programs/ Day_02/ Prog_02.java
import java.util.Scanner;
import static java.lang.System.out;

class My_Integer
{
  public int x;
  public void Show_Integer()
  { out.print("\nThe Value is: "+x); }
};

class Integer_Addition
{
  public static void main(String args[])
  {
    My_Integer a, b, c;
    a = new My_Integer();
    b = new My_Integer();
    c = new My_Integer();
  
    Scanner S = new Scanner(System.in);

    out.print("\nPlease Enter an Integer: ");
    a.x = S.nextInt();

    out.print("\nPlease Enter another Integer: ");
    b.x = S.nextInt();

    S.close();

    c.x= a.x+b.x;
    c.Show_Integer();
    out.print("\n\n");
   
    return;
  }
}; 


Output:
$ javac Prog_02.java
$ java Integer_Addition

Please Enter an Integer: 12

Please Enter another Integer: 10

The Value is: 22

$

Implementation 03
Program Reference: Java_Programs/ Day_02/ Prog_03.java
import java.util.Scanner;
import static java.lang.System.out;

class My_Integer
{
  public int x;

  public void Read_Integer()
  {
    Scanner S = new Scanner(System.in);

    out.print("\nPlease Enter an Integer: ");
    x = S.nextInt();
  }

  public void Show_Integer()
  { out.print("\nThe Value is: "+x); }
};

class Integer_Addition
{
  public static void main(String args[])
  {
    My_Integer a, b, c;
    a = new My_Integer();
    b = new My_Integer();
    c = new My_Integer();
  
    a.Read_Integer();
    b.Read_Integer();
    c.x= a.x+b.x;

    c.Show_Integer();
    out.print("\n\n");
   
    return;
  }
}; 


Output: 
$ javac Prog_03.java
$ java Integer_Addition

Please Enter an Integer: 12

Please Enter an Integer: 10

The Value is: 22

$

 

C++ Implementation of class My_Integer.

Implementation 01
Program Reference: Cpp_Programs/ Day_02/ Prog_01.cpp
using namespace std;

#include<stdlib.h> 
#include <iostream> 
class My_Integer
{
  public: int x;
};

int main(void)
{
  My_Integer a, b;

  system("clear"); 

  cout<<"\nEnter an Integer: ";
  cin>>a.x;

  cout<<"\nEnter another Integer: ";
  cin>>b.x;

  cout<<"\n\nResult of Addition is: ";
  cout<<a.x+b.x;
  cout<<"\n\n";

Output:
$ g++ Prog_01.cpp
$ ./a.out
Enter an Integer: 12

Enter another Integer: 10


Result of Addition is: 22

$



Implementation 02
Program Reference: Cpp_Programs/ Day_02/ Prog_02.cpp
using namespace std;

#include<stdlib.h> 
#include <iostream> 
class My_Integer
{
  public: int x;
  void Show_Integer()
  {  cout<<"\nThe Value is: "<<x;  }
};

int main(void)
{
  My_Integer a, b, c;

//  system("clear"); 

  cout<<"\nEnter an Integer: ";
  cin>>a.x;

  cout<<"\nEnter another Integer: ";
  cin>>b.x;

  c.x= a.x+b.x;
  c.Show_Integer();
  cout<<"\n\n";
}

Output:
$ g++ Prog_02.cpp
$ ./a.out

Enter an Integer: 12

Enter another Integer: 10

The Value is: 22

$



Implementation 03
Program Reference: Cpp_Programs/ Day_02/ Prog_03.cpp
using namespace std;

#include<stdlib.h> 
#include <iostream> 
class My_Integer
{
  public: int x;

  void Show_Integer()
  {  cout<<"\nThe Value is: "<<x;  }

  void Read_Integer()
  {
    cout<<"\nEnter an Integer: ";
    cin>>x;
  }

};

int main(void)
{
  My_Integer a, b, c;

  a.Read_Integer();
  b.Read_Integer();

  c.x= a.x+b.x;
  c.Show_Integer();
  cout<<"\n\n";
}

Output:
$ g++ Prog_03.cpp
$ ./a.out

Enter an Integer: 12

Enter an Integer: 10

The Value is: 22

$