Click here to Skip to main content
14,423,036 members

Managed C++/CLI

 
AnswerRe: NEED HELP ASAP!!!!!!! I ALREADY DID THE PROGRAM IN MAIN I JUST NEED HELP TO SEPERATE IN .CPP, .H AND MAIN Pin
Richard MacCutchan17-Apr-14 23:15
communityengineerRichard MacCutchan17-Apr-14 23:15 
GeneralRe: NEED HELP ASAP!!!!!!! I ALREADY DID THE PROGRAM IN MAIN I JUST NEED HELP TO SEPERATE IN .CPP, .H AND MAIN Pin
Akandel18-Apr-14 6:55
MemberAkandel18-Apr-14 6:55 
GeneralRe: NEED HELP ASAP!!!!!!! I ALREADY DID THE PROGRAM IN MAIN I JUST NEED HELP TO SEPERATE IN .CPP, .H AND MAIN Pin
Richard MacCutchan18-Apr-14 7:14
communityengineerRichard MacCutchan18-Apr-14 7:14 
GeneralRe: NEED HELP ASAP!!!!!!! I ALREADY DID THE PROGRAM IN MAIN I JUST NEED HELP TO SEPERATE IN .CPP, .H AND MAIN Pin
Akandel18-Apr-14 7:59
MemberAkandel18-Apr-14 7:59 
GeneralRe: NEED HELP ASAP!!!!!!! I ALREADY DID THE PROGRAM IN MAIN I JUST NEED HELP TO SEPERATE IN .CPP, .H AND MAIN Pin
Richard MacCutchan18-Apr-14 9:19
communityengineerRichard MacCutchan18-Apr-14 9:19 
GeneralRe: NEED HELP ASAP!!!!!!! I ALREADY DID THE PROGRAM IN MAIN I JUST NEED HELP TO SEPERATE IN .CPP, .H AND MAIN Pin
Akandel18-Apr-14 9:43
MemberAkandel18-Apr-14 9:43 
GeneralRe: NEED HELP ASAP!!!!!!! I ALREADY DID THE PROGRAM IN MAIN I JUST NEED HELP TO SEPERATE IN .CPP, .H AND MAIN Pin
Richard MacCutchan18-Apr-14 9:51
communityengineerRichard MacCutchan18-Apr-14 9:51 
QuestionNEED HELP ASAP!!!!!!! I ALREADY DID THE PROGRAM IN MAIN I JUST NEED HELP TO SEPERATE IN .CPP, .H AND MAIN Pin
Akandel17-Apr-14 18:28
MemberAkandel17-Apr-14 18:28 
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 Array<std::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 once

template <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>
Array<T>::Array(int s)
{
    size=s;
    array=new T [size];
}
template <typename T>
Array<T>::~Array()
{
    delete [] array;
}
template <typename T>
const T & Array<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];
}


IN MAIN:::
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <exception>
#include "Array.h"


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;

}

QuestionBuilding with Warning level 4 Pin
John Schroedl17-Apr-14 8:31
professionalJohn Schroedl17-Apr-14 8:31 
AnswerRe: Building with Warning level 4 Pin
John Schroedl18-Apr-14 7:22
professionalJohn Schroedl18-Apr-14 7:22 
Questionaccess functions in a DLL created in C # for borland c + + builder Pin
Willian Mathias9-Apr-14 9:03
MemberWillian Mathias9-Apr-14 9:03 
AnswerRe: access functions in a DLL created in C # for borland c + + builder Pin
Richard Andrew x6417-Apr-14 9:59
professionalRichard Andrew x6417-Apr-14 9:59 
Questionretrieve words in a HTML page Pin
neterfari2-Apr-14 0:42
Memberneterfari2-Apr-14 0:42 
SuggestionRe: retrieve words in a HTML page Pin
Richard MacCutchan2-Apr-14 1:22
communityengineerRichard MacCutchan2-Apr-14 1:22 
GeneralRe: retrieve words in a HTML page Pin
neterfari2-Apr-14 3:45
Memberneterfari2-Apr-14 3:45 
GeneralRe: retrieve words in a HTML page Pin
Richard MacCutchan2-Apr-14 4:02
communityengineerRichard MacCutchan2-Apr-14 4:02 
GeneralRe: retrieve words in a HTML page Pin
neterfari2-Apr-14 4:26
Memberneterfari2-Apr-14 4:26 
QuestionMultimedia Timer Problem in MFC VS2010 Pin
lephanhung30-Mar-14 19:44
Memberlephanhung30-Mar-14 19:44 
AnswerRe: Multimedia Timer Problem in MFC VS2010 Pin
Richard MacCutchan30-Mar-14 22:52
communityengineerRichard MacCutchan30-Mar-14 22:52 
GeneralRe: Multimedia Timer Problem in MFC VS2010 Pin
lephanhung31-Mar-14 3:11
Memberlephanhung31-Mar-14 3:11 
GeneralRe: Multimedia Timer Problem in MFC VS2010 Pin
lephanhung31-Mar-14 20:24
Memberlephanhung31-Mar-14 20:24 
QuestionCompiler Bug in C++/CLI vs Native C++ Pin
John Schroedl28-Mar-14 2:51
professionalJohn Schroedl28-Mar-14 2:51 
QuestionRun exe resource from memory. Pin
Member 1055963526-Mar-14 2:23
MemberMember 1055963526-Mar-14 2:23 
AnswerRe: Run exe resource from memory. Pin
Richard Andrew x6426-Mar-14 6:41
professionalRichard Andrew x6426-Mar-14 6:41 
Newscursor problem Pin
neterfari26-Mar-14 2:10
Memberneterfari26-Mar-14 2:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.