PREV NEXT
C++ Objects and Functions:
What are objects?
The class provides the blueprint for the objects so; objects are created from within the class. The objects of a class are declared exactly with the same sort of declaration that we declare the variables of basic types.
To understand the concept clearly let us have a look at the 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 |
#include <iostream> using namespace std; class Sum { public: int a; // data members of class Sum int b; }; int main() { Sum s1; // Declare s1 of type Sum Sum s2; // Declare s2 of type Sum int add = 0; // it will store the sum of two data members // s1 specification s1.a = 5; s1.b = 6; // s2 specification s2.a = 4; s2.b = 8; // addition of s1 add = s1.a + s1.b; cout << "Addition of s1 : " << add <<endl; // addition of s2 add = s2.a + s2.b; cout << "Addition of s2 : " << add <<endl; return 0; } |
What are functions?
- Functions are used for the structured programming which follows top-down approach. Function is a collection of statements or set of statements that perform the task together.
- In C++, every program consists of a single function that is a main( ) function. The functions take the inputs and perform the task on those inputs and provide output. It provides standard to a program and while creating a program we use functions which makes program easier to understand, edit and check the errors, etc.
Let us have a look at the 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 |
#include < iostream> using namespace std; int add (int x, int y); //declaring function int main() { int a = 5; int b = 20; int c = add (a, b); //calling function cout << c; return 0; } int add (int x, int y) //defining function { return (x + y); } |
How to pass the objects as function arguments in C++?
- The objects can be passed as the arguments to member functions as well as to non-member functions either by value or by reference.
- When the object is passed by value then a copy of the actual object is created inside the function and it is destroyed when the function is terminated. So, any changes made in the copy of the object inside the function are not reflected in the actual object.
- But in the pass by reference only the reference of that object is passed to the function so, any changes made to the object within the function are reflected in the actual object.
Let us have a look at the examples of Pass by value and Pass by reference:
Pass by value:
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 |
#include <iostream> using namespace std; class Sum { public : int i; void add(Sum s) { s.i=s.i+5; cout << "The value of i in member function : " << s.i; } }; int main () { Sum s1; s1.i=10; s1.add(s1); cout << "\nThe value of i in main : " << s1.i << "\n"; return 0; } Pass by reference: #include <iostream> using namespace std; class Sum { public : int i; void add(Sum &s) { s.i=s.i+2; cout << "Value of i in member function : " << s.i; } }; int main () { Sum s1; s1.i=3; s1.add(s1); cout << "\nValue of i in main : " << s1.i << "\n"; return 0; } |