What Is Object-Oriented Programming?

This article will provide features and critical aspects of Object-Oriented Programming and its relation with Simula. So have patients to read till the end.

Object-Oriented programming is inherited from Simula. Improved features of Object-Oriented programming was observed in Smalltalk. Thus, Smalltalk is considered to be the first Object-Oriented Programming language. The class concept of object-oriented programming is taken from Simula.

Smalltalk and Object-Oriented Programming

Smalltalk lays the foundation of Object-Oriented Programming. Therefore, object-Oriented programming is one of the components of Smalltalk. Smalltalk compiler provides you with the syntax and semantics of the language used to build programming constructs. 

How to use these programming constructs is depicted by the programming paradigm of Smalltalk. Programming constructs of Smalltalk consists of Object and Classes. These objects and classes are used in the programming paradigm.

System of Object-Oriented Programming

The system of Object-Oriented Programming consists of Objects. Therefore, understanding the system of Object-Oriented Programming behaviour of Objects and their concept is a must.

Understanding Objects

In Smalltalk, every item used is an object. Each Object in Smalltalk possesses certain characteristics. These characteristics decide – processing techniques of these objects, how communication will be made using objects, how these objects will appear, the Object status, and how these objects will be referred.

All objects communicate with each other using message passing techniques. In order to communicate, objects transmit a message.

Smalltalk consists of primitive and non-primitive objects. Primitive objects consist of data types such as integer, and non-primitive objects consist of user-defined objects. Both primitive and non-primitive objects are equal. In Smalltalk, classes are also objects and are the same as primitive object integers.

An object is communicated using its name. Each Object has methods and states, which can not be changed. It is not possible to know the internal state of the objects.

Communicating with Objects

Each Object in the small talk has well-defined behaviour. This behaviour decides the techniques of processing these objects. Message passing techniques are used to communicate with objects. Each Object has the capability of receiving and sending messages.

The message is passed to the Object to accomplish desired functionality. An object may accomplish this functionality either by itself or by communicating with other objects. The message passing technique is used to communicate with and between objects. This message passing technique is an essential component of Smalltalk.

Messages in Object-Oriented Programming  

A message in Object-Oriented programming is a text string that may contain parameters or values required to accomplish desired functionalities. Messages in Object-Oriented programming begin processing and may request desired information.

The text of each message specifies what is required. For example, in Object-Oriented programming message is passed to the receiver, and the receiver interprets the message to accomplish the desired task.

Object-Oriented programming provides the facility of sharing. Sharing introduced the concept of usability. Usability adds clarity and modularity to the programming constructs.

With the help of sharing, programming constructs can be modified significantly. This is because sharing defines the same property to be reinterpreted in the desired terminology. In Object-Oriented programming, common attributes are shared. Furthermore, the common attributes are overridden to adapt as per the desired functionality. Thus, Object-Oriented programming supports adaptation.

Object-Oriented programming supports the concept of Inheritance. Inheritance is sharing of common attributes by classing, subclassing, and superclassing. Thus, Inheritance adds to the functionality of Object-Oriented programming.

Object-Oriented Programming 

ALGOL based Simula is the first programming language that introduced the concept of class. Simula introduced Object-Oriented programming concepts. The ADA programming language introduced the concept of data encapsulation. ADA also introduced the concept of operator overloading.

Object-Oriented programming is related to abstract data types, mechanism of data encapsulation, mechanism of information hiding, and techniques of modularization.

The mechanism of data encapsulation is used to bind data and functions. Object-Oriented programming language protects data using private, public and protected keywords. Encapsulation restricts access to data outside of the class. Encapsulation adds to the understandability of the program.

Objects may bear a relationship with each other, which introduced the concept of Object Composition in Object-Oriented programming. Object composition represents a relationship between two objects using a “has-a” relationship.

In Object-Oriented programming, the concept of Inheritance is used. The Inheritance is used to arrange the classes hierarchically and represent the relationship that objects bear among each other. The methods defined in a class can be reused in a sub-class, and if required, its definition may also be changed.

Object-Oriented programming also introduced the concept of the abstract class. Abstract classes are used to implement their methods, and their objects can not be created. 

Polymorphism is another Object-Oriented feature. Polymorphism is used to represent different forms of the same method. Polymorphism is one of the most critical concepts of Object-Oriented Programming. Object-Oriented programming supports two types of polymorphism – Compile time polymorphism and Run time polymorphism.

Compile-time polymorphism is responsible for function overloading or operator overloading. Run-time polymorphism is responsible for achieving function overriding.

Use of Object-Oriented Programming

Object-Oriented programming is used in Computer networks to establish a client-server environment. Therefore, client-server architecture is best established using the Object-Oriented paradigm.

The object-Oriented paradigm is also used in Distributed Data Management Architecture. For example, the object-Oriented feature is used to define fields that form messages. In addition, distributed Data Management Architecture use Object-Oriented programming to define directory services, security and concurrency control.

Object-Oriented programming is also used in databases. Object-Oriented Programming and relational database management systems are related using Object-Relational Mapping; Visual FoxPro is an example of this mapping. 

Object-Oriented Programming is also used in SOLID. SOLID represents five programming techniques: Single responsibility principle, Open/Closed principle, Liskov substitution principle, Interface segregation principle, and Dependency inversion principle.

C++ Programs to implement Object-Oriented Principles

C++ program to illustrate the use of Objects and Classes

class myCar
{
    
    char my_name[20];
    int my_speed;
    int my_weight;
 
public:
    
    void my_brake()
    {
        
    }
    void my_slowDown()
    {
        
    }
}; 
int main()
{ 
   myCar my_ford;  
}

Code Analysis

This example illustrates the use of object and class. 

C++ program to illustrate the use of inheritance.

#include <iostream>
using namespace std;
class my_Teacher 
{
  public:
       my_Teacher()
       {
           cout<<"Hello I am your Teacher "<<endl;
       }
       
       string my_collegeName = "This article is for beginners ";
};

class my_MathTeacher: public my_Teacher 
{
    public:
    
          my_MathTeacher()
          {
             cout<<"I am your Mathametic Teacher"<<endl;
          }
          string my_mainSub = "this is Maths";
          string my_name = "this is Negan";
};

int main() 
{
  my_MathTeacher my_obj;
  cout<<" My Name: "<<my_obj.my_name<<endl;
  cout<<"My College_Name: "<<my_obj.my_collegeName<<endl;
  cout<<"My Main_Subject: "<<my_obj.my_mainSub<<endl;
  return 0;
}

Output:

Hello I am your Teacher 
I am your Mathametic Teacher
 My Name: this is Negan
My College_Name: This article is for beginners 
My Main_Subject: this is Maths

Code Analysis

In the above example, the use of Inheritance is explained. Inheritance is used to achieve reusability. In this code,  class my_Teacher is the base class and class my_MathTeacher is the derived class.

The drives class can access the data members of the base class. My_obj.my_name is used to access the data member of the derived class and in the same way my_obj.my_mainSub is used to access the data member of the derived class.

my_obj.my_collegeName is used to access the data member of the base class. This is a data member of the base class but it can be accessed using the object of the derived class. 

C++ program to implement Polymorphism.

#include <iostream>
using namespace std;
class my_Add 
{
   public:
         int my_sum(int my_num1, int my_num2)
         {
                 return my_num1+my_num2;
         }
         int my_sum(int my_num1, int my_num2, int my_num3)
         {
             return my_num1+my_num2+my_num3;
         }
};
int main() 
{
  my_Add my_obj;
  
  cout<<"Output is: "<<my_obj.my_sum(10, 20)<<endl;
  
  cout<<"Output is: "<<my_obj.my_sum(11, 22, 33);
  return 0;
}


Output:

Output is: 30
Output is: 66

Code Analysis

This example illustrates the use of polymorphism. In polymorphism, two functions can have the same name but different arguments. In the above example, the call to function is made depending on the number of parameters passed. The function call and the function definition is attached at compile time.

In the above example, the function my_sum(10, 20); my_sum(11, 22, 33); is associated with the definition of the function at compile time.  

#include <iostream>
using namespace std;
class my_A 
{
   public:
         void my_disp()
         {
            cout<<"This is function of Super_Class "<<endl;
         }
};

class my_B: public my_A
{
    public:
          void my_disp()
          {
             cout<<"This function of Sub_Class";
          }
};

int main() 
{
  
  my_A my_obj;
  my_obj.my_disp();
  
  my_B my_obj2;
  my_obj2.my_disp();
  return 0;
}

Code Analysis

In the above example the Run time polymorphism is made, the function call is associated with the body of the function at run time. This is also known as late binding. The function call is deferred till run time. 

In the above example, two display functions are used namely my_disp(); call to this function is made at run time.  

Output:

This is function of Super_Class
This function of Sub_Class

C++ example to illustrate the use of Function Overloading.

#include <iostream>
using namespace std;
class my_Addition 
{
    public:
          int my_sum(int my_num1,int my_num2) 
          {
            return my_num1+my_num2;
          }
          int my_sum(int my_num1,int my_num2, int my_num3) 
          {
            return my_num1+my_num2+my_num3;
          }
};

int main(void) 
{
    my_Addition my_obj;
    cout<<my_obj.my_sum(200, 150)<<endl;
    cout<<my_obj.my_sum(810, 1000, 100);
    return 0;
}


Output:

350
1910

Code Analysis:

This example illustrates the use of function overloading. In function overloading, more than one can have the same name but the number of parameters must be different. The parameters must be different in data types as well as in sequence. 
In the above example, function  my_sum(200, 150); my_sum(810, 1000, 100); have the same but the number of parameters is different, the first function has two parameters and the second parameter has three parameters. The return type of both these functions is the same.

#include <iostream>
    using namespace std;
    class myDemoClass 
    {
        public:
              int mydemoFunction(int myi) 
              {
                 return myi;
              }
              double mydemoFunction(double myd) 
              {
                 return myd;
              }
    };
    int main(void) 
    {
        myDemoClass myobj;
        cout<<myobj.mydemoFunction(1000)<<endl;
        cout<<myobj.mydemoFunction(50005.5106);
        return 0;
    }

Output:

1000
50005.5

Code Analysis:

In this example function overloading illustrates that functions having different parameter list with different data types can be overloaded, these functions can have different return types.
In the above example, the function mydemoFunction(1000); mydemoFunction(50005.5106); have arguments with different data types and the return types of function are also different.

#include <iostream>
using namespace std;
class BaseClass {
public:
   void mydisp(){
      cout<<"This is Parent_Class Function";
   }
};
class myDerivedClass: public myBaseClass{
public:
   void mydisp() {
      cout<<"This is Child_Class Function";
   }
};
int main() {
   myDerivedClass myobj = myDerivedClass();
   myobj.mydisp();
   return 0;
}


Output:

Function of Child Class

Code Analysis:

This program illustrates the use of function overriding. Function overriding is done by keeping the same prototype in the child class. The prototype is made the same by keeping the data type and sequence of parameters the same. In the above example, there are two display functions having the same name. The display function in parent class disp() is exactly the same as disp() function in child class.  
When the function disp()  is called the function of child class is called.

#include <iostream>
using namespace std;
class myBaseClass {
public:
   void mydisp(){
      cout<<"This is Parent_Class Function";
   }
};
class myDerivedClass: public myBaseClass{
public:
   void mydisp() {
      cout<<"This is Child_Class Function";
   }
};
int main() {
   
   myBaseClass myobj = myDerivedClass(); 
   myobj.mydisp();
   return 0;
}

Output:

This is Parent_Class Function

Code Analysis:

This program illustrates the use of function overriding. When a function call is made child function gets overrides. 

C++ program to illustrate the use of Encapsulation.

#include<iostream>
using namespace std;
class myExampleEncap{
private:
   
   int mynum;
   char mych;
public:
   
   int mygetNum() const {
      return mynum;
   }
   char mygetCh() const {
      return mych;
   }
   
   void mysetNum(int mynum) {
      this->mynum = mynum;
   }
   void mysetCh(char mych) {
      this->mych = mych;
   }
};
int main(){
   myExampleEncap myobj;
   myobj.mysetNum(1000);
   myobj.mysetCh('B');
   cout<<myobj.mygetNum()<<endl;
   cout<<myobj.mygetCh()<<endl;
   return 0;
}

Output:

1000
B

Code Analysis:

This program illustrates the use of Encapsulation. Encapsulation combines data members and member functions into a single unit called class. In the above example class myExampleEncap has data member int mynum; char mych; and member function mysetNum(1000); mysetCh(‘B’); mygetNum(); mygetCh();

C++ program to illustrate the use of Abstraction.

#include <iostream>
using namespace std;
class myAbstractionExample{
private:
   
   int mynum;
   char mych;

public:
   void mysetMyValues(int myn, char myc) {
      mynum = myn; mych = myc;
   }
   void mygetMyValues() {
      cout<<"The Numbers is: "<<mynum<< endl;
      cout<<"The Char is: "<<mych<<endl;
   }
};
int main(){
   myAbstractionExample myobj;
   myobj.mysetMyValues(100, 'X');
   myobj.mygetMyValues();
   return 0;
}

Output:

The Numbers is: 100
The Char is: X

Code Analysis:

This example illustrates the use of Abstraction. This is done by making the data member private. Private data members are not accessible from outside of the class. These data members are accessible using public functions.  

Data member mynum and mych are private and are accessible only by public member functions of the class. 

Conclusion

Objects in Object-Oriented Programming are used for computation and storing states because they are used in the simulation. Object-Oriented Programming is also used in system programming as it is used in processes, directories, and files.

Object-Oriented programming is extensively used in Artificial Intelligence.

Following are the terminology used in Object-Oriented Programming:

  • Classes: A class is used to describe the overall behaviour and structure of an entity that is to be modelled. 
  • Class Inheritance: This concept is used for sub-classification.
  • Class Variables: A class variable is used by all the objects of the class.
  • Abstraction: The class consists of data members and member functions. The implementation of data member and member functions are hidden from the user giving rise to abstraction.
  • Delegation: A delegation is used to handle the message of another object.
  • Instance:  Instance is used to define the relationship between an Object and its class. 
  • Message: Message defines the way of carrying out an operation on an object.  
  • Method: This is the definition to be executed when the message is forwarded to the Object.
  • Object: This is the most elementary unit of Object-Oriented programming. The Object contains data members and member functions.
  • Polymorphism: Polymorphism is used to implement different methods of the same class using the same interface. 
  • Super Class: A superclass is the top-most class of inheritance lattice.
  • Sub Class: A class below superclass in inheritance lattice. 

In this article, the author has given an overview of Object-Oriented programming Features its terminology, values, and properties used in Object-Oriented Programming. This is an informative article giving you an answer to – What is Object-Oriented Programming.