Message passing in C++

Message Passing the Topic itself gives a meaning “Passing something”. So in this topic, we are gonna learn What is Message Passing and how it works in C++.

From the basics of Object-Oriented Programming Concepts, we will see how an object is Pass through its class and call by the user with some good examples related to our real-life problems.

What is a Message?

A request for an object to perform one of its operations is called a message.
Operation means method/function. A message cannot go automatically it creates an interface, which means it creates an interface for an object. The interface provides the abstraction over the message means hide the implementation. So we get to know,
An interface is a set of operations that a given object can perform.

All communication between objects is done via message is called message passing, as people exchange information similarly the sending and receiving of information by the object is said to be message passing, to make possible message passing the following things have to be followed to be done :

  • We have to create classes that define objects and its behaviour.
  • Then Creating the objects from class definitions
  • Calling and connecting the communication among objects

Real word Examples :

Sending Message to Object –

Example: we want to move a car fast we will accelerate it :

move a car faster (press accelerator())
Press accelerator ----> send some message to car object ----> call its function(accelerate) ----> perfrom its task of moving
stop --->> Brakes (stopping the car)   ---->stopTheCar()

Another in our real life we want to withdraw money from ATM so

withdraw money ATM(press or touch or click the withdraw option ---> send some message to object ATM ----> call its method or function withDrawMoney() ---> task()

There could be another function which you can use in ATM-like we can

changePIN(), changeMobile() , generateStatement() , transfer() etc.

So in every case, we are sending messages or passing some message of sending some message and all these are implemented using a member function call.

Message  -------->   member function calls

So we can say that there are some behavior or capabilities that are achieved by an object and we implement using member function.

Behaviours (capabilities)   ----->   member functions (methods)

we have some attributes or some properties of object and we are going to specify these using data members or variables.

Attributes (Properties)     ----->   data members(variables)

Basic C++ Code for Message Passing

Below is the C++ code implementation for the message passing task:

class A
{
    public void Methodname(Object obj)
    {
        // Method does something which you assigned to do
    }
}

class B
{
    Object obj1 = new Object();

    A a = new A();
    a.Method1(obj1);
}

In the code passing parameter (obj1) to Method1 is said to be Message Passing.

Leave a Reply