Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-22-2004, 08:44 PM   #1 (permalink)
MadMAxJr
Registered User
 
Join Date: Feb 2004
Location: Central Oklahoma
Posts: 4
MadMAxJr is on a distinguished road
Send a message via ICQ to MadMAxJr
Reviewing Overloading....

I'm trying to review on how to do this for an upcomming C++ exam. Care to help?

Code:
#include <iostream>
#include <iomanip>

using namespace std;

class IntArray {
	friend ostream operator<<(ostream &, const IntArray &);
	friend istream operator>>(istream &, IntArray &);
private:
	int size;
	int *ptr;
public:
	IntArray(int = 5);
	IntArray operator=( const IntArray&);
	IntArray operator++();

};
IntArray::IntArray(int s) {
	size = s;
	ptr = new int[size];
}
That is what is given. There are three questions, but only one of them I really need help with.

Overload = operator so that the internal array contents could be copied into the array contents of another IntArray object. If internal array sizes are different, the internal array is reallocated to have the same size. Write the function definition outside the class declaration. Assume this is the input:

Code:
IntArray a(15), b(12), c(20);
... // Suppose a is assigned values
c = b = a; // note that array sizes might be different.
That is the one I am having the most trouble with. It reutrns an IntArray type, and I keep running into numerous errors because of the const declaration. So when examining it, (*this) is the left operator, and we'll say we're passing it IntArray right, so we can compare (*this).size vs right.size. I tried this,
Code:
ntArray IntArray::operator=(const IntArray &right){
	int holder1 = right.size;
	if (&right.size > &(*this).size ) {
		&(*this).size = new int[ &right.size ];
	}
	for (int i = 0; i < size; i++ ){
		ptr[i] = &right.ptr;
	}
	return &this;
Any help, tips, or samples would be greatly appreciated.


The following questions aren't as hard, but I figured I'd list them anyway.
After that, we want to overload ++ such that all the elements in the internal array are incremented by 1. Assume this input
Code:
IntArray n(10), m(10);
m = ++n;
And the last one:
Overload << such that all the contents of the internal array can be displayed on the screen.
MadMAxJr is offline   Reply With Quote
Old 02-28-2004, 02:58 PM   #2 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
Well, there are a few things wrong with your code. First off, your this qualifier is actually a IntArray*, not IntArray. So you are actually returning (according to your code) an address of an address. You actually want to return a *this, not &this , so it actually reurns the object.

Second, you actually want to return a reference to an object, not a copy, so your return value should be IntArray & operator...

Third, you must also check in the beginning of the function whether or not you are actually assigning to yourself, and if so, don't actually copy, just return yourself, like so..

Code:
IntArray & operator=(const IntArray& rhs) {
   if (this != &rhs) {
     // Check sizes, and reallocate
     // Do copy
   }
   return *this
}
Fourth, since your types of this and such were wrong to begin with, your size check must be changed as well, but I'll leave that up to you.

Fifth, make sure you deallocate your array before you reallocate it to another size, as this will cause a memory leak.

Sixth, why are you doing a new int[size] on the size variable of your class? I think that should be ptr = new int[rhs.size].

You shouldn't run into errors because you're only changing this, not rhs.

Just one more thing, you don't have to do &(*this).size, you can just do size == rhs.size.

Sorry if I am being harsh, I did not mean to be. For more help, just ask! I'd be glad to help more. If I haven't answered your question, then again, just ask! I might pick up on the actual question next time!

Ted Morse
__________________
while(1) fork();
ender is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
operate overloading member function in C# sureshkumar_kc MS Technologies ( ASP, VB, C#, .NET ) 2 10-15-2004 03:36 AM
Operator= overloading SuperFerrell Standard C, C++ 3 02-04-2003 03:43 PM
c++ overloading sde Standard C, C++ 0 06-20-2002 08:38 PM


All times are GMT -8. The time now is 11:58 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting