Lecture 8 – 9 Arrays with in a class Memory allocation for objects of class Array of objects Object as function arguments
Arrays within a class Arrays can be used as class member variables. a is an integer array. It is declared as a private data member of the class array. Can be used in class member functions. class array { int a[10]; public: void setVal(); void sort(); void display(); };
Example #include<iostream> using namespace std; class array { int a[10]; public: void setVal() { for (int i = 0; i < 10; i++) cin >> a[i]; } void sort() { for (int i = 0; i < 9; i++) for (int j = 0; j < 10 - i - 1; j++) { if (a[j] > a[j + 1]) { a[j] = a[j] + a[j + 1]; a[j + 1] = a[j] - a[j + 1]; a[j] = a[j] - a[j + 1]; } } }
Contd… void display() { for (int i = 0; i < 10; i++) cout << a[i] << '\t'; } }; int main() { array Arr; Arr.setVal(); Arr.sort(); cout << "Sorted sequence is:\n"; Arr.display(); Output: 10 9 8 7 6 5 4 3 2 1 Sorted sequence is: 1 2 3 4 5 6 7 8 9 10
Memory allocation for objects of class
Contd… During class specification During object creation No memory space is allocated for the class data members. Memory is allocated to class member functions once they are defined as a part of class specification. During object creation No separate space is allocated for class member functions, they remain same for all objects. Memory is allocated for class data members separately for each object as they hold different data values for different objects.
Array of objects It is possible to have arrays of objects. Means array of variables of type class. The syntax for declaring and using an object array is exactly the same as it is for any other type of array. Use usual array-accessing methods to access individual elements, and then the dot member operator to access the member functions. An array of objects is stored inside the memory in the same way as a multi-dimensional array.
Contd… class emp { char name[10]; int age; public: void getData(); void putData(); }; emp manager[3]; // Array of manager emp worker[10]; // Array of worker Accessing member functions. manager[i].getData(); manager[i].putData();
Contd… An array of objects is stored similar to multi-dimensional array. emp manager[3]; name manager[0] age manager[1] manager[2]
Example #include<iostream> using namespace std; class emp { char name[10]; int age; public: void getData(); void putData(); }; void emp :: getData() { cout << "Enter Name: "; cin >> name; cout << "Enter Age: "; cin >> age; } void emp :: putData() { cout << "\tName: " << name << "\tAge: " << age << endl; }
Contd… int main() { emp manager[3]; for (int i = 0; i < 3; i++) { cout << "\nEnter details of manager " << i + 1 << endl; manager[i].getData(); } { cout << "\nManager " << i + 1; manager[i].putData(); return 0;
Contd… Enter details of manager 1 Enter Name: Arun Enter Age: 50 Enter details of manager 2 Enter Name: Amita Enter Age: 35 Enter details of manager 3 Enter Name: Yashika Enter Age: 45 Manager 1 Name: Arun Age: 50 Manager 2 Name: Amita Age: 35 Manager 3 Name: Yashika Age: 45
Objects as function arguments Object can be passed as a function argument, like any other data type to member functions as well as non-member functions. Pass-by-value Pass a copy of the entire object, which destroys when the function terminates. Changes made to the copy of an object within function are not reflected in the actual object. Pass-by-reference Pass only the address of the object, not the entire object. Changes made to an object within the function are also reflected in the actual object.
Contd… When a class object is passed to a member function of the same class. Its data members can be accessed inside the function using the object name and the dot operator. However, data members of the calling object can be accessed directly inside the function without using the object name and the dot operator. When a class object is passed to a non-member function. Its public members can only be accessed inside the function using the object name and the dot operator. No access to private members.
Example (Pass-by-value) #include <iostream> using namespace std; class Convert { public : int i; void increment(Convert obj) { obj.i=obj.i*2; cout << "Value of i in member function : " << obj.i; } }; int main () { Convert obj1; obj1.i=3; obj1.increment(obj1); cout << "\nValue of i in main : " << obj1.i << "\n"; return 0; } Output: Value of i in member function : 6 Value of i in main : 3
Example (Pass-by-reference) #include <iostream> using namespace std; class Convert { public : int i; void increment(Convert &obj) { obj.i=obj.i*2; cout << "Value of i in member function : " << obj.i; } }; int main () { Convert obj1; obj1.i=3; obj1.increment(obj1); cout << "\nValue of i in main : " << obj1.i << "\n"; return 0; } Output: Value of i in member function : 6 Value of i in main : 6
Example #include<iostream> using namespace std; class Time { int h, m, s; public: void getTime(int p, int q, int r) { h = p; m = q; s = r; } void putTime() { cout << h << ":" << m << ":" << s << endl; } void sumTime(Time t1, Time t2) { s = t1.s + t2.s; m = s / 60; s = s % 60; m = m + t1.m + t2.m; h = m / 60; m = m % 60; h = h + t1.h + t2.h; } };
Contd… int main() { Time t1, t2, t3; t1.getTime(1,30,15); t3.sumTime(t1,t2); cout << "T1: "; t1.putTime(); cout << "T2: "; t2.putTime(); cout << "T3: "; t3.putTime(); return 0; } Output: T1: 1:30:15 T2: 4:30:55 T3: 6:1:10