NEED HELP ASAP!!!!!!! I ALREADY DID THE PROGRAM IN MAIN I JUST NEED HELP TO SEPERATE IN .CPP, .H AND MAIN
-
THE QUESTION IS AS FOLLOWS:
Create a template class called Array that implements an array and works with any type (int, double, etc.). The constructor should take a size for the array and use new to allocate memory (don't forget to delete in the destructor). Overload operator[] to access the elements of the array. If an attempt is made to access an element outside the array bounds (either above OR below), throw an exception. In the main program, first create an Array<int> object, add a few values, and demonstrate that operator[] throws an exception if an attempt is made to access an out-of-bounds elements. Catch the exception and print an appropriate message. Then do the same thing with an Arraystd::string.Hint: you will need both of these overloads:
T& operator[](int i)
const T& operator[](int i) const
I DID THE PROGRAM IN MAIN AS FOLLOWS AND IT DOES WELL IN THE MAIN BUT ITS GIVING ME HARD TIME DIVIDING IT INTO DIFFERENT FILES.
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <exception>
#include "Array.h"template <typename T>
class Array
{
int size; T*array;
public:
Array(int s)
{
size=s;
array=new T [size];
}
~Array()
{
delete [] array;
}
const T &operator[](int i) const
{
if(i < 0)
throw exception("Index out of bounds LOWER");
else if(i>=size)
throw exception("Index out of Bounds OVER");
else
return array[i];
}};
int _tmain(int argc, _TCHAR* argv[])
{
Array <int> num(12);
try
{
int i=num[-1];
}
catch (std :: exception & ex)
{
cout<<ex.what()<< endl;
}
Array <string> num1(12);
try
{
string j=num1[14];
}
catch (std :: exception & ex)
{
cout<<ex.what()<< endl;
}
cin.get();
cin.get();
return 0;
}THEN I DIVIDED THE PROGRAM INTO DIFFERENT FILES AS FOLLOWS:
IN ARRAY.H
#pragma oncetemplate <typename T>
class Array
{
public:
Array(int s);
~Array();
const T &operator[](int i)const;
private:
int size; T*array;
};IN ARRAY.CPP
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <exception>
#include "Array.h"template <typename T>
-
THE QUESTION IS AS FOLLOWS:
Create a template class called Array that implements an array and works with any type (int, double, etc.). The constructor should take a size for the array and use new to allocate memory (don't forget to delete in the destructor). Overload operator[] to access the elements of the array. If an attempt is made to access an element outside the array bounds (either above OR below), throw an exception. In the main program, first create an Array<int> object, add a few values, and demonstrate that operator[] throws an exception if an attempt is made to access an out-of-bounds elements. Catch the exception and print an appropriate message. Then do the same thing with an Arraystd::string.Hint: you will need both of these overloads:
T& operator[](int i)
const T& operator[](int i) const
I DID THE PROGRAM IN MAIN AS FOLLOWS AND IT DOES WELL IN THE MAIN BUT ITS GIVING ME HARD TIME DIVIDING IT INTO DIFFERENT FILES.
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <exception>
#include "Array.h"template <typename T>
class Array
{
int size; T*array;
public:
Array(int s)
{
size=s;
array=new T [size];
}
~Array()
{
delete [] array;
}
const T &operator[](int i) const
{
if(i < 0)
throw exception("Index out of bounds LOWER");
else if(i>=size)
throw exception("Index out of Bounds OVER");
else
return array[i];
}};
int _tmain(int argc, _TCHAR* argv[])
{
Array <int> num(12);
try
{
int i=num[-1];
}
catch (std :: exception & ex)
{
cout<<ex.what()<< endl;
}
Array <string> num1(12);
try
{
string j=num1[14];
}
catch (std :: exception & ex)
{
cout<<ex.what()<< endl;
}
cin.get();
cin.get();
return 0;
}THEN I DIVIDED THE PROGRAM INTO DIFFERENT FILES AS FOLLOWS:
IN ARRAY.H
#pragma oncetemplate <typename T>
class Array
{
public:
Array(int s);
~Array();
const T &operator[](int i)const;
private:
int size; T*array;
};IN ARRAY.CPP
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <exception>
#include "Array.h"template <typename T>
-
THE QUESTION IS AS FOLLOWS:
Create a template class called Array that implements an array and works with any type (int, double, etc.). The constructor should take a size for the array and use new to allocate memory (don't forget to delete in the destructor). Overload operator[] to access the elements of the array. If an attempt is made to access an element outside the array bounds (either above OR below), throw an exception. In the main program, first create an Array<int> object, add a few values, and demonstrate that operator[] throws an exception if an attempt is made to access an out-of-bounds elements. Catch the exception and print an appropriate message. Then do the same thing with an Arraystd::string.Hint: you will need both of these overloads:
T& operator[](int i)
const T& operator[](int i) const
I DID THE PROGRAM IN MAIN AS FOLLOWS AND IT DOES WELL IN THE MAIN BUT ITS GIVING ME HARD TIME DIVIDING IT INTO DIFFERENT FILES.
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <exception>
#include "Array.h"template <typename T>
class Array
{
int size; T*array;
public:
Array(int s)
{
size=s;
array=new T [size];
}
~Array()
{
delete [] array;
}
const T &operator[](int i) const
{
if(i < 0)
throw exception("Index out of bounds LOWER");
else if(i>=size)
throw exception("Index out of Bounds OVER");
else
return array[i];
}};
int _tmain(int argc, _TCHAR* argv[])
{
Array <int> num(12);
try
{
int i=num[-1];
}
catch (std :: exception & ex)
{
cout<<ex.what()<< endl;
}
Array <string> num1(12);
try
{
string j=num1[14];
}
catch (std :: exception & ex)
{
cout<<ex.what()<< endl;
}
cin.get();
cin.get();
return 0;
}THEN I DIVIDED THE PROGRAM INTO DIFFERENT FILES AS FOLLOWS:
IN ARRAY.H
#pragma oncetemplate <typename T>
class Array
{
public:
Array(int s);
~Array();
const T &operator[](int i)const;
private:
int size; T*array;
};IN ARRAY.CPP
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <exception>
#include "Array.h"template <typename T>
-
You do not need ARRAY.CPP, all that code should be in the header file, following the class definition.
@Richardd MacCutchann But the checklist is as follows: hecklist 1. Did NOT create a folder for the solution 2. Project/solution named correctly 3. Correct comments at top 4. Consistent indentation 5. Good variable names 6. Overall neat organization 7. Comments in code explain what's being done 8. Correct division of code into .h and .cpp files 9. Use of #pragma once in .h files (or, #ifndef) 10. #include "stdafx.h" in cpp files (or, suppress pch) 11. Test for below as well as above out-of-range
-
@Richardd MacCutchann But the checklist is as follows: hecklist 1. Did NOT create a folder for the solution 2. Project/solution named correctly 3. Correct comments at top 4. Consistent indentation 5. Good variable names 6. Overall neat organization 7. Comments in code explain what's being done 8. Correct division of code into .h and .cpp files 9. Use of #pragma once in .h files (or, #ifndef) 10. #include "stdafx.h" in cpp files (or, suppress pch) 11. Test for below as well as above out-of-range
-
@Richard-MacCutchann I actually did and have posted the main program and my attempt to divide it into .cpp and .h files i am almost there and i just need help to figure out the minor mistakes i have made . if you look at my main post you will see all the program please have a look and let me know if you can help me or not thanks.
-
@Richard-MacCutchann I actually did and have posted the main program and my attempt to divide it into .cpp and .h files i am almost there and i just need help to figure out the minor mistakes i have made . if you look at my main post you will see all the program please have a look and let me know if you can help me or not thanks.
-
I already gave you a suggestion. If you want more help then you need to provide a more detailed explanation of your problem.
if you look at the main question i have my main program that runs and gives me desired output but when i try to make a seperate .cpp and .h files for the program (that i also have attached in the main question) gives me some error so i want to request you to have a look at that and let me know what i should do to make the program work with a seperate .cpp and .h file for the main program that i have attached in the question
-
if you look at the main question i have my main program that runs and gives me desired output but when i try to make a seperate .cpp and .h files for the program (that i also have attached in the main question) gives me some error so i want to request you to have a look at that and let me know what i should do to make the program work with a seperate .cpp and .h file for the main program that i have attached in the question