Please I need help with this Lanuage C Beginner level Please
Please... I need help with this....
Lanuage: C++ (Beginner level)
Please leave comment (//) next to your code as detail as possible... and please attach a successful output as well.
**********************************************************************************************************************************************************************
WRITE THE CODE below in ONE FILE... MyArrayPtrArithTemp2.cpp Convert Program 1 to a template.
//header files
#include<iostream>
using namespace std;
//declares the myArrayClass
class myArrayClass
{
//public Access specifier
public:
//declares the data members
int arraySize;
//declares pointer array
int * ptrArray;
//default constructor implementation
myArrayClass()
{
//assigns array to zero
arraySize=0;
//assigns the null pointer
ptrArray=NULL;
}
//parameterized constructor Implementation
myArrayClass(int size)
{
//checks the size is greater than 0
if(size>0)
{
//assigns the array size
arraySize=size;
//creates a pointer to array size
ptrArray=new int[arraySize];
}
}
//defines the setSize for setting the size
void setSize(int size)
{
//checks the size and the pointer array
if(size>0 && ptrArray==NULL)
{
//assigns the size is array size
arraySize=size;
//creates a pointer to array size
ptrArray=new int[arraySize];
}
}
//defines the setAllValues function
void setAllValues()
{
//getting the values from user
cout<<\"Enter all values: \";
//assigns the pointer array to null
if(ptrArray!=NULL)
{
//declares the pointer
int *ptr=ptrArray;
//loop to get the values
for(int i=0;i<arraySize;i++)
{
//getting input as pointer
cin>>(*ptr);
//increments the pointer
ptr++;
}
}
}
//defines the function printAll()
void printAll()
{
//assigns the array is NULL
if(ptrArray!=NULL)
{
//assigns a pointer array
int *ptr=ptrArray;
//loop to print the elements
for(int i=0;i<arraySize;i++)
{
//printing the values
cout<<(*ptr)<<endl;
//increments the pointer
ptr++;
}
}
//else statement
else
{
//printing the messages
cout<<\"No values to print.\"<<endl;
}
}
};
//main program for the class
int main()
{
//defines object for the class
myArrayClass Array1;
//calls the function with the created object
Array1.setSize(6);
//calls the function with the created object
Array1.setAllValues();
//calls the function with the created object
Array1.printAll();
//pauses the console
system(\"pause\");
//return statement
return 0;
}
**********************************************************************************************************************************************************************
Then, Split the code from above into two files: MyArrayPtrArithMain.cpp
// contains int main() function and its code MyArrayPtrArithTemp.cpp
// contains class template code and run.
Solution
Main:
#include<iostream> //header files
#include \"MyArrayPtrArithTemp.h\"
using namespace std;
int main(){ //main program for the class
myArrayClass Array1; //defines object for the class
Array1.setSize(6); //calls the function with the created object
Array1.setAllValues(); //calls the function with the created object
Array1.printAll(); //calls the function with the created object
system(\"pause\"); //pauses the console
return 0; //return statement
}
Temp:
#include<iostream> //header files
#include \"MyArrayPtrArithTemp.h\"
using namespace std;
class myArrayClass{ //declares the myArrayClass
public: //public Access specifier
int arraySize; //declares the data members
int * ptrArray; //declares pointer array
myArrayClass(){ //default constructor implementation
arraySize=0;//assigns array to zero
ptrArray=NULL; //assigns the null pointer
}
myArrayClass(int size){ //parameterized constructor Implementation
if(size>0){ //checks the size is greater than 0
arraySize=size; //assigns the array size
ptrArray=new int[arraySize]; //creates a pointer to array size
}
}
void setSize(int size){ //defines the setSize for setting the size
if(size>0 && ptrArray==NULL) { //checks the size and the pointer array
arraySize=size; //assigns the size is array size
ptrArray=new int[arraySize]; //creates a pointer to array size
}
}
void setAllValues() { //defines the setAllValues function
cout<<\"Enter all values: \"; //getting the values from user
if(ptrArray!=NULL) { //assigns the pointer array to null
int *ptr=ptrArray; //declares the pointer
for(int i=0;i<arraySize;i++) { //loop to get the values
cin>>(*ptr); //getting input as pointer
ptr++; //increments the pointer
}
}
}
void printAll(){ //defines the function printAll()
if(ptrArray!=NULL) { //assigns the array is NULL
int *ptr=ptrArray; //assigns a pointer array
for(int i=0;i<arraySize;i++) { //loop to print the elements
cout<<(*ptr)<<endl; //printing the values
ptr++; //increments the pointer
}
}
else{//else statement
cout<<\"No values to print.\"<<endl; //printing the messages
}
}
};
headerfile:
#ifndef _A_H // must be unique name in the project
#define _A_H
void setSize(int size); // prototype declaration of the function in a.cpp
void setAllValues();
void printAll();
#endif




