Menu
Forum
General Car Audio
Subwoofers
Speakers
Amplifiers
Head Units
Car Audio Build Logs
Wiring, Electrical and Installation
Enclosure Design & Construction
Car Audio Classifieds
Home Audio
Off-topic Discussion
The Lounge
What's new
Search forums
Gallery
New media
New comments
Search media
Members
Registered members
Current visitors
Classifieds Member Feedback
SHOP
Shop Head Units
Shop Amplifiers
Shop Speakers
Shop Subwoofers
Shop eBay Car Audio
Log in / Register
Forum
Search
Search titles and first posts only
Search titles only
Search titles and first posts only
Search titles only
Log in / Join
What’s new
Search
Search titles and first posts only
Search titles only
Search titles and first posts only
Search titles only
General Car Audio
Subwoofers
Speakers
Amplifiers
Head Units
Car Audio Build Logs
Wiring, Electrical and Installation
Enclosure Design & Construction
Car Audio Classifieds
Home Audio
Off-topic Discussion
The Lounge
What's new
Search forums
Menu
Reply to thread
Forum
Off-topic Discussion
The Lounge
More C++ Help needed ( overloading operators )
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="lilmaniac2" data-source="post: 3619397" data-attributes="member: 565395"><p>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.</p><p></p><p>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:</p><p></p><p>myArray list (5); // Line 1</p><p></p><p>myArray myList (2,13); // Line 2</p><p></p><p>myArray yourList (-5,9); // Line 3</p><p></p><p>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.</p><p></p><p>[CODE]/******************************************************************************/</p><p>/*********************************Chad Hicks***********************************/</p><p>/*************************** Final Exam Prob ****************************/</p><p>/**************************Written Using Dev-C++ Ver4**************************/</p><p>/************************* Written on November 8th 2007************************/</p><p>/**************************For Math 240 Alan Smothers**************************/</p><p>/*****************************At Concord University****************************/</p><p>/******************************************************************************/</p><p></p><p></p><p></p><p></p><p>#include &lt;iostream&gt;</p><p>#include &lt;stdlib.h&gt;</p><p>#include &lt;stdio.h&gt;</p><p>#include &lt;time.h&gt;</p><p>#include &lt;math.h&gt;</p><p>#include &lt;fstream&gt;</p><p>#include &lt;string&gt;</p><p>#include &lt;iomanip&gt;</p><p></p><p>using namespace std;</p><p>//template&lt;class Type&gt;</p><p>class myArray</p><p> {</p><p></p><p> friend ostream&amp; operator&lt;&lt;(ostream&amp;, const myArray);</p><p> friend istream&amp; operator&gt;&gt;(istream&amp;, const myArray &amp;);</p><p></p><p> public:</p><p> int *stuff;</p><p> int diff;</p><p> int holder;</p><p> myArray (int index)</p><p> {</p><p> stuff = new int[index];</p><p> }</p><p> myArray (int index1,int index2)</p><p> {</p><p> diff= index2-index1;</p><p> stuff = new int[diff];</p><p> }</p><p> myArray operator[](int index)</p><p> {</p><p> return stuff[index];</p><p> }</p><p> myArray operator+(myArray);</p><p></p><p> myArray operator= (myArray);</p><p></p><p> };</p><p></p><p> int main()</p><p></p><p> {</p><p></p><p> myArray list1(5);</p><p> myArray list2(5);</p><p></p><p> int i;</p><p></p><p> cout&lt;&lt;"list1 : ";</p><p> for (i = 0; i&lt;5; i++)</p><p> cout&lt;&lt;list1[i]&lt;&lt;" ";</p><p> cout&lt;&lt;endl;</p><p></p><p> cout&lt;&lt;"Enter 5 integers: ";</p><p> for (i=0; i&lt;5; i++)</p><p> cin&gt;&gt;list1[i];</p><p> cout&lt;&lt;endl;</p><p></p><p> cout&lt;&lt;"After filling list1: ";</p><p></p><p> for (i=0; i&lt;5; i++)</p><p> cout&lt;&lt;list1[i]&lt;&lt;" ";</p><p> cout&lt;&lt;endl;</p><p></p><p> list2 = list1;</p><p> cout&lt;&lt;"list2: ";</p><p> for (i = 0; i&lt;5; i++)</p><p> cout&lt;&lt;list2[i]&lt;&lt;" ";</p><p> cout&lt;&lt;endl;</p><p></p><p> cout&lt;&lt;"Enter 3 elements: ";</p><p></p><p> for (i=0; i&lt;3; i++)</p><p> cin&gt;&gt;list1[i];</p><p> cout&lt;&lt;endl;</p><p></p><p> cout&lt;&lt;"First three elements of list1: ";</p><p> for (i=0; i&lt;3; i++)</p><p> cin&gt;&gt;list1[i];</p><p> cout&lt;&lt;endl;</p><p></p><p> myArray list3(-2, 6);</p><p></p><p> cout&lt;&lt;"list3: ";</p><p> for (i=-2; i&lt;6; i++)</p><p> cout&lt;&lt;list3[i]&lt;&lt;" ";</p><p> cout&lt;&lt;endl;</p><p></p><p> list3[-2]=7;</p><p> list3[4]=8;</p><p> list3[0]=54;</p><p> list3[2]=list3[4]+list3[-2];</p><p></p><p> cout&lt;&lt;"list3: ";</p><p> for (i=-2; i&lt;6; i++)</p><p> cout&lt;&lt;list3[i]&lt;&lt;" ";</p><p> cout&lt;&lt;endl;</p><p></p><p> return 0;</p><p></p><p> }</p><p></p><p>ostream&amp; operator&lt;&lt;(ostream&amp; osObject, myArray&amp; cObject)</p><p> {</p><p> return osObject;</p><p> }</p><p></p><p>istream&amp; operator&gt;&gt;(istream&amp; isObject, myArray&amp; cObject)</p><p> {</p><p> return isObject;</p><p> }</p><p></p><p>myArray myArray::operator= (myArray otherList)</p><p> {</p><p> return otherList;</p><p> }</p><p></p><p>myArray myArray::operator+ (myArray param)</p><p> {</p><p> myArray temp(5);</p><p> temp.holder= holder+param.holder;</p><p> return temp;</p><p> }[/CODE]</p></blockquote><p></p>
[QUOTE="lilmaniac2, post: 3619397, member: 565395"] 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; }[/CODE] [/QUOTE]
Insert quotes…
Verification
Post reply
Forum
Off-topic Discussion
The Lounge
More C++ Help needed ( overloading operators )
Top
Menu
What's new
Forum list