Messages

Messages are the communication mechanism between objects.

 

Messages enable one object to invoke an action (method) in another object.

 

Messages comprise of three components:

  1. The object being addressed
  2. The method to perform
  3. Any parameters required by the method

Carry on with the previous example, the messages can be seen on line 32:

 

objectA.set("Hello, ", "World!") ;

 

and similarly on line 34 (without parameters):

#include <iostream>
#include <string>
using namespace std;

class MyClass{
	private :
		string first, second ;

	public:
		void set(string, string) ;
		string getFirst() ;
		string getSecond() ;
};

void MyClass::set(string x, string y){
	first = x ;
	second = y ;
}

string MyClass::getFirst(){
	return first;
}

string MyClass::getSecond(){
	return second;
}

int main() {

	MyClass objectA ;

	objectA.set("Hello, ","World!") ;

	cout << objectA.getFirst() << objectA.getSecond() << endl ;

	return 0;
}

Compile & Run:

Hello, World!

Leave a Reply