polymorphism

What's wrong with this? when I compile it, it says missing ; before :

CShape::checkMovement():lBlock[bstate][i][0],lBlock[bstate][i][1];

What I want is to inherit the x,y values of the subclass with this function...
I'm still learning the object-oriented concepts of C++

Thanks. here are some parts of my code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class CShape{
	//class CShape checks the movement and rotation of the blocks
	protected:
		int x, y, bstate, fill, shape, i, j, p, q, r, s, t, count;
		char solid, blank;
		bool newBlock;
		static const int GREEN = 10, TEAL = 11, RED = 12, PINK = 13, YELLOW = 14;
		static const int occupied = 1, unoccupied = 0;
		CGraphics graph;
		virtual bool checkMovement();
	public:
		CShape(int xl = 39, int yl = 4): x(xl), y(yl){}
                virtual ~CShape();
		virtual int deleteBlocks(int addScore);
		virtual int addUpScore(int sc);
		void set_values (int xaxis, int yaxis, int st, int bl, int sh, bool nextbl)
				{x = xaxis; y = yaxis; bstate = st; fill = bl; shape = sh; newBlock = nextbl;}
		int xyLoc[50][25];
};

bool CShape::checkMovement()
{
       //I want to change the value of x and y using the value of x and y from
       //the subclass...
		if((xyLoc[x][y] == 1) && (fill == solid))
			return false;
		graph.posShape(x,y,GREEN,fill);
		if(newBlock == true){
			color.xyColor[x][y] = GREEN;
		}//end if
		if(fill == solid)
			xyLoc[x][y] = occupied;
		else
			xyLoc[x][y] = unoccupied;
	return true;
}
class CShapeL : public CShape
{
	public:
		virtual bool rotateMoveBlocks();
};

bool CShapeL::rotateMoveBlocks()
{	
	for(i = 34; i<46; i++){
		for(j=3; j<23; j++){
			if(i < 35 || i > 44){
				xyLoc[i][j] = 1;
			}
			else if(((i>34 && i<45))&&(j<4 || j>21))
				xyLoc[i][j] = 1;
		}//end for
	}//end for
	solid = (char)001; blank = (char)000;
	int lBlock[4][4][3] = //assigns the xy positions of four blocks, 10 is green
		{{{x-1,y,10},{x,y,10},{x+1,y,10},{x+1,y+1,10}},//stateL = 0 default position ™™|
		{{x,y-1,10},{x,y,10},{x,y+1,10},{x-1,y+1,10}},//stateL = 1  
		{{x+1,y,10},{x,y,10},{x-1,y,10},{x-1,y-1,10}},//stateL = 2  
		{{x,y+1,10},{x,y,10},{x,y-1,10},{x+1,y-1,10}}};//stateL = 3 
	for(i = 0; i < 4; i++)
             //I want the checkMovement function inherit the x, y location
             //of this subclass...
             CShape::checkMovement():lBlock[bstate][i][0],lBlock[bstate][i][1];
	}
	return true;
}
Last edited on
CShape::checkMovement():lBlock[bstate][i][0],lBlock[bstate][i][1];

What do you expect the colon after checkmovement() to do? Is that a typo or do you think that is proper syntax? I don't understand what you are asking.
Last edited on
Functions don't inherit anything. Classes do.

If you want to pass additional information to a function, pass it as a parameter.
Hmm...

I guess I'm totally wrong with this one...

I only copied the format from a constructor I've seen and think that it can be applied to functions

1
2
3
4
5
6
7
8
9
10
11
12
13
class Enemy
{
    public:
        Enemy(int damage = 10): m_Damage(damage){}
    private:
        m_Damage;
}

class Boss: public Enemy
{
    public:
        Boss(int damage = 30): Enemy(damage){}
}


i got this code from "Beginning C++ Game Programming" by Michael Dawson.

I tried using constructor but i can't initialize the values since the values depends on the passed values.

I think the format is only applicable for constructors....

Well, I think I must pass the x,y values like this one?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
bool CShape::checkMovement(int x, int y)
{
       //I want to change the value of x and y using the value of x and y from
       //the subclass...
		if((xyLoc[x][y] == 1) && (fill == solid))
			return false;
		graph.posShape(x,y,GREEN,fill);
		if(newBlock == true){
			color.xyColor[x][y] = GREEN;
		}
		if(fill == solid)
			xyLoc[x][y] = occupied;
		else
			xyLoc[x][y] = unoccupied;
	return true;
}

bool CShapeL::rotateMoveBlocks()
{
       //omitted for brevity

	for(i = 0; i < 4; i++)
             //I want the checkMovement function inherit the x, y location
             //of this subclass...
             CShape::checkMovement(lBlock[bstate][i][0],lBlock[bstate][i][1]);
	}
	return true;
}


What I want is to explore more about polymorphism...

can anyone here provide a very good link for polymorphism with examples?

Thanks
I think the format is only applicable for constructors...


It's called the "initializer list", and you're right... it can only be used in constructors.


Well, I think I must pass the x,y values like this one?


Yes... that example looks like it's what you need to do.

But beware that you now have a name conflict! Because you have both a local 'x' and 'y' (local to checkMovement), and a member 'x' and 'y' (part of the CShape class).

This will compile okay and will use the local variables, but I still highly recommend against having conflicting names like this.

What I want is to explore more about polymorphism...


From what I can tell this particular problem doesn't seem to have very much to do with polymorphism.

I don't have a link to any particular tutorial or anything, sadly. Unless there's one on this site that I don't know about, which is very possible (to be honest I never looked over most of the tutorials and stuff on this site).
There is a tutorial on this site, though I am getting tired of pointing to it:
http://www.cplusplus.com/doc/tutorial/polymorphism/

1
2
3
4
5
6
while(i != people.getAmount() ){
     if(people[i].needHelp() ){
          people[i].currentPage = "http://www.cplusplus.com/doc/tutorial";
     }
     ++i;
}


if only ...
Last edited on
@Disch
yeah I already fixed the name conflict. Thanks

@ DrChill
I already red that tutorial...
but I want more complex examples...

now my problem is...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

class CShape{
	//class CShape checks the movement and rotation of the blocks
	protected:
		//other functions is omitted for brevity
		static const int occupied = 1, unoccupied = 0;
		static const int LBORDER = 34, RBORDER = 45, TBORDER = 3, BBORDER = 22;
		CGraphics graph;
	public:
		virtual bool checkMovement(int xl, int yl);
                //other functions is omitted for brevity
		int xyLoc[50][25];
};
void CShape::setBorderValues()
{
	for(i = LBORDER; i < RBORDER+1; i++){
		for(j = TBORDER; j < BBORDER+1; j++){
			if(i < LBORDER+1 || i > RBORDER-1){
				xyLoc[i][j] = occupied;//value of 1 means that the position is filled or occupied
			}
			else if(((i > LBORDER && i < RBORDER)) && (j < TBORDER+1 || j > BBORDER-1))
				xyLoc[i][j] = occupied;
		}//end for
	}//end for
}

bool CShape::checkMovement(int xl, int yl)
{
                //problem here
                //this xyLoc[xl][yl] ignores the xyLoc values from setBorderValues
                //like for example if xl = LBORDER it does not return false;
		if((xyLoc[xl][yl] == occupied) && (fill == solid))
			return false;//rotation or movement of the blocks is not allowed

		graph.posShape(xl,yl,GREEN,fill);
		if(newBlock == true){
			color.xyColor[xl][yl] = GREEN;
		}//end if
		if(fill == solid)
			xyLoc[xl][yl] = occupied;//set the position to occupied
		else//deletes/clears the position of the current object.
			xyLoc[xl][yl] = unoccupied;//set the position to unoccupied
	return true;
}

Last edited on
this xyLoc[xl][yl] ignores the xyLoc values from setBorderValues


No it doesn't.

What's more likely happening is:

1) you didn't call setBorderValues for this particular object
or
2) you did call it, but then changed xyLoc later
or
3) memory corruption.

This looks to be more of a problem with the code calling these functions than with the functions themselves.


Also.. what is this? Is this like a tetris thing or something?
yeah it is a tetris game....

Oops...

I forgot to call the object in my main function...

Thanks a lot...
Topic archived. No new replies allowed.