user3051411
user3051411

Reputation:

Implement functions from other class files C++

So I'm a little new with C++, and our teacher hasn't gotten to the point of telling us how to use functions from separate class files.

Right now we're just doing text based stuff, but I have it where it randomly picks an enemy type and enemy from arrays. This is the function that's in the separate class file "enemy.cpp"

In the "main.cpp" I want to call this function. (I thought it would be something like "enemy.genRandEnemy()" but it's not working).

Here's the code for the enemy class -

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>

using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;

class enemy{
    public:
        void genRandEnemy();
};

void genRandEnemy() {

    string enemy[] = {"dragon", "troll", "wolf", "wraith", "spider", "scorpion", "hydra", "snake", "reaper", "centipede", "worm"};
    string enemyType[] = {"hell", "ice", "soul eater", "bone", "carnivorous"};

    srand(time(0)); 
    int randomEnemy = rand();
    int randomEnemyType = rand();

    int randEnemy = (randomEnemy % 11);
    int randEnemyType = (randomEnemyType % 5);

    if(randEnemyType == 0){cout << enemyType[0];} 
    else if(randEnemyType == 1){cout << enemyType[1];} 
    else if(randEnemyType == 2){cout << enemyType[2];}
    else if(randEnemyType == 3){cout << enemyType[3];}
    else if(randEnemyType == 4){cout << enemyType[4];}

    cout << " ";

    if(randEnemy == 0){cout << enemy[0];}
    if(randEnemy == 1){cout << enemy[1];}
    if(randEnemy == 2){cout << enemy[2];}
    if(randEnemy == 3){cout << enemy[3];}
    if(randEnemy == 4){cout << enemy[4];}
    if(randEnemy == 5){cout << enemy[5];}
    if(randEnemy == 6){cout << enemy[6];}
    if(randEnemy == 7){cout << enemy[7];}
    if(randEnemy == 8){cout << enemy[8];}
    if(randEnemy == 9){cout << enemy[9];}
    if(randEnemy == 10){cout << enemy[10];}
}

and here's the code snip from the main class --

cout << "Then out comes three " << enemy.genRandEnemy() << "s and they encircle you. ";

Also in the main class, i have up where the includes are -- #include "enemy.cpp"

I don't think i have the enemy class set up right to do this and i'm not sure how.

Upvotes: 0

Views: 399

Answers (2)

SHR
SHR

Reputation: 8333

This is your class:

class Enemy{ //I change it to not confuse with the local variable in genRandEnemy.
    public:
        void genRandEnemy(); //you should return a string instead of the printing
};

the function prototype is:

void Enemy::genRandEnemy() {
...
}

and you use it like this:

int main()
{
   Enemy e;//declare a variable
   //now you use the variable:
   cout << "Then out comes three ";
   e.genRandEnemy();
   cout << "s and they encircle you. ";
   return 0;
}

BTW:

you can short your code:

 if(randEnemyType == 0){cout << enemyType[0];} 
 else if(randEnemyType == 1){cout << enemyType[1];} 
 else if(randEnemyType == 2){cout << enemyType[2];}
 else if(randEnemyType == 3){cout << enemyType[3];}
 else if(randEnemyType == 4){cout << enemyType[4];}

can be replaced by:

 cout<<enemyType[randEnemyType];     

And the same:

 if(randEnemy == 0){cout << enemy[0];}
 if(randEnemy == 1){cout << enemy[1];}
 if(randEnemy == 2){cout << enemy[2];}
 if(randEnemy == 3){cout << enemy[3];}
 if(randEnemy == 4){cout << enemy[4];}
 if(randEnemy == 5){cout << enemy[5];}
 if(randEnemy == 6){cout << enemy[6];}
 if(randEnemy == 7){cout << enemy[7];}
 if(randEnemy == 8){cout << enemy[8];}
 if(randEnemy == 9){cout << enemy[9];}
 if(randEnemy == 10){cout << enemy[10];}

can be replaced by:

 cout << enemy[randEnemy];

Upvotes: 0

5gon12eder
5gon12eder

Reputation: 25459

Your splitting of code into several files is unfortunate. You should put the declaration of types and functions you wish to use in more than one file into a header file. Therefore, create the file enemy.hpp and put enemy's declaration in it:

class enemy
{
public:
  void genRandEnemy();
};

Put the definition of functions into a separate source file and name it enemy.cpp. It will look like this:

#include "enemy.hpp"
#include <iostream>
// All your other includes needed to define the function...

void
enemy::genRandEnemy()
{
  // Implementation of the function...
}

Then, in any file that needs to call enemy::genRandEnemy:

#include "enemy.hpp"

void
f()
{
  enemy foe;
  foe.genRandEnemy();
  // Or whatever...
}

Note that your code is inconsistent: You declare genRandEnemy as a member function of class enemy but define it as a free function outside any class. My example always puts it as a member of enemy. (This actually seems weired to me. A function with that name would be supposed to create a new enemy and return it. If it is a member of enemy, I will need an enemy ahead of time to create one.)

Two other remarks:

  • You are re-seeding the random generator every time the genRandEnemy function is called. This will make for a poor randomness.
  • I'm sure you can come up with a better solution than the pile of if(randEnemy == 1) { cout << enemy[1]; }. Can you?

Upvotes: 1

Related Questions