Code:
1.
My example code of my classes
2.
class Mohreh
3.
{
4.
5.
public:
6.
Mohreh( int X, int Y, color c, mohreh_type mt, char ch ) : x(X), y(Y), col( c ), type( mt ), a( ch ) { }
7.
8.
virtual const Mohreh& operator=( const Mohreh& r )
9.
{
10.
11.
col = r.col;
12.
type = r.type;
13.
a = r.a;
14.
return *this;
15.
}
16.
void set( int a, int b )
17.
{
18.
x=a;
19.
y=b;
20.
}
21.
22.
23.
virtual bool checkmove( int, int ) = 0;
24.
virtual void move( int, int ) = 0;
25.
color col;
26.
char a;
27.
protected:
28.
int x, y;
29.
30.
mohreh_type type;
31.
32.
33.
};
34.
-------------------
35.
extern Mohreh* safheh[8][8];
36.
37.
class Asb : public Mohreh
38.
{
39.
public:
40.
Asb( int X, int Y, color c, mohreh_type t, char );
41.
const Asb &operator=( const Asb& );
42.
43.
bool checkmove( int, int );
44.
void move( int, int );
45.
46.
};
47.
#endif
48.
----------------------
49.
#include "Mohreh.h"
50.
51.
extern Mohreh* safheh[8][8];
52.
53.
class Sarbaz : public Mohreh
54.
{
55.
public:
56.
Sarbaz( int X, int Y, color c, mohreh_type t, char );
57.
bool checkmove( int, int );
58.
void move( int, int );
59.
60.
private:
61.
int counter;
62.
};
63.
#endif
64.
-------------------------
65.
#include "Mohreh.h"
66.
67.
extern Mohreh* safheh[8][8];
68.
69.
class Galeh : public Mohreh
70.
{
71.
public:
72.
Galeh( int X, int Y, color c, mohreh_type t, char );
73.
bool checkmove( int, int );
74.
void move( int, int );
75.
76.
};
77.
#endif
78.
------------------------
79.
function main()
80.
81.
#include <iostream>
82.
#include "Mohreh.h"
83.
#include "Asb.h"
84.
#include "Sarbaz.h"
85.
#include "Shah.h"
86.
#include "Vazir.h"
87.
#include "Fil.h"
88.
#include "Galeh.h"
89.
#include <typeinfo>
90.
using namespace std;
91.
92.
Mohreh* safheh[8][8];
93.
94.
void func( void );
95.
void print( void );
96.
void zero( void );
97.
void deletefunc( void );
98.
99.
int main()
100.
{
101.
102.
103.
safheh[1][0] = new Sarbaz( 1,0, black, sarbaz, '^' );
104.
safheh[1][1] = new Sarbaz( 1,1, black, sarbaz, '^' );
105.
safheh[1][2] = new Sarbaz( 1,2, black, sarbaz, '^' );
106.
safheh[1][3] = new Sarbaz( 1,3, black, sarbaz, '^' );
107.
safheh[1][4] = new Sarbaz( 1,4, black, sarbaz, '^' );
108.
safheh[1][5] = new Sarbaz( 1,5, black, sarbaz, '^' );
109.
safheh[1][6] = new Sarbaz( 1,6, black, sarbaz, '^' );
110.
safheh[1][7] = new Sarbaz( 1,7, black, sarbaz, '^' );
111.
112.
safheh[6][0] = new Sarbaz( 6,0, white, sarbaz, '^' );
113.
safheh[6][1] = new Sarbaz( 6,1, white, sarbaz, '^' );
114.
safheh[6][2] = new Sarbaz( 6,2, white, sarbaz, '^' );
115.
safheh[6][3] = new Sarbaz( 6,3, white, sarbaz, '^' );
116.
safheh[6][4] = new Sarbaz( 6,4, white, sarbaz, '^' );
117.
safheh[6][5] = new Sarbaz( 6,5, white, sarbaz, '^' );
118.
safheh[6][6] = new Sarbaz( 6,6, white, sarbaz, '^' );
119.
safheh[6][7] = new Sarbaz( 6,7, white, sarbaz, '^' );
120.
121.
safheh[0][0] = new Galeh( 0,0, black, galeh, '@' );
122.
safheh[0][1] = new Asb( 0,1, black, asb, '#' );
123.
safheh[0][2] = new Fil( 0,2, black, fil, '%' );
124.
safheh[0][3] = new Vazir( 0,3, black, vazir, '$' );
125.
safheh[0][4] = new Shah( 0,4, black, shah, '*' );
126.
safheh[0][5] = new Fil( 0,5, black, fil, '%' );
127.
safheh[0][6] = new Asb( 0,6, black, asb, '#' );
128.
safheh[0][7] = new Galeh( 0,7, black, galeh, '@' );
129.
130.
safheh[7][0] = new Galeh( 7,0, white, galeh, '@' );
131.
safheh[7][1] = new Asb( 7,1, white, asb, '#' );
132.
safheh[7][2] = new Fil( 7,2, white, fil, '%' );
133.
safheh[7][3] = new Vazir( 7,3, white, vazir, '$' );
134.
safheh[7][4] = new Shah( 7,4, white, shah, '*' );
135.
safheh[7][5] = new Fil( 7,5, white, fil, '%' );
136.
safheh[7][6] = new Asb( 7,6, white, asb, '#' );
137.
safheh[7][7] = new Galeh( 7,7, white, galeh, '@' );
138.
func( );
139.
print( );
140.
func( );
141.
print( );
142.
func( );
143.
print( );
144.
deletefunc( );
145.
return (0);
146.
}
147.
void func( void )
148.
{
149.
int x,y,a,b;
150.
cout << " Enter X: ";
151.
cin >> x;
152.
cout << " Enter Y: ";
153.
cin >> y;
154.
cout << " Enter A: ";
155.
cin >> a;
156.
cout << " ENTER B: ";
157.
cin >> b;
158.
safheh[x][y]->move( a,b );
159.
160.
}
161.
162.
void print( void )
163.
{
164.
165.
for( int i=0; i < 8; i++ )
166.
{
167.
for( int l=0; l < 20; l++ )
168.
cout << " -";
169.
cout << endl;
170.
for( int j=0; j < 8; j++ ){
171.
172.
cout << " |";
173.
if( safheh[i][j] )
174.
cout << safheh[i][j]->a;
175.
else
176.
cout << " ";
177.
cout << " |";
178.
}
179.
cout << endl;
180.
}
181.
182.
}
183.
184.
185.
186.
void deletefunc( void )
187.
{
188.
for( int i=0; i < 8; i++ )
189.
for( int j=0; j < 8; j++ ){
190.
if( safheh[i][j] )
191.
delete safheh[i][j];
192.
}
193.
}
194.
---------------------------------------------
195.
Asb.cpp
196.
#include "Mohreh.h"
197.
#include "Asb.h"
198.
#include <iostream>
199.
using namespace std;
200.
201.
Asb::Asb(int X, int Y, color c, mohreh_type t, char ch) : Mohreh( X, Y, c, t, ch ) { }
202.
203.
bool Asb::checkmove( int a, int b )
204.
{
205.
if( ( a < 0 || b > 7 ) || ( b < 0 || b > 7 ) )
206.
return false;
207.
if( ( a == x ) && ( b == y ) )
208.
return false;
209.
210.
if( (( a == x-1 && b == y+2 ) || ( a == x-2 && b == y+1)
211.
|| ( a == x-2 && b == y-1 ) || ( a == x-1 && b == y-2 )
212.
|| ( a == x+1 && b == y-2 ) || ( a == x+2 && b == y-1 )
213.
|| ( a == x+2 && b == y+1 ) || ( a == x+1 && b == y+2 ))
214.
&& ( safheh[ a ][ b ]!= 0 ) && ( safheh[ a ][ b ]->col != col ) )
215.
return true;
216.
else if( (( a == x-1 && b == y+2 ) || ( a == x-2 && b == y+1)
217.
|| ( a == x-2 && b == y-1 ) || ( a == x-1 && b == y-2 )
218.
|| ( a == x+1 && b == y-2 ) || ( a == x+2 && b == y-1 )
219.
|| ( a == x+2 && b == y +1 ) || ( a == x+1 && b == y+2 ))
220.
&& (!( safheh[ a ][ b ]!= 0 )) )
221.
return true;
222.
else
223.
return false;
224.
225.
}
226.
227.
void Asb::move( int a, int b )
228.
{
229.
if( checkmove( a, b ) == true )
230.
{
231.
232.
if( safheh[a][b] != 0 )
233.
delete safheh[a][b];
234.
235.
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
236.
//safheh[a][b]->set(a, b );
237.
238.
safheh[x][y] = 0;
239.
//safheh[a][b]->x = a;
240.
//safheh[a][b] -> y = b;
241.
242.
243.
}
244.
else
245.
cout << "Shoma nemitavanid in harekat ra anjam dahid\n";
246.
}
247.
248.
const Asb &Asb::operator =( const Asb &r )
249.
{
250.
Mohreh::operator =( r );
251.
return *this;
252.
}
253.
-------------------------------------
254.
enter x=7
255.
enter y=6
256.
enter a=5
257.
enter b= 5
258.
dont copy safheh[7][6] to safheh[5][5]
i want to overload operator = for base class and derived classes plz help me