pgrm_007.cpp (1175B)
1 /* Right a program to calculate sum and product of array elements. */ 2 3 #include <iostream> 4 using namespace std; 5 6 class ArrayOperations { 7 private: 8 int *arr; 9 int n; 10 11 public: 12 int i; 13 ArrayOperations(int size) { 14 n = size; 15 arr = new int[n]; 16 } 17 18 void getData() { 19 for (i = 0; i < n; i++) { 20 cout << "Enter element " << i + 1 << ": "; 21 cin >> arr[i]; 22 } 23 } 24 25 int add() { 26 int sum = 0; 27 for (i = 0; i < n; i++) { 28 sum += arr[i]; 29 } 30 return sum; 31 } 32 33 long product() { 34 long prod = 1; 35 for (i = 0; i < n; i++) { 36 prod *= arr[i]; 37 } 38 return prod; 39 } 40 41 void display() { 42 cout << "sum = " << add() << endl; 43 cout << "product = " << product() << endl; 44 } 45 46 ~ArrayOperations() { 47 delete[] arr; 48 } 49 }; 50 51 int main() { 52 int n; 53 cout << "Enter size: "; 54 cin >> n; 55 ArrayOperations ao(n); 56 ao.getData(); 57 ao.display(); 58 return 0; 59 }