2a Question 2a . Declare a class SNAP (\"static, mumber, array, pointer\") as fo
ID: 3918257 • Letter: 2
Question
2a Question 2a . Declare a class SNAP ("static, mumber, array, pointer") as follows. . The data in class SHAP is statie string st, number int n, array int ta, pointer int "p . A non-default constructor is written for you. 1. The pointer p is dynamically allocated to a single object. 2. The pointer a is dynamically allocated to an array of length n. 3. You are given n> 0. class SNAP public: SNAP(int x, int m) // given to you // you are given that m> o n # m; a new int [n]; p new int; for (int i 0; iExplanation / Answer
If you have any doubts, please give me comment...
#include<iostream>
#include<string>
using namespace std;
class SNAP{
public:
SNAP(int x, int m){
n = m;
a = new int[n];
p = new int;
*p = x;
for(int i=0; i<n; ++i)
a[i] = 0;
}
int get_n() const{
return n;
}
string get_st(){
return st;
}
void set_st(string s){
st = s;
}
SNAP(){
n = 0;
a = NULL;
p = NULL;
}
SNAP(const SNAP ©):SNAP(*(copy.p) ,copy.get_n()){
}
SNAP& operator=(const SNAP &c){
if(this == &c)
return *this;
this->n = c.n;
this->a = new int[c.n];
this->p = new int;
this->p = c.p;
for(int i=0; i<c.n;i++)
this->a[i] = c.a[i];
return *this;
}
~SNAP(){
delete[] a;
delete p;
}
private:
static string st;
int n;
int *a;
int *p;
};
string SNAP::st = "SNAP";
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.