Write the definition of a class called NumberSense that is defined as follows: M
ID: 3559896 • Letter: W
Question
Write the definition of a class called NumberSense that is defined as follows: Member variables: - Two simple int variables called x and y - Two pointer int variables called a and b Member functions: - You should have get() and set() functions for all four of your variables. For the get() variables, these should be returning actual values, not addresses. For the set() methods, remember that you cannot access a pointer variable without using the derefencing operator. - Have a default constructor that initializes x and y to 0 and dynamically allocates a and b using the new operator. - Include a parameter-based constructor that initializes x and y to parameters and dynamically allocates a and b, then initializes the references. - Have a destructor that deallocates a and b. You will break this program into a header/implementation/main file as we have done with classes in the past. You should have NumberSense.h, NumberSense.cpp, and a NumberSenseMain.cpp files. For testing purposes, up through this point, just create a simple NumberSense variable in main() that is initialized by using data that you make up. Call the get() and set() functions and make sure everything is ok. Once that is working, in main(), create a NumberSense variable that receive its input from a file. To do this in way that makes sense, and that practice pointers J, first use a dynamically allocated array of 4 elements (call it numbersInARow) that represents the data from the row in the file. Then create the NumberSense variable that takes each element of the numbersInARow array as a parameter. Call get() and set() methods and just do some simple output to so I can see that your program is working.
Explanation / Answer
/*NumberSense.h*/
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
class NumberSense {
private :
int x;
int y;
int *a;
int *b;
public:
NumberSense();
NumberSense(int i,int j,int *k,int *l);
void setX(int i);
void setY(int j);
void setA(int *k);
void setB(int *l);
int getX();
int getY();
int getA();
int getB();
~NumberSense();
};
******************************************************************************************************
/*NumberSense.cpp*/
#include "NumberSense.h"
using namespace std;
NumberSense :: NumberSense (){
x = 0;
y = 0;
a = new int;
a = NULL;
b = new int;
b = NULL;
}
NumberSense :: NumberSense (int i,int j,int *k,int *l){
x = i;
y = j;
a = new int;
a = k;
b = new int;
b = l;
}
NumberSense :: ~NumberSense(){
delete a;
a = NULL;
delete b;
b = NULL;
}
void NumberSense :: setX(int i){
x = i;
}
void NumberSense :: setY(int j){
y = j;
}
void NumberSense :: setA(int *k){
a = new int;
a = k;
}
void NumberSense :: setB(int *l){
b = new int;
b = l;
}
int NumberSense :: getX(){
return x;
}
int NumberSense :: getY(){
return y;
}
int NumberSense :: getA(){
return *a;
}
int NumberSense :: getB(){
return *b;
}
*********************************************************************************************
/*NumberSenseMain.cpp*/
#include "NumberSense.cpp"
#include <fstream>
#include <string>
using namespace std;
int main()
{
NumberSense ns;
ns.setX(1);
ns.setY(2);
int a1 = 3;
int b1 = 4;
ns.setA(&a1);
ns.setB(&b1);
int e = ns.getX();
int f = ns.getY();
int g = ns.getA();
int h = ns.getB();
cout << e << " " <<f <<" " <<g << " " << h;
int *numbersInARow = new int[4];
ifstream fin("NumberSense.txt");
string line;
if (fin.is_open())
{
while ( getline (fin,line) )
{
}
}
int len = line.length();
cout <<" ";
int count = 0;
for(int i = 0; i < len; i++){
if(line.at(i) == ' '){
continue;
}
else{
numbersInARow[count++] = line.at(i) - '0';
}
}
NumberSense ns1(numbersInARow[0],numbersInARow[1],&numbersInARow[2],&numbersInARow[3]);
cout << "x:"<<ns1.getX()<<endl;
cout << "y:"<<ns1.getY()<<endl;
cout << "a:"<<ns1.getA()<<endl;
cout << "b:"<<ns1.getB()<<endl;
return 0;
}
/*OutPut*/
1 2 3 4
x:1
y:2
a:3
b:4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.