-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticep29.cpp
More file actions
42 lines (35 loc) · 782 Bytes
/
practicep29.cpp
File metadata and controls
42 lines (35 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using namespace std;
//pointer within class
//using pointer to make array
class Array{
int *arr;
int size;
public:
void get_data(int n){
size=n;
arr=new int[size];//arr points to the starting space(address) or 0th index.
cout<<"Enter elements :";
for(int i=0;i<size;i++){
cin>>*(arr+i); //incrementing the pointer which is pointing to the index.
}
}
//ex:
//arr[0] -> *(arr+0) or *arr.
void add(){
int sum=0;
for(int i=0;i<size;i++){
sum+=*(arr+i);
}
cout<<"Sum of elements: "<<sum<<endl;
}
};
int main(){
Array a;
int n;
cout<<"Number of elements: ";
cin>>n;
a.get_data(n);
a.add();
return 0;
}