Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. NEED HELP ASAP!!!!!!! I ALREADY DID THE PROGRAM IN MAIN I JUST NEED HELP TO SEPERATE IN .CPP, .H AND MAIN

NEED HELP ASAP!!!!!!! I ALREADY DID THE PROGRAM IN MAIN I JUST NEED HELP TO SEPERATE IN .CPP, .H AND MAIN

Scheduled Pinned Locked Moved Managed C++/CLI
c++databasedata-structuresperformancehelp
1 Posts 1 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Akandel
    wrote on last edited by
    #1

    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 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 "A

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups