Class inheritance help

A project ive been working on became to big, and Ive decided to split it into classes. If you've been around this forum you'll see my 2d array project all over, and yes, this is another that is based off the same code :D . I have 2 main classes now, A Map class and a Player class. Currently you friendly forum goers have solved all my problems, so im just updating this code. Feel free to critique me and give me suggestions, but im sure ill have problems soon.

MAIN.CPP
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
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <windows.h>
#include "Map.h"
#include "Player.h"

void cls();
int main()
{
    int * choice = new int;
    std::cout << "How many players would you like in this map?";
    std::cin >> *choice;
    delete choice;
    Map mObj(2);
    mObj.createmap();
    while(std::cin){
        std::cout << "w - up\ns - down\na - left\nd - right\n";
        mObj.InputChoice(); //run input function to see where to move
        cls();
        mObj.RefreshScreen(); //rewrite the array
    }


}



void cls() //windows h function to replace screen with nulls
{
  DWORD n;
  DWORD size;
  COORD coord = {0};
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  GetConsoleScreenBufferInfo ( h, &csbi );
  size = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
  SetConsoleCursorPosition ( h, coord );
}

MAP.CPP
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
#include "Map.h"
#include "Player.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <windows.h>
Map::Map(int noP):
pObj(5,5)
{

}
void Map::InputChoice()
{
    /* i didnt know how to access the player class outside without making another object to player, so i just made a function that
    calls the object, i need this seperate because i need to call input, THEN cls(), then refresh, in that specific order. Comment if you think i should
    move the refresh function into map aswell to make some giant function that handles it. Problem is i feel like im stuffing to much stuff into Map.h and
    .cpp */
    pObj.InputChoice(map, bgmap);
}
void Map::RefreshScreen() //reprint array
{
    for( int nrow = 0; nrow < nrows; nrow++){
            for(int ncol = 0; ncol < ncols;ncol++)
                std::cout << map[nrow][ncol] << " ";
            std::cout << std::endl;
            }
}
void Map::createmap() { //read map in from "Map.txt" file
    using namespace std;
    ifstream MapStruct("Map.txt");
    while(!MapStruct.eof()){
        for( int nrow = 0; nrow < nrows; nrow++){
            for(int ncol = 0; ncol < ncols;ncol++){
                    MapStruct >> map[nrow][ncol];
                    bgmap[nrow][ncol] = map[nrow][ncol];
                }
            }
        }
MapStruct.close();
}

MAP.H
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
#ifndef MAP_H
#define MAP_H
#include <string>
#include "Map.h"
#include "Player.h"
#include <vector>
class Map
{
    public:
        void createmap() ;
        void InputChoice();
        void RefreshScreen();
        Map(int noP); //number of PLAYERS
    private:
        static const int nrows = 20;
        static const int ncols = 20;
        char map[nrows][ncols];
        char bgmap[nrows][ncols];
        Player pObj; //a map HAS a player, a player is NOT a map(thanks you guys for that logic, helped A LOT)


};

#endif // MAP_H

PLAYER.CPP
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
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <windows.h>
#include "Map.h"
#include "Player.h"
Player::Player(int x, int y):
xp(x),
yp(y)
{
}
void Player::InputChoice(char map[][20], char bgmap[][20])
{
        char x;
        std::cin >> x;
         switch(x){
                case 'w' :        //moves 'x' up one and replaces the coord before with what it originally had
                    if(yp <= 0){
                        yp = 0;
                    }else{yp-=1;}
                        map[yp + 1][xp] = bgmap[yp + 1][xp]; //replace last coord with the background map, which stays constant.
                    break;
                case 's':
                    if(yp >= 19){
                        yp = 19;
                    }else{yp+=1;}
                        map[yp - 1][xp] = bgmap[yp - 1][xp];
                    break;
                case 'a':
                    if(xp <= 0){
                        xp = 0;
                    }else{xp -=1;}
                        map[yp][xp + 1] = bgmap[yp][xp + 1];
                    break;
                case 'd':
                    if(xp >= 19){
                        xp = 19;
                    }else{xp +=1;}
                        map[yp][xp - 1] = bgmap[yp][xp - 1];
                    break;
            }
            map[yp][xp] = 'x'; //set current position to 'x', later in this project i will be using a varible when i ask how to put in multiple players
            // and study vectors a little more(lots of learning to do!).
}

PLAYER.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef PLAYER_H
#define PLAYER_H
#include "Map.h"
#include <iostream>
class Player //not much in this class, work in progress
{
    public:
        Player(int x, int y); //takes the players coords as parameters
        void InputChoice(char map[][20], char bgmap[][20]); //uses the current map to move across
    private:
    int xp;
    int yp;
};

#endif

Last edited on
Ok you followed my advice according the background map (not that you gave any feedback)
just wondering why didn't you follow my advice according InputChoice()? I mean now you have extra lines for each case which doing actually all the same, but ok...

I doubt that you need PlayerOne and PlayerTwo. Player suffice. You likely create 2 instances of Player

Inheritance means to extend the functionality of the base class. Usually this is done by overwriting virtual functions.

Here you can't inherit Player(One) from 'Map'. All players in your game are using 1 Map. That means your player class needs a reference/pointer to Map which is set in the constructor. Or (probably better) alternatively the map holds an array/list/vector of player.

Whatever the InputChoice() function of the class Map requires the paramter 'PlayerOne *' where it finds/modifys the actuall x/y coordinate
Last edited on
i think you want to create 1 map and use that map for all the players?
PlayerOne creates a object of PlayerOne combined with Map. So PlayerOne = MAP + extra's.
So you are actually making 2 maps objects , mObj and one inside oO.
one is create from map.txt and one not created at all.

you are able to do this:
oO.createmap();
But i think that is not what you want.
Last edited on
Sorry about not responding coder, i had forgotten after i saw the code, your suggestions were great and im very thankful. But what exactly is the program doing that messes it up? does it just make a copy of map?
does it just make a copy of map?
Yes it does. Each player has it's own map because they inherite one
Updated, thank you coder! your logic helped me solve the problem, i don't think i really did what you were thinking of, but i was able to get it to work using my at hand programming intelligence(not nearly as up to par as yours).
Well, you edited the original post that makes the posts below appear useless...

But as of yet your design looks good.

You can put cls() at the beginning of RefreshScreen() because it has to be (later you may want less flicker). And yes, everything that regards the map has to be within you map class.

If you want more players and more maps (levels) you need pointers and a class that surrounds/holds the players and the maps. Why not naming that class 'Game'. That game class would allow players to change maps...

1
2
3
4
    int * choice = new int;
    std::cout << "How many players would you like in this map?";
    std::cin >> *choice;
    delete choice;
¡¿?!
ne555, i deleted that out, it was arbitrary.

Coder777 - so you mean classes inside of classes? because that actually seems like a great idea! how would you go about calling that? would it be
1
2
Game gObj
gObj.Map.createmap()

see my last post:

you are able to do this:
oO.createmap();
But i think that is not what you want.


You will get all the features of MAP and Player inside Player
Last edited on
Sorry, was a bit busy lately

What do you mean by 'classes inside of classes'?

What I meant was:
1
2
3
4
5
6
7
class CGame // I prefix class names with C since then I can use class name otherwise (i.e. namespaces)
{
  void Run()
  {
    // Put everything from main() to here since classes are reusable, main() not
  }
};


If you're interested, I could show you how I'd design that...

@Jikax
I really don't know what you're trying to say
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class A{
public:
  A();
  ~A();

  void A_func();
};

class B : public A{
public:
  B();
  ~B();

  void B_func();
};

int main()
{
  B b;
  b.B_func();
  b.A_func();

  return 0;
}
Topic archived. No new replies allowed.