Jump to content

Overloading Operators for Array Class

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
how is it possible to overload operators for a class array if there is only one integer not pointer to an integer ? can anyone demonstrate it with a little piece of code ? i know how to do it with pointer to integer but don't know how to with a single integer :(

#2
michaelvd12

michaelvd12

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
We cant without your source code help you;)

#3
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Here is a bit i made how to overload it ? i can't user pointer to array in the class so please help me out :(


class myInt{

	int a;


public:

	myInt(int p=0){

		a=p; }


	int get(){ return a; }


	myInt operator + (myInt aOb[]){

		myInt temp;

		for(int i=0;i<size;i++){

			temp=a[i]+aOb[i].a; }

		return temp; }

};


#4
michaelvd12

michaelvd12

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
this is c or c# ,

sorry , i cant programming in that language

#5
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
it's C++ =/

#6
michaelvd12

michaelvd12

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
ow ^^ :P

Then i see maybe the fold :

class myInt{
	int a;

public:
[B]	myInt(int p= "0"){
		a = "p"; }
[/B]
	int get(){ return a; }

	myInt operator + (myInt aOb[]){
		myInt temp;
		for(int i=0;i<size;i++){
			temp=a[i]+aOb[i].a; }
		return temp; }
};

it can be stupid because i am a beginner ;)

if it doesnt help, i cant help you anymore

#7
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
hehe i think you don't know C++ so its ok :)

#8
michaelvd12

michaelvd12

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
i know c++ , but work style is different than that from me ;)

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'm really not clear on what you have in mind. It looks like you want the following code to do something:
myInt daint;
int a[10];
for (int i=0;i<10;i++)
  a[i]=i;
daint = daint + a;
Is that correct?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
that's why I said in my first post that I want a sample code in which we just take a single integer and then we have to make an array of objects in main and perform operations on it , i.e the operators which will be overloaded for it so can you please post some sample ? I can't find any with a simple int :(

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You would do better to use a vector of ints instead of an array of ints. With an array, you have no way of knowing how big it is inside method operator+. A vector has a size attribute, however, that will allow you to know how many items it has.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#12
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Haven't studied vectors in university so far :( , maybe i should do it with an pointer to an int then