C++ allows us to pass object as arguments to member function of the class( may be of the same class, or different class/es) or even non-member functions of the class . The general syntax of declaring such method would look like:
1 |
return_type method_name(Class_name); |
While defining the method outside of the class, it would look like:
1 2 3 4 5 |
return_type Class_name :: method_name(Class_name object_name) { //statement/s //return statement } |
The concept can be depicted with a simple program with the help of the following example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#include<iostream> #include<cmath> using namespace std; //class definition class Point { private: int x, y; public: /** Default Constructor: A default constructor must be declared if there is some parametrized constructor. In this case the compiler does not provide the default constructor **/ Point(); /** parameterized constructor **/ Point(int,int); //setter void setData(); //getter void getData(); double calculateDistance(Point,Point); }; //main implementation int main() { //create two points class Point p1, p2(16,6); // p1 is at the origin, since, it invokes default constructor //p2 is at the given location, passed as arguments cout << "The distance is " <<p1.calculateDistance(p1, p2) <<endl; p1.getData();// see if p1 is at origin or not p1.setData();//shift the point p1 p2.setData();// shift the point p2 //now the distance should change as well cout << "The current distance between the two points is " <<p1.calculateDistance(p1, p2); return 0; }//main ends /***************** methods defined outside class **************/ //definition of the default constructor Point::Point() { //assuming all the points are created at the origin first x = 0; y = 0; }//method ends //parametrized constructor definition Point::Point(int x_cord, int y_cord) { x = x_cord; y = y_cord; }//method ends //setter method definition void Point :: setData() { /************************************************************* ** this method will simulate that the objects are ** ** created on the stack by looking at the memory addresses, ** ** which decreases with the total size of the memory ** ** reserved by the object (sum of sizes of all properties ** **************************************************************/ cout <<" Enter the value of X-coordinate for point at " <<this <<endl; /********************************************************** ** It outputs the base address of 'this' pointer which ** ** refers the current object which invokes the method. ** **********************************************************/ cin >> x; cout <<" Enter the value of Y-coordinate for point at " <<this<<endl; cin >> y; }//method ends //getter method defined void Point :: getData() { cout <<"The point is at ("<<x<<","<<y<<")"<<endl; }//method ends //definition of the method to calculate distance, mutator double Point::calculateDistance(Point initialPoint, Point finalPoint) { double distance;//to hold the calculated value //using the distance formula distance = sqrt(pow((finalPoint.x - initialPoint.x),2) +pow((finalPoint.y - initialPoint.y),2)); return distance; } |
I hope it will help someone clear their concepts regarding default constructor, parametrized constructor and passing object as arguments demonstrating, WHYs? and HOWs?
For Advanced C++ Developers or Serious Programmers:
The same method calculateDistance() could have been designed by passing a single argument of Point type since one point is already invoking the method, so ‘this’ pointer will hold its properties.
1 2 3 4 5 6 7 |
.... //declaration double calculateDistance(Point); // single argument ... |
Then, definition outside the class would be:
1 2 3 4 5 6 7 8 9 |
double Point::calculateDistance(Point finalPoint) { double distance;//to hold the calculated value //using the distance formula distance = sqrt(pow((finalPoint.x - x),2) +pow((finalPoint.y - y),2)); //x and y can be referred as this.x and this.y instead, where 'this' is a pointer, //here used to reference implicitly return distance; } |
Leave a Reply
You must be logged in to post a comment.