Develop a class to represent an array of integers. The member variables include:
ID: 3682805 • Letter: D
Question
Develop a class to represent an array of integers.
The member variables include:
1- An integer to represent the size of array.
2- an integer pointer to represent the address of the first element of the array.
The user is allowed to create an array at run-time using this class.
this class include a default constructor where the user will be asked to input the size of the array
Also include the following methods:
1- A method that adds the contents of array. Give it a proper name and specify the number and type or arguments (if you need any).
2- A method to print the elements of the array Give it a proper name and specify the number and type or arguments (if you need any).
Explanation / Answer
Answer for Question:
1. This below class will create the Integers with size and dyamic array.
2. Created default constructor and Print the elelemnts of the array.
class Integers
{
int size;
int *array;
public:
Integers()
{
size = 3;
array[] = new int[size];
}
Integers(int size1)
{
size = size1;
}
void readArray()
{
for(int i=0; i<size; i++)
cin>>array[i];
}
void addArrayElements()
{
int sum =0;
for(int i=0; i<size;i++)
sum = sum + array[i];
}
void printArray()
{
for(int i=0;i<size;i++)
cout<<array[i]<<" ";
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.