4 Replies - 1754 Views - Last Post: 02 May 2009 - 04:14 AM Rate Topic: -----

#1 smakdown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 30-April 09

re-assign the array of a class

Posted 02 May 2009 - 02:26 AM

Hi guys


if we have class like this :

class P {

public : 

int example (); 

}


the definition for example() is :

int P::example () {
int array [] = {1,2,3,4}; 
return array; 
}




is this allowed if i re-assign array like this :

int M = array.example() .

because i want M take the values of this array
array[i].example
and be like this M[i] and all values of
array[i].example
inside
M[i]
;


plz guys help ...

Is This A Good Question/Topic? 0
  • +

Replies To: re-assign the array of a class

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: re-assign the array of a class

Posted 02 May 2009 - 02:47 AM

View Postsmakdown, on 2 May, 2009 - 01:26 AM, said:

the definition for example() is :

int P::example () {
int array [] = {1,2,3,4}; 
return array; 
}



That's not a definition.
It is an implementation.


View Postsmakdown, on 2 May, 2009 - 01:26 AM, said:

is this allowed if i re-assign array like this :

int M = array.example() .


How could an int variable hold an array?

You really aren't being very clear in your question.

If I am guessing what you are trying to do then you will need to
1 - Declare an array called M
int M[x];

2 - Loop through each element of your existing array and allocate that value to the corresponding element in the new array.
The easiest way to do that would be to write a class method to copy arrays and declare M as an instance of the P class.
But do you have some reason for not wanting M to be a instance of the P class?
Was This Post Helpful? 0
  • +
  • -

#3 smakdown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 30-April 09

Re: re-assign the array of a class

Posted 02 May 2009 - 03:17 AM

View Postjanotte, on 2 May, 2009 - 01:47 AM, said:

View Postsmakdown, on 2 May, 2009 - 01:26 AM, said:

the definition for example() is :

int P::example () {
int array [] = {1,2,3,4}; 
return array; 
}



That's not a definition.
It is an implementation.


View Postsmakdown, on 2 May, 2009 - 01:26 AM, said:

is this allowed if i re-assign array like this :

int M = array.example() .


How could an int variable hold an array?

You really aren't being very clear in your question.

If I am guessing what you are trying to do then you will need to
1 - Declare an array called M
int M[x];

2 - Loop through each element of your existing array and allocate that value to the corresponding element in the new array.
The easiest way to do that would be to write a class method to copy arrays and declare M as an instance of the P class.
But do you have some reason for not wanting M to be a instance of the P class?



sorry about i was not clear my question because i'm beginner in C++ so i don't how to express meaning right

yeah you right i only want int M[x] hold the data in array[x].example() .

for your question i have 4 types of arrays and actually the data is holing in 4 different classes (means each array holing data for each class ) and i want only to use M[x] every time and make it in one function because i want to do some functions like next :

let's say i have array[x].example() , array[x].example2() , array[x].example3() , so i will do if-condition so the user will chose , if he will choose "2" means the data in array[x].example2() , "3" array[x].example3() , so i want every time those data equal to M[x] to get the data , and i don't have to do four functions and those doing the same work ...
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: re-assign the array of a class

Posted 02 May 2009 - 03:29 AM

I'm 100% sure your English is better than my ability in your main language but I am having trouble understanding you.

If I understand you right your best answer is to write a class method that will copy the given class object array into another array.

Something that could be called like this
example.copy(M[]);

Would have a definition something like this
void copy( int destinationArray[] );

And an implementation something like this
void P::copy( int destinationArray[] ) {

    // nifty code goes here

}


Was This Post Helpful? 0
  • +
  • -

#5 smakdown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 30-April 09

Re: re-assign the array of a class

Posted 02 May 2009 - 04:14 AM

Friend this's my code , please take a look :


double Avarg_eng (Person year1[], int size , char check) {
	 
	int m;
	double sum = 0; 

	
	if ( check == '1' ) {
		for (int i = 0; i <= size-1; i++) 
		{
			m = year1[i].getEnglish(); 
			sum = sum + m; 
		}
	} 
	
	else if (check == '2' ) {
		for (int i = 0; i <= size-1; i++)
		{
			m = year1[i].getMath(); 
			sum = sum + m; 
		}
	}
	
	else if (check == '3' ) {
		for (int i = 0; i <= size-1; i++)
		{
			m = year1[i].getCpp(); 
			sum = sum + m; 
		}
	}
	
	else if (check == '4' ) {
		for (int i = 0; i <= size-1; i++)
		{
			m = year1[i].getSe(); 
			sum = sum + m; 
		}
	}
	  return sum / static_cast<double>(size);  
}



what i was asking you how ti minimize this :

 for (int i = 0; i <= size-1; i++)
		{
			m = year1[i].getSe(); 
			sum = sum + m; 
		}


and make all those loops in one function ( and other word make getEnglish , gerMath() , getSe() and getCpp () in one function ) because i'll use those those loops for other functions but the different will be the sum only , i'll use another object .

when i was asking the question i was thinking to make it like this

double Avarg_eng (Person year1[], int size , char check) {
	 
	int m , m2; 
	double sum = 0; 

	
	if ( check == '1' ) {	
			m[] = year1[].getEnglish(); 
	
	else if (check == '2' ) {
		m[] = year1[].getMath(); 
	
	else if (check == '3' ) {
		m[] = year1[].getCpp(); 
	
	else if (check == '4' ) {
		m[] = year1[].geSe();

for (int i = 0; i <= size-1; i++) 
		{
			m2 = m[i];
			sum = sum + m; 
		}
 
}





i really appreciate your help friend ,,

Thanks

This post has been edited by smakdown: 02 May 2009 - 04:21 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1