Hello please help me

class X
{
	protected:
		int x,y;
		color cl;
		X-type type;
		char a;
};
-----------------------------
X* arr[10][10];
int main()
{
...
}

------------------------------

i want define a constructor to this class that declare for x, y like:
arr[2][1]->x=2;
and
arr[2][1]->y = 1;
that declare any of arr[10][10] like above object
how can i do this?

Recommended Answers

All 11 Replies

Your question is cyclic :
1>A pointer to a particular class cannot be defined until the class is defined.
2>And you want to define the pointer to the class and manipulate it within the constructor of the class itself.

Which is interdependent.So ask your question clearly and state your exact requirement in a more understandable way.

i want to define the pointer to the class and manipulate it within the constructor of the class itself.

Ya it is something like an object pointing to itself. Even though you may try to code it for experimentation you wont get any benefits from it and i dont even see a specific use....That is the reason i wanted you to be more specific about you requirement (not about your stated question) as to how and where are you using it?And why?

please help me

please help me

He IS helping you. He's asking you questions in an attempt to clarify your goals. Depending on your answers to those questions, he'll offer different advice. Often, if you answer these questions, not only will the problem be clearer to him (and the rest of us), but it will clarify things to YOU too.

So please restate the problem in a little more detail. You mentioned that you want this done in a constructor. You have not posted the constructor (or presumably you have not written it yet). You have a very rough skeleton. You should probably fill it in a little more. And a few more sentences, as csurfer mentioned, would be very helpful in helping us help you.

you wont get any benefits from it and i dont even see a specific use

I think it is a great way to explain the simplicity of C++. You will have to fight off the newbies wanting to learn if you show them the clear and intuitive code that can be written:

int main(){X*p;std::cout.operator<<(*(X*)&((*new X(&p))()=10)).put(10);delete p;}

:D

I will leave the simple definition of X as an exercise for the reader.

I think it is a great way to explain the simplicity of C++. You will have to fight off the newbies wanting to learn if you show them the clear and intuitive code that can be written:

int main(){X*p;std::cout.operator<<(*(X*)&((*new X(&p))()=10)).put(10);delete p;}

:D

I will leave the simple definition of X as an exercise for the reader.

I agree with your
way of explanation Tom Gunn But the OP's question is not manipulating the pointer to a class inside main function but to manipulate it inside the class itself and I don't think you have spoken anything in that regard.

class X
{
	public:
		virtual void function( ) = 0;
	protected:
		int x,y;
		color cl;
		X-type type;
		char a;
};
class X is abstract class.
---------------------------------
X* arr[10][10];

i want t initialize to x, y of any arr[10][10]
for example:
arr[0][0] --> x=0, y=0
&
arr[0][1] --> x=0, y=1
also arr[0][2] ..... arr[10][10]
how can i do this
in function main or own classcode

I don't think you have spoken anything in that regard.

I have not bothered to seriously answer this question because it is too ambiguous. Each time thr clarifies what he wants, I get more confused. Now X is an abstract class. I can suggest something like this:

#include <iostream>

using namespace std;

class X
{
	public:
		virtual void function( ) = 0;
	protected:
        X(int xx, int yy): x(xx), y(yy) {}
		int x,y;
};

class Y: public X
{
public:
    Y(int xx, int yy): X(xx, yy) {}
    void function() { cout << x << ',' << y << '\n'; }
};

int main()
{
    X *arr[10][10];

    for (int x = 0; x < 10; ++x)
    {
        for (int y = 0; y < 10; ++y)
        {
            arr[x][y] = new Y(x, y);
        }
    }

    for (int x = 0; x < 10; ++x)
    {
        for (int y = 0; y < 10; ++y)
        {
            arr[x][y]->function();
            delete arr[x][y];
            arr[x][y] = 0;
        }
    }
}

But it is just a shot in the dark.

My example code of my classes
class Mohreh
{
	
public:
	Mohreh( int X, int Y, color c, mohreh_type mt, char ch ) : x(X), y(Y), col( c ), type( mt ), a( ch ) { }
	
	virtual const Mohreh& operator=( const Mohreh& r )
	{
		
		col = r.col;
		type = r.type;
		a = r.a;
		return *this;
	}
	void set( int a, int b )
	{
		x=a;
		y=b;
	}


	virtual bool checkmove( int, int ) = 0;
	virtual void move( int, int ) = 0;
	color col;
	char a;
protected:
	int x, y;
	
	mohreh_type type;
	
	
};
-------------------
extern Mohreh* safheh[8][8];

class Asb : public Mohreh
{
public:
	Asb( int X, int Y, color c, mohreh_type t, char );
	const Asb &operator=( const Asb&  );
	
	bool checkmove( int, int );
	void move( int, int );
	
};
#endif
----------------------
#include "Mohreh.h"

extern Mohreh* safheh[8][8];

class Sarbaz : public Mohreh
{
public:
	Sarbaz( int X, int Y, color c, mohreh_type t, char );
	bool checkmove( int, int );
	void move( int, int );
	
private:
	int counter;
};
#endif
-------------------------
#include "Mohreh.h"

extern Mohreh* safheh[8][8];

class Galeh : public Mohreh
{
	public:
	Galeh( int X, int Y, color c, mohreh_type t, char );
	bool checkmove( int, int );
	void move( int, int );
	
};
#endif
------------------------
function main()

#include <iostream>
#include "Mohreh.h"
#include "Asb.h"
#include "Sarbaz.h"
#include "Shah.h"
#include "Vazir.h"
#include "Fil.h"
#include "Galeh.h"
#include <typeinfo>
using namespace std;

Mohreh* safheh[8][8];

void func( void );
void print( void );
void zero( void );
void deletefunc( void );

int main()
{
	

	safheh[1][0] = new Sarbaz( 1,0, black, sarbaz, '^' );
	safheh[1][1] = new Sarbaz( 1,1, black, sarbaz, '^' );
	safheh[1][2] = new Sarbaz( 1,2, black, sarbaz, '^' );
	safheh[1][3] = new Sarbaz( 1,3, black, sarbaz, '^' );
	safheh[1][4] = new Sarbaz( 1,4, black, sarbaz, '^' );
	safheh[1][5] = new Sarbaz( 1,5, black, sarbaz, '^' );
	safheh[1][6] = new Sarbaz( 1,6, black, sarbaz, '^' );
	safheh[1][7] = new Sarbaz( 1,7, black, sarbaz, '^' );
	
	safheh[6][0] = new Sarbaz( 6,0, white, sarbaz, '^' );
	safheh[6][1] = new Sarbaz( 6,1, white, sarbaz, '^' );
	safheh[6][2] = new Sarbaz( 6,2, white, sarbaz, '^' );
	safheh[6][3] = new Sarbaz( 6,3, white, sarbaz, '^' );
	safheh[6][4] = new Sarbaz( 6,4, white, sarbaz, '^' );
	safheh[6][5] = new Sarbaz( 6,5, white, sarbaz, '^' );
	safheh[6][6] = new Sarbaz( 6,6, white, sarbaz, '^' );
	safheh[6][7] = new Sarbaz( 6,7, white, sarbaz, '^' );

	safheh[0][0] = new Galeh( 0,0, black, galeh, '@' );
	safheh[0][1] = new Asb( 0,1, black, asb, '#' );
	safheh[0][2] = new Fil( 0,2, black, fil, '%' );
	safheh[0][3] = new Vazir( 0,3, black, vazir, '$' );
	safheh[0][4] = new Shah( 0,4, black, shah, '*' );
	safheh[0][5] = new Fil( 0,5, black, fil, '%' );
	safheh[0][6] = new Asb( 0,6, black, asb, '#' );
	safheh[0][7] = new Galeh( 0,7, black, galeh, '@' );

	safheh[7][0] = new Galeh( 7,0, white, galeh, '@' );
	safheh[7][1] = new Asb( 7,1, white, asb, '#' );
	safheh[7][2] = new Fil( 7,2, white, fil, '%' );
	safheh[7][3] = new Vazir( 7,3, white, vazir, '$' );
	safheh[7][4] = new Shah( 7,4, white, shah, '*' );
	safheh[7][5] = new Fil( 7,5, white, fil, '%' );
	safheh[7][6] = new Asb( 7,6, white, asb, '#' );
	safheh[7][7] = new Galeh( 7,7, white, galeh, '@' );
func( );
	print( );
func( );
	print( );
func( );
	print( );
deletefunc( );
	return (0);
}
void func( void )
{
	int x,y,a,b;
	cout << " Enter X: ";
	cin >> x;
	cout << " Enter Y: ";
	cin >> y;
	cout << " Enter A: ";
	cin >> a;
	cout << " ENTER B: ";
	cin >> b;
	safheh[x][y]->move( a,b );
	
}

void print( void )
{
	
	for( int i=0; i < 8; i++ )
	{
		for( int l=0; l < 20; l++ )
			cout << " -";
		cout << endl;
		for( int j=0; j < 8; j++ ){

			cout << " |";
			if( safheh[i][j] )
				cout << safheh[i][j]->a;
			else 
				cout << " ";
			cout << " |";
		}
		cout << endl;
	}

}



void deletefunc( void )
{
	for( int i=0; i < 8; i++ )
		for( int j=0; j < 8; j++ ){
			if( safheh[i][j] )
				delete safheh[i][j];
		}
}
---------------------------------------------
Asb.cpp
#include "Mohreh.h"
#include "Asb.h"
#include <iostream>
using namespace std;

Asb::Asb(int X, int Y, color c, mohreh_type t, char ch) : Mohreh( X, Y, c, t, ch ) {  }

bool Asb::checkmove( int a, int b )
{
	if( ( a < 0 || b > 7 ) || ( b < 0 || b > 7 ) )
		return false;
	if( ( a == x ) && ( b == y ) )
		return false;
	
	if( (( a == x-1 && b == y+2 ) || ( a == x-2 && b == y+1)
		|| ( a == x-2 && b == y-1 ) || ( a == x-1 && b == y-2 )
		|| ( a == x+1 && b == y-2 ) || ( a == x+2 && b == y-1 )
		|| ( a == x+2 && b == y+1 ) || ( a == x+1 && b == y+2 ))
		&& ( safheh[ a ][ b ]!= 0 ) && ( safheh[ a ][ b ]->col != col ) )
		return true;
	else if( (( a == x-1 && b == y+2 ) || ( a == x-2 && b == y+1)
		|| ( a == x-2 && b == y-1 ) || ( a == x-1 && b == y-2 )
		|| ( a == x+1 && b == y-2 ) || ( a == x+2 && b == y-1 )
		|| ( a == x+2 && b == y +1 ) || ( a == x+1 && b == y+2 ))
		&& (!( safheh[ a ][ b ]!= 0 )) )
		return true;
	else
	    return false;
	
}

void Asb::move( int a, int b )
{
	if( checkmove( a, b ) == true )
	{
		
		if( safheh[a][b] != 0 )
			delete safheh[a][b];
		
		safheh[a][b] = safheh[x][y]; // I want to copy any data of safheh[x][y] to safheh[a][b] except x, y and set safheh[a][b]---> x, y by under code but operator = dont work
		//safheh[a][b]->set(a, b );
		
		safheh[x][y] = 0;
		//safheh[a][b]->x = a;
		//safheh[a][b] -> y = b;
		
		
	}
	else
		cout << "Shoma nemitavanid in harekat ra anjam dahid\n";
}

const Asb &Asb::operator =( const Asb &r )
{
	Mohreh::operator =( r );
	return *this;
}
-------------------------------------
enter x=7
enter y=6
enter a=5
enter b= 5
dont copy safheh[7][6] to safheh[5][5]

please help me

please help me to overload operator = for base class and derived classes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.