C++ Programming

Array in Class

Learn C plus plus example for sum of array elements in class

11/16/2019
0 views
array-in-class.cppCPP
/* Sum of array elements with class */
#include<iostream>
using namespace std;

class arrayElementSum {

	/* Declearation of array */
	int array[5];
	public:
		int sum;
	
		arrayElementSum(void) {
			cout << "Enter data for array:" << endl;

			/* '(sizeof(array)/sizeof(*array))' use to get length of the array */
			for(int i=0; i < (sizeof(array)/sizeof(*array)); i++)
				cin >> array[i];
				
		}

		void calculation() {
			for(int i=0; i< (sizeof(array)/sizeof(*array)); i++)
				sum += array[i];
		}
};

int main() {
	arrayElementSum arrayEsum = arrayElementSum();
	arrayEsum.calculation();
	
	/* Access public member variable 'sum' */
	cout << "Sum of array elements: " << arrayEsum.sum << endl;
	return 0;
}


/* Output */
Enter data for array:
1
2
3
4
5

Sum of array elements: 15
cppsum of array elementsarray in class

Loading comments...

Related Examples

Deliver breaking news, insightful commentary, and exclusive reports.

Targeting readers who rely on our platform to stay ahead of the curve.

Contact Us: benzingaheadlines@gmail.com