Like variables, objects can also be passed using pass-by-value, pass-by-reference, and pass-by-address. In pass-by-value we pass the copy of an object to a function. In pass-by-reference we pass a reference to the existing object. In pass-by-address we pass the address of an existing object. Following program demonstrates all three methods of passing objects as function arguments:
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 | #include<iostream> using namespace std; class Student { private: string name; string regdno; int age; public: string branch; void get_details(); void show_details(); //Pass-by-value void set_branch(Student s) { branch = s.branch; } //Pass-by-reference void set_branch(Student & s, int x) { branch = s.branch; } //Pass-by-address void set_branch(Student * s) { branch = s -> branch; } }; int main() { Student s1, s2, s3, s4; s1.branch = “cse”; s2.set_branch(s1); s3.set_branch(s1, 10); s4.set_branch( & s1); return 0; } |
Returning Objects
We can not only pass objects as function arguments but can also return objects from functions. We can return an object in the following three ways:
- Returning by value which makes a duplicate copy of the local object.
- Returning by using a reference to the object. In this way the address of the object is passed implicitly to the calling function.
- Returning by using the ‘this pointer’ which explicitly sends the address of the object to the calling function.
Following program demonstrates the three ways of returning objects from a function:
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 | #include<iostream> using namespace std; class Comparer { public: int value; Comparer compare_val(Comparer); Comparer & compare_ref(Comparer); Comparer & compare_add(Comparer); }; //Return by value Comparer Comparer::compare_val(Comparer c) { Comparer x; x.value = 10; if (x.value < c.value) return x; else return c; } //Return by reference Comparer & Comparer::compare_ref(Comparer c) { Comparer x; x.value = 10; if (x.value < c.value) return x; else return c; } //Return by address Comparer & Comparer::compare_add(Comparer c) { Comparer x; x.value = 10; if (x.value < c.value) return x; else return *this; } int main() { Comparer obj; obj.value = 20; obj = obj.compare_val(obj); cout << "Least value is: " << obj.value << endl; obj.value = 5; obj = obj.compare_ref(obj); cout << "Least value is: " << obj.value << endl; obj.value = 2; obj = obj.compare_ref(obj); cout << "Least value is: " << obj.value << endl; return 0; } |
Output:
1 2 3 4 5 | Least value is: 10 Least value is: 5 Least value is: 2 |
this Pointer
For every object in C++, there is an implicit pointer called this pointer. It is a constant pointer which always points to the current object. Uses of this pointer are as follows:
- When data member names and function argument names are same, this pointer can be used to distinguish a data member by writing this->member-name.
- It can be used inside a member function to return the current object by address by writing return *this.
The this pointer cannot be used along with static functions as static functions are not part of any object. Following program demonstrates the use of this pointer:
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 | #include<iostream> using namespace std; class Circle { private: int r; public: void area(int); void peri(int); }; void Circle::area(int r) { this -> r = r; cout << "Area of circle is: " << (3.1415 * this -> r * this -> r) << endl; } void Circle::peri(int r) { this -> r = r; cout << "Perimeter of circle is: " << (2 * 3.1415 * this -> r) << endl; } int main() { Circle c; c.area(10); c.peri(10); return 0; } |
1 2 3 4 | Area of circle is: 314.15 Perimeter of circle is: 62.83 |
Modify the area() function in the above program with the following code:
1 2 3 4 5 6 7 | void Circle::area(int r) { r = r; cout << "Radius of circle is: " << this -> r << endl; cout << “Area of circle is: “ << (3.1415 * this -> r * this -> r) << endl; } |
Take your time to comment on this article.