You are on page 1of 18

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.
class array • It is declared as a private data
{ int a[10]; member of the class array.
public: • Can be used in class member
void setVal(); functions.
void sort();
void display();
};
2
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])
12. { a[j] = a[j] + a[j + 1];
13. a[j + 1] = a[j] - a[j + 1];
14. a[j] = a[j] - a[j + 1]; } }
15. } 3
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. }
Output:
10 9 8 7 6 5 4 3 2 1
Sorted sequence is:
1 2 3 4 5 6 7 8 9 4
10
Memory allocation for objects of class

5
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.
6
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.
7
Contd…

class emp • emp manager[3];


{ char name[10]; // Array of manager
int age; • emp worker[10];
public: // Array of worker
void getData(); Accessing member functions.
void putData(); • manager[i].getData();
}; • manager[i].putData();

8
Contd…
• An array of objects is stored similar to multi-
dimensional array.
emp manager[3];
name
manager[0]
age
name
manager[1]
age
name
manager[2]
age
9
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()
14. { cout << "\tName: " << name << "\tAge: " << age << endl; }
10
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. }
25. return 0;
26. }
11
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


12
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.
13
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.
14
Example (Pass-by-value)
1. #include <iostream> Output:
2. using namespace std;
3. class Convert Value of i in member function : 6
4. { public : Value of i in main : 3
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; } 15
Example (Pass-by-reference)
1. #include <iostream> Output:
2. using namespace std;
3. class Convert Value of i in member function : 6
4. { public : Value of i in main : 6
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
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;
14. h = m / 60; m = m % 60;
15. h = h + t1.h + t2.h; }
16. }; 17
Contd…
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();
Output:
25. return 0;
26. }
T1: 1:30:15
T2: 4:30:55
T3: 6:1:10

18

You might also like