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
9 Posts 2 Posters 6 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 "Array.h"

    template <typename T>

    A L 2 Replies Last reply
    0
    • A Akandel

      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 "Array.h"

      template <typename T>

      A Offline
      A Offline
      Akandel
      wrote on last edited by
      #2

      @Richard-MacCutchann

      1 Reply Last reply
      0
      • A Akandel

        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 "Array.h"

        template <typename T>

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You do not need ARRAY.CPP, all that code should be in the header file, following the class definition.

        A 1 Reply Last reply
        0
        • L Lost User

          You do not need ARRAY.CPP, all that code should be in the header file, following the class definition.

          A Offline
          A Offline
          Akandel
          wrote on last edited by
          #4

          @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

          L 1 Reply Last reply
          0
          • A Akandel

            @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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Then you need to follow the instructions and do your own work.

            A 1 Reply Last reply
            0
            • L Lost User

              Then you need to follow the instructions and do your own work.

              A Offline
              A Offline
              Akandel
              wrote on last edited by
              #6

              @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.

              L 1 Reply Last reply
              0
              • A Akandel

                @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.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                I already gave you a suggestion. If you want more help then you need to provide a more detailed explanation of your problem.

                A 1 Reply Last reply
                0
                • L Lost User

                  I already gave you a suggestion. If you want more help then you need to provide a more detailed explanation of your problem.

                  A Offline
                  A Offline
                  Akandel
                  wrote on last edited by
                  #8

                  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

                  L 1 Reply Last reply
                  0
                  • A Akandel

                    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

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    I have looked, and I have given you a suggestion; it is up to you to implement it. You then need to build and test until it runs successfully. You also need to provide specific details of the errors you receive if you want further help.

                    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