CLARRAY example shown here is a complete program that shows one way to create your own array class:   // clarray.cpp // creates array class #include using namespace std; //////////////////////////////////////////////////////////////// class Array //models a normal C++ array {  private:         int* ptr; //pointer to Array contents         int size; //size of Array  public:         Array(int s) //one-argument constructor         {            size = s; //argument is size of Array            ptr = new int[s]; //make space for Array         }         ~Array() //destructor         {                      delete[] ptr;               }         int& operator [] (int j) //overloaded subscript operator         {                      return *(ptr+j);               } }; //////////////////////////////////////////////////////////////// int main() {    const int ASIZE = 10; //size of array    Array arr(ASIZE); //make an array    for(int j=0; j

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

It’s possible to make a class that acts like an array. The CLARRAY example shown here is a complete program that shows one way to create your own array class:

 

// clarray.cpp

// creates array class

#include <iostream>

using namespace std;

////////////////////////////////////////////////////////////////

class Array //models a normal C++ array

{

 private:

        int* ptr; //pointer to Array contents

        int size; //size of Array

 public:

        Array(int s) //one-argument constructor

        {

           size = s; //argument is size of Array

           ptr = new int[s]; //make space for Array

        }

        ~Array() //destructor

        {

                     delete[] ptr;

              }

        int& operator [] (int j) //overloaded subscript operator

        {

                     return *(ptr+j);

              }

};

////////////////////////////////////////////////////////////////

int main()

{

   const int ASIZE = 10; //size of array

   Array arr(ASIZE); //make an array

   for(int j=0; j<ASIZE; j++) //fill it with squares

   {

          arr[j] = j*j;

   }

   for(int j=0; j<ASIZE; j++) //display its contents

   {

          cout << arr[j] <<" ";

       cout << endl;

   }

return 0;

}

 

The output of this program is

0 1 4 9 16 25 36 49 64 81

Starting with CLARRAY, add an overloaded assignment operator and an overloaded copy constructor to the Array class. Then add statements such as

 

Array arr2 (arr1);

and

arr3 = arr1;

to the main() program to test whether these overloaded operators work. The copy constructor should create an entirely new Array object with its own memory for storing array elements. Both the copy constructor and the assignment operator should copy the contents of the old Array object to the new one. What happens if you assign an Array of one size to an Array of a different size?            

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY