Click here to Skip to main content
15,886,095 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Count from 1 to 1000 without using loops Pin
Amrit Agr26-May-14 21:09
Amrit Agr26-May-14 21:09 
GeneralRe: Count from 1 to 1000 without using loops Pin
WuRunZhe19-Jun-14 5:44
WuRunZhe19-Jun-14 5:44 
GeneralRe: Count from 1 to 1000 without using loops Pin
Richard MacCutchan19-Jun-14 6:02
mveRichard MacCutchan19-Jun-14 6:02 
GeneralRe: Count from 1 to 1000 without using loops Pin
WuRunZhe19-Jun-14 15:01
WuRunZhe19-Jun-14 15:01 
QuestionBackground Image causes exception on startup but works in Forms Editor Pin
David W. Griffin20-May-14 8:36
David W. Griffin20-May-14 8:36 
Questioncompiling makefile Pin
MikcaCP19-May-14 11:11
MikcaCP19-May-14 11:11 
AnswerRe: compiling makefile Pin
Wes Aday19-May-14 13:28
professionalWes Aday19-May-14 13:28 
AnswerRe: compiling makefile Pin
Richard MacCutchan19-May-14 21:45
mveRichard MacCutchan19-May-14 21:45 
AnswerRe: compiling makefile Pin
Erik Westermann11-Jul-14 4:48
professionalErik Westermann11-Jul-14 4:48 
Questionc++ programming Pin
Member 107890691-May-14 16:42
Member 107890691-May-14 16:42 
AnswerRe: c++ programming Pin
Richard MacCutchan1-May-14 22:56
mveRichard MacCutchan1-May-14 22:56 
Questionc++ programming Pin
Member 107890691-May-14 16:34
Member 107890691-May-14 16:34 
QuestionRe: c++ programming Pin
Richard MacCutchan1-May-14 22:51
mveRichard MacCutchan1-May-14 22:51 
QuestionHow to convert the following C# line? Pin
Paramu197327-Apr-14 4:04
Paramu197327-Apr-14 4:04 
GeneralRe: How to convert the following C# line? Pin
Wes Aday27-Apr-14 5:49
professionalWes Aday27-Apr-14 5:49 
QuestionRe: How to convert the following C# line? Pin
Richard MacCutchan28-Apr-14 22:33
mveRichard MacCutchan28-Apr-14 22:33 
AnswerRe: How to convert the following C# line? Pin
Paramu197330-Apr-14 9:48
Paramu197330-Apr-14 9:48 
AnswerRe: How to convert the following C# line? Pin
WuRunZhe19-Jun-14 5:48
WuRunZhe19-Jun-14 5:48 
QuestionNeed help.... Pin
Akandel22-Apr-14 8:56
Akandel22-Apr-14 8:56 
AnswerRe: Need help.... Pin
Akandel22-Apr-14 8:57
Akandel22-Apr-14 8:57 
GeneralRe: Need help.... Pin
Richard MacCutchan28-Apr-14 21:57
mveRichard MacCutchan28-Apr-14 21:57 
Questiondescribe this code Pin
mostafaghanei17-Apr-14 19:21
mostafaghanei17-Apr-14 19:21 
AnswerRe: describe this code Pin
Richard MacCutchan17-Apr-14 22:19
mveRichard MacCutchan17-Apr-14 22:19 
AnswerRe: describe this code Pin
Munchies_Matt23-Apr-14 4:20
Munchies_Matt23-Apr-14 4:20 
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 17:37
Akandel17-Apr-14 17:37 
XML
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;
}



PLEASE REPLY SOON.

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.