Lecture 8 – 9
•
Arrays with in a class
•
Memory allocation for objects of class
•
Array of objects
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.
2
class array
{
int a[10];
public:
void setVal();
void sort();
Example
1. #include<iostream> 2. using namespace std; 3. class array
4. { int a[10]; 5. public:
6. void setVal()
7. { for (int i = 0; i < 10; i++) cin >> a[i]; } 8. void sort()
9. { for (int i = 0; i < 9; i++)
10. for (int j = 0; j < 10 - i - 1; j++) 11. { if (a[j] > a[j + 1])
Contd…
16. void display()
17. { for (int i = 0; i < 10; i++) cout << a[i] << '\t'; 18. }
19. };
20. int main() 21. { array Arr; 22. Arr.setVal(); 23. Arr.sort();
24. cout << "Sorted sequence is:\n"; 25. Arr.display();
26. }
4
Output:
10 9 8 7 6 5 4 3 2 1 Sorted sequence is:
Contd…
•
During class specification
–
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
Contd…
8
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.
Contd…
•
An array of objects is stored similar to
multi-dimensional array.
emp manager[3];
name
manager[0]
age
name
manager[1]
age
name
Example
1. #include<iostream> 2. using namespace std; 3. class emp
4. { char name[10];
5. int age;
6. public:
7. void getData(); 8. void putData(); 9. };
10. void emp :: getData()
11. { cout << "Enter Name: "; cin >> name; 12. cout << "Enter Age: "; cin >> age; } 13. void emp :: putData()
Contd…
15. int main()
16. { emp manager[3];
17. for (int i = 0; i < 3; i++)
18. { cout << "\nEnter details of manager " << i + 1 << endl; 19. manager[i].getData();
20. }
21. for (int i = 0; i < 3; i++)
22. { cout << "\nManager " << i + 1; 23. manager[i].putData();
24. }
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
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)
1. #include <iostream> 2. using namespace std; 3. class Convert
4. { public :
5. int i;
6. void increment(Convert obj) 7. { obj.i=obj.i*2;
8. cout << "Value of i in member function : " << obj.i;
9. }
10. };
11. int main ()
12. { Convert obj1;
13. obj1.i=3; obj1.increment(obj1);
14. cout << "\nValue of i in main : " << obj1.i << "\n";
Output:
Example (Pass-by-reference)
1. #include <iostream> 2. using namespace std; 3. class Convert
4. { public :
5. int i;
6. void increment(Convert &obj) 7. { obj.i=obj.i*2;
8. cout << "Value of i in member function : " << obj.i;
9. }
10. };
11. int main ()
12. { Convert obj1;
13. obj1.i=3; obj1.increment(obj1);
14. cout << "\nValue of i in main : " << obj1.i << "\n";
15. return 0; } 16
Output:
Example
1. #include<iostream> 2. using namespace std; 3. class Time
4. { int h, m, s; 5. public:
6. void getTime(int p, int q, int r) 7. { h = p; m = q; s = r; }
8. void putTime()
9. { cout << h << ":" << m << ":" << s << endl; } 10. void sumTime(Time t1, Time t2)
11. { s = t1.s + t2.s;
12. m = s / 60; s = s % 60; 13. m = m + t1.m + t2.m;
Contd…
18
17. int main()
18. { Time t1, t2, t3;
19. t1.getTime(1,30,15); 20. t2.getTime(4,30,55); 21. t3.sumTime(t1,t2);
22. cout << "T1: "; t1.putTime(); 23. cout << "T2: "; t2.putTime(); 24. cout << "T3: "; t3.putTime(); 25. return 0;
26. }
Output: