0

So im trying to make a Die class and a LoadedDie class where the Die class is the base and the LoadedDie class is derived. The only difference between the 2 classes is that the roll function in the LoadedDie has a 25% chance of getting the highest number, whereas the Die, has a 1/6 chance. This is my 1st time working with derived class and i seem to have quite a problem trying to complete this task. Can someone explain to me how it works and show me how i would do it with my classes (as im a visual learner and need examples). I know my base class (Die) works as i tested it out before moving to my LoadedDie.

Die.cpp

#include<iostream>
#include <cstdlib>
using namespace std;

class Die{
    public:
        Die();
        Die(int numSide);
        virtual int roll() const;
        int rollTwoDice(Die d1, Die d2); 
        int side;
};

Die::Die():side(6){}

Die::Die(int numSide):side(numSide){}

int Die::roll() const{
    return rand() % side + 1;
}

int Die::rollTwoDice(Die d1, Die d2){
    return d1.roll()+d2.roll();
}

LoadedDie.cpp

#include<iostream>
#include <cstdlib>
#include "Die.cpp"
using namespace std;

class LoadedDie: public Die{
    public:
        LoadedDie();
        LoadedDie(int numSide);
        virtual int loadedroll() const;
};

LoadedDie::LoadedDie():side(6){}

LoadedDie::LoadedDie(int numSide):side(numSide){}

int LoadedDie::loadedroll() const{
    if ((rand() % 2)+1) = 1){
        return side;    
    }
    return (rand() % (side-1)) + 1;
}

int LoadedDie::rollTwoDice(LoadedDie d1, LoadedDie d2){
    return d1.roll()+d2.roll();
}

My DieTester.cpp (just a main class to test if the above classes work):

#include "LoadedDie.cpp"
#include<iostream>
#include<time.h>
#include <stdlib.h> 
using namespace std;

int main(){

    srand(time(NULL));  

    Die d(6);
    LoadedDie dl(6);
    cout << d.roll()<<endl;
    cout<<"ld " << dl.roll()<<endl;
}

With the classes above, i get this error when i run my DieTester.cpp (if it helps):

In file included from DieTester.cpp:1:
LoadedDie.cpp: In constructor ‘LoadedDie::LoadedDie()’:
LoadedDie.cpp:13: error: class ‘LoadedDie’ does not have any field named ‘side’
LoadedDie.cpp: In constructor ‘LoadedDie::LoadedDie(int)’:
LoadedDie.cpp:15: error: class ‘LoadedDie’ does not have any field named ‘side’
LoadedDie.cpp: In member function ‘virtual int LoadedDie::loadedroll() const’:
LoadedDie.cpp:18: error: expected primary-expression before ‘=’ token
LoadedDie.cpp:18: error: expected ‘;’ before ‘)’ token

I believe that these errors are due to me not deriving the classes properly. Can someone explain to me how this is done?

ShadowViper
  • 365
  • 1
  • 5
  • 24
  • 2
    That is why you break up **declarations** into header files, and **definitions** into cpp files. You should not ever include a cpp file in another cpp file. – Cory Kramer Nov 19 '14 at 17:59
  • 2
    `#include "Die.cpp"` is wrong. Separate out your class declaration to a header file and use a proper include guard. – πάντα ῥεῖ Nov 19 '14 at 17:59
  • Also, use include guards of some sort, like `#pragma once`. And `LoadedDie`'s constructor should construct it's `Die`, not a `side`. – Mooing Duck Nov 19 '14 at 18:01
  • 2
    And `rollTwoDice` should be a free function, or a static function. And should take it's parameters by reference/pointer, not making a copy of a `Die` object. – Mooing Duck Nov 19 '14 at 18:02
  • @MooingDuck, i not sure what u mean by the constructing Die not side? also, how would i make rollTwoDie take in reference/pointer instead of a copy? i havent learned that yet. – ShadowViper Nov 19 '14 at 18:13
  • `Die(int numSide);` should be `explicit`. Also, if you add *any* `virtual` functions, you should also add a `virtual` destructor (even if it is `= default;`. – Deduplicator Nov 19 '14 at 18:35

2 Answers2

2

While the commends about splitting the code into header and code files are all valid (do that NOW) they have nothing to do with the remaining errors and your questions. Lets go through them one by one:

LoadedDie.cpp: In constructor ‘LoadedDie::LoadedDie()’:
LoadedDie.cpp:13: error: class ‘LoadedDie’ does not have any field named ‘side’

LoadedDie indeed has no such member. It is a member of Die. You do not initialize the members of your base class in the derived class like that. Instead you have to call the constructor of the base class like this:

LoadedDie::LoadedDie() : Die() { }

LoadedDie.cpp: In constructor ‘LoadedDie::LoadedDie(int)’:
LoadedDie.cpp:15: error: class ‘LoadedDie’ does not have any field named ‘side’

Same problem again. Same solution:

LoadedDie::LoadedDie(int numSide) : Die(numSide) { }

Note that you can take advantage of default values for arguments and simply write one constructor instead of two:

class Die {
public:
    Die(int numSide = 6);
    ....
}

if ((rand() % 2)+1) = 1){
LoadedDie.cpp: In member function ‘virtual int LoadedDie::loadedroll() const’:
LoadedDie.cpp:18: error: expected primary-expression before ‘=’ token
LoadedDie.cpp:18: error: expected ‘;’ before ‘)’ token

'=' is assignment while you ment equal '=='. You can't assign 1 to (rand() % 2)+1).

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
1

It's generally bad form to #include CPP files. But if you want to do it, get rid of the #include "Die.cpp" statement in main.cpp. It's already included in LoadedDie.cpp and so you are including it twice and defining it twice--hence the 'redefinition' error.

Joseph Willcoxson
  • 5,853
  • 1
  • 15
  • 29
  • I have to do the include cpp just because of the way my prof marks the assigments. I fixed the include die.cpp in the main, but im still getting the errors above. – ShadowViper Nov 19 '14 at 18:11
  • You can't initialize a member of a baseclass--you can't set side. Instead call the baseclass constructor : Die(6) -- in the initializer list. Instead of LoadedDie::LoadedDie():side(6){} LoadedDie::LoadedDie():Die(6){} – Joseph Willcoxson Nov 19 '14 at 18:19
  • ok but how would i make it a LoadedDie then if i initlized it as Die(6)? – ShadowViper Nov 19 '14 at 18:22
  • Instead of LoadedDie::LoadedDie():side(6){} use LoadedDie::LoadedDie():Die(6){} – Joseph Willcoxson Nov 19 '14 at 18:23
  • Instead of LoadedDie::LoadedDie(int numSide):side(numSide){} use LoadedDie::LoadedDie(int numSide):Die(numSide){} – Joseph Willcoxson Nov 19 '14 at 18:25