More C++ Help needed ( overloading operators )

lilmaniac2
5,000+ posts

Ask Me about SI Mag V4!
Recall that in C++ there is no check on an array index out of bounds. However, during program execution, an array index out of bounds can cause serious problems. Also, in C++ the array index starts at 0.

Design and implement the class myArray that solves the array index out of bound problem, and also allow the user to begin the array index starting at any integer, positive or negative. Every object of the type myArray is an array of the type int. During execution, when accessing an array component, if the index is out of bounds, the program must terminate with an appropriate error message. Consider the following statements:

myArray list (5); // Line 1

myArray myList (2,13); // Line 2

myArray yourList (-5,9); // Line 3

The statement in Line 1 declares list to be an array of 5 components, the component type is int, and the components are: list[0], list[1], …, list[4]; the statement in Line 2 declares mylist to be an array of 11 components, the component type is int, and the components are myList[2], myList[3], …., myList[12]; the statement in Line 3 declares yourList to be an array of 14 components, the component type is int, and the components are: yourList[-5], yourList[-4], …., yourList[0], …., yourList[8]. Write a program to test the class myArray.

Code:
/******************************************************************************/
/*********************************Chad Hicks***********************************/
/***************************     Final Exam Prob   ****************************/
/**************************Written Using Dev-C++ Ver4**************************/
/************************* Written on November 8th 2007************************/
/**************************For Math 240 Alan Smothers**************************/
/*****************************At Concord University****************************/
/******************************************************************************/




#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
//template<class Type>
class myArray
     {

      friend ostream& operator<<(ostream&, const myArray);
      friend istream& operator>>(istream&, const myArray &);

      public:
      int *stuff;
      int diff;
      int holder;
      myArray (int index)
              {
              stuff = new int[index];
              }
      myArray (int index1,int index2)
              {
              diff= index2-index1;
              stuff = new int[diff];
              }
      myArray operator[](int index)
              {
              return stuff[index];
              }
      myArray operator+(myArray);

      myArray operator= (myArray);

      };

 int main()

 {

   myArray list1(5);
   myArray list2(5);

   int i;

   cout<<"list1 : ";
   for (i = 0; i<5; i++)
       cout<<list1[i]<<" ";
   cout<<endl;

   cout<<"Enter 5 integers: ";
   for (i=0; i<5; i++)
       cin>>list1[i];
   cout<<endl;

   cout<<"After filling list1: ";

   for (i=0; i<5; i++)
       cout<<list1[i]<<" ";
   cout<<endl;

   list2 = list1;
   cout<<"list2: ";
   for (i = 0; i<5; i++)
       cout<<list2[i]<<" ";
   cout<<endl;

   cout<<"Enter 3 elements: ";

   for (i=0; i<3; i++)
       cin>>list1[i];
   cout<<endl;

   cout<<"First three elements of list1: ";
   for (i=0; i<3; i++)
       cin>>list1[i];
   cout<<endl;

   myArray list3(-2, 6);

   cout<<"list3: ";
   for (i=-2; i<6; i++)
       cout<<list3[i]<<" ";
   cout<<endl;

   list3[-2]=7;
   list3[4]=8;
   list3[0]=54;
   list3[2]=list3[4]+list3[-2];

   cout<<"list3: ";
   for (i=-2; i<6; i++)
       cout<<list3[i]<<" ";
   cout<<endl;

   return 0;

     }

ostream& operator<<(ostream& osObject, myArray& cObject)
   {
   return osObject;
   }

istream& operator>>(istream& isObject, myArray& cObject)
   {
   return isObject;
   }

myArray myArray::operator= (myArray otherList)
       {
       return otherList;
       }

myArray myArray::operator+ (myArray param)
       {
       myArray temp(5);
       temp.holder= holder+param.holder;
       return temp;
       }
 
ive been working on this for close to 10 hours trying to figure out the stupid syntax

now im getting no compiler errors but its throwing a linker error which is verry hard to find

 
just did an easy first assignment for JAVA this week. you guys might LOL at how easy it is.

Ch1Programming_Initials-1.png


 
nothing to see here

Code:
/******************************************************************************/
/*********************************Chad Hicks***********************************/
/******************************  AU Unit Prog *********************************/
/**************************Written Using Dev-C++ Ver4**************************/
/*************************Written on     March 6 2008 *************************/
/**************************For Math 271 Alan Smothers**************************/
/*****************************At Concord University****************************/
/******************************************************************************/




#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <fstream>
#include <string>

using namespace std;                                                            //standard name functions


int main()                                                                      //begin main
{
char c;
unsigned int memory[6000];
int x=0;
bool ioflag=0;
 ifstream is;
 is.open ("hw.txt");        // open file

 unsigned int loc=0;
 int count4=0;
 int shift=4;
 for (int ad=0; ad<4000; ad++)
     memory[ad]=0;

 for (int b=0; b<8; b++)
   {
   c = is.get();       // get character from file
   x=c;
   if (x>=48 && x<=57)
      {
      x=x-48;
      }
   if (x>=65 && x<=70)
      {
      x=x-55;
      }
   loc=loc+x;
   cout<<loc<<endl;
   loc=loc<<4;
   }
   cout<<endl<<endl<<loc;
 while (is.good())     // loop while extraction from file is possible
 {
   c = is.get();       // get character from file
   x=c;
   if (x>=48 && x<=57)
   {
   x=x-48;
   }
   if (x>=65 && x<=70)
   {
   x=x-55;
   }
   if (c=='\n')
   x=-1;
   if (x>=0)
   {
   if (x<8)
      {
      memory[loc]=memory[loc]<<1;
      shift--;
      }
   if (x<4)
      {
      memory[loc]=memory[loc]<<1;
      shift--;
      }
   if (x<2)
      {
      memory[loc]=memory[loc]<<1;
      shift--;
      }
   memory[loc]=memory[loc]<<shift;
   memory[loc]=x+memory[loc];
   count4++;
   shift=4;
   if (count4>=8)
      {
      loc++;
      count4=0;
      }
   }
 }

 is.close();           // close file

if (count4==4)
memory[loc]=memory[loc]<<4;



system("PAUSE");
return 0;                                                                //end main
}
 
Don't remember off the bat the syntax for operator overloading, but one issue I see right now is that your [] operator function doesn't check to make sure the index is in the proper range, thus defying the whole point of the project.

 
Activity
No one is currently typing a reply...

About this thread

lilmaniac2

5,000+ posts
Ask Me about SI Mag V4!
Thread starter
lilmaniac2
Joined
Location
Somewhere, Else
Start date
Participants
Who Replied
Replies
9
Views
301
Last reply date
Last reply from
Hundreth
IMG_20260516_193114554_HDR.jpg

sherbanater

    May 16, 2026
  • 0
  • 0
IMG_20260516_192955471_HDR.jpg

sherbanater

    May 16, 2026
  • 0
  • 0

New threads

Top