Creating a Derived Class in C++

1k Views Asked by At

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?

2

There are 2 best solutions below

5
On BEST ANSWER

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.

0
On

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).