Passing Parameters

S

Sonia

Hi, Please look at the following code. This is a simple program for a game
of craps - very basic.
I have declared the gameStatus as global variable. I was wondering if there
is a better way of handling this (possibly via local variables)
I am using that variable to keep track of the status of the game and pass
it back to main() from craps() where I either increase or decrease a bank
balance.

Should this be handled with local parameters ? would that be a better way.
If so, how would I do that ?

Thanks

Following is the code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int rollDice();
int craps();

enum Status {CONTINUE, WON, LOST};
Status gameStatus;

int main(){

int bankBalance = 1000;
int wager;
char answer;

do {
cout<<"Place a wager: ";
cin>>wager;

while (wager >= bankBalance) {
cout<<"\nYour bank balance is: "<<bankBalance<<endl;
cout<<"Please enter a valid wager: "<<endl;
cin>>wager;
}


craps();

if (gameStatus == WON) {
bankBalance = bankBalance + wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;

}
else {
bankBalance = bankBalance - wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;
}

cout<<"Do you want to play again? (y/n)";
cin>>answer;

} while( answer =='y' || answer == 'Y'); // end of while

return 0;
}

int craps() {

int sum, myPoint;

srand(time(0));
sum = rollDice();

switch (sum) {

case 7:
case 11:
gameStatus = WON;
break;

case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout<<"Point is: "<<myPoint<<endl;

}

while(gameStatus==CONTINUE) {
sum=rollDice();

if(sum==myPoint)
gameStatus = WON;
else if (sum==7)
gameStatus = LOST;
}

if (gameStatus == WON)
cout<<"Player wins"<<endl;
else
cout<<"Player Loses"<<endl;

return(gameStatus);
}

int rollDice() {

int die1, die2, workSum;

die1 = 1+rand() % 6;
die2 = 1+rand() % 6;

workSum = die1 + die2;
cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<endl;

return workSum;

}
 
M

Mike Wahler

Sonia said:
Hi, Please look at the following code. This is a simple program for a game
of craps - very basic.
I have declared the gameStatus as global variable. I was wondering if there
is a better way of handling this (possibly via local variables)
I am using that variable to keep track of the status of the game and pass
it back to main() from craps() where I either increase or decrease a bank
balance.

Should this be handled with local parameters ? would that be a better way.
If so, how would I do that ?

You don't need to do any of that. See below.
Thanks

Following is the code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int rollDice();
int craps();

enum Status {CONTINUE, WON, LOST};

Status gameStatus;

Remove this line.
int main(){

int bankBalance = 1000;
int wager;
char answer;

do {
cout<<"Place a wager: ";
cin>>wager;

while (wager >= bankBalance) {
cout<<"\nYour bank balance is: "<<bankBalance<<endl;
cout<<"Please enter a valid wager: "<<endl;
cin>>wager;
}

Remove this line.
if (gameStatus == WON) {

Replace with:

if (craps() == WON) {
bankBalance = bankBalance + wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;

}
else {
bankBalance = bankBalance - wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;
}

cout<<"Do you want to play again? (y/n)";
cin>>answer;

} while( answer =='y' || answer == 'Y'); // end of while

return 0;
}

-Mike
 
G

Gianni Mariani

Sonia said:
Hi, Please look at the following code. This is a simple program for a game
of craps - very basic.
I have declared the gameStatus as global variable. I was wondering if there
is a better way of handling this (possibly via local variables)
I am using that variable to keep track of the status of the game and pass
it back to main() from craps() where I either increase or decrease a bank
balance.

Should this be handled with local parameters ? would that be a better way.
If so, how would I do that ?

Thanks

Following is the code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int rollDice();
int craps();

enum Status {CONTINUE, WON, LOST};
Status gameStatus;

int main(){

int bankBalance = 1000;
int wager;
char answer;

do {
cout<<"Place a wager: ";
cin>>wager;

while (wager >= bankBalance) {
cout<<"\nYour bank balance is: "<<bankBalance<<endl;
cout<<"Please enter a valid wager: "<<endl;
cin>>wager;
}


craps();

if (gameStatus == WON) {
bankBalance = bankBalance + wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;

}
else {
bankBalance = bankBalance - wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;
}

cout<<"Do you want to play again? (y/n)";
cin>>answer;

} while( answer =='y' || answer == 'Y'); // end of while

return 0;
}

int craps() {

int sum, myPoint;

srand(time(0));
sum = rollDice();

switch (sum) {

case 7:
case 11:
gameStatus = WON;
break;

case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout<<"Point is: "<<myPoint<<endl;

}

while(gameStatus==CONTINUE) {
sum=rollDice();

if(sum==myPoint)
gameStatus = WON;
else if (sum==7)
gameStatus = LOST;
}

if (gameStatus == WON)
cout<<"Player wins"<<endl;
else
cout<<"Player Loses"<<endl;

return(gameStatus);
}

int rollDice() {

int die1, die2, workSum;

die1 = 1+rand() % 6;
die2 = 1+rand() % 6;

workSum = die1 + die2;
cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<endl;

return workSum;

}

Try to create a few classes. See - no globals !


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


class Dice
{
public:

int rollDice() {

int die1, die2, workSum;

die1 = 1+rand() % 6;
die2 = 1+rand() % 6;

workSum = die1 + die2;
cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<endl;

return workSum;
}

};

class CrapsGame : public Dice
{
public:

enum Status {CONTINUE, WON, LOST};
Status gameStatus;

int craps() {

int sum, myPoint;

srand(time(0));
sum = rollDice();

switch (sum) {

case 7:
case 11:
gameStatus = WON;
break;

case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout<<"Point is: "<<myPoint<<endl;

}

while(gameStatus==CONTINUE) {
sum=rollDice();

if(sum==myPoint)
gameStatus = WON;
else if (sum==7)
gameStatus = LOST;
}

if (gameStatus == WON)
cout<<"Player wins"<<endl;
else
cout<<"Player Loses"<<endl;

return(gameStatus);
}

};


class CrapsPlayer
{
public:

int bankBalance;

CrapsPlayer()
: bankBalance( 1000 )
{
}

int PlaceAWager()
{
int wager;

cout<<"Place a wager: ";
cin>>wager;

while (wager >= bankBalance) {
cout<<"\nYour bank balance is: "<<bankBalance<<endl;
cout<<"Please enter a valid wager: "<<endl;
cin>>wager;
}

return wager;
}

void PlayCraps( int wager, CrapsGame & game )
{
game.craps();

if (game.gameStatus == CrapsGame::WON) {
bankBalance += wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;

}
else
{
bankBalance -= wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;
}
}

bool AskToPlay()
{
char answer;
cout<<"Do you want to play again? (y/n)";
cin>>answer;

return answer =='y' || answer == 'Y';
}

};

int main(){

int wager;

CrapsGame game;

CrapsPlayer player;

do {

player.PlayCraps( player.PlaceAWager(), game );

} while( player.AskToPlay() ); // end of while

return 0;
}
 
S

Sonia

John Harrison said:
Gianni Mariani said:
Sonia said:
Hi, Please look at the following code. This is a simple program for a game
of craps - very basic.
I have declared the gameStatus as global variable. I was wondering if there
is a better way of handling this (possibly via local variables)
I am using that variable to keep track of the status of the game and pass
it back to main() from craps() where I either increase or decrease a bank
balance.

Should this be handled with local parameters ? would that be a better way.
If so, how would I do that ?
[snip]


Try to create a few classes. See - no globals !


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


class Dice
{
public:

int rollDice() {

int die1, die2, workSum;

die1 = 1+rand() % 6;
die2 = 1+rand() % 6;

workSum = die1 + die2;
cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<endl;

return workSum;
}

};

class CrapsGame : public Dice
{
public:

enum Status {CONTINUE, WON, LOST};
Status gameStatus;

int craps() {

int sum, myPoint;

srand(time(0));
sum = rollDice();

switch (sum) {

case 7:
case 11:
gameStatus = WON;
break;

case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout<<"Point is: "<<myPoint<<endl;

}

while(gameStatus==CONTINUE) {
sum=rollDice();

if(sum==myPoint)
gameStatus = WON;
else if (sum==7)
gameStatus = LOST;
}

if (gameStatus == WON)
cout<<"Player wins"<<endl;
else
cout<<"Player Loses"<<endl;

return(gameStatus);
}

};

[snip]

Deriving CrapsGame from Dice? Poor design surely, there's no obvious
releationship.

Make Dice a freestanding function and class it from CrapsGame (or anywhere
else).

john

Thank You all, John, Gianni and Mike. That helped a lot. I am working my
way to classes, this is only chapter 3 for me. But thanks in advance. It
makes it much clearer to see an example that's done without classes and them
seeing classes created on an example familiar to me.
Mike, when you say he derived a CrapsGame from dice are you referring to the
"class CrapsGame : public Dice" line of code ? Do you suggest that a dice
should be left as a function rather than a class ?
Thank You again.
 
S

Sonia

John Harrison said:
You don;t need parameters you need craps() to return the game status, see
below.


Remove line above.


Add this

Status gameStatus;


Replace with this

gameStatus = craps();


Replace with this

Status craps() {


You were almost there already.

john

Yes John, Thanks, this is what I was thinking as well, just wasn't sure how
to arrange it. Now it makes sense. I appreciate it.
Sonia
 
S

Sonia

John Harrison said:
You don;t need parameters you need craps() to return the game status, see
below.


Remove line above.


Add this

Status gameStatus;


Replace with this

gameStatus = craps();


Replace with this

Status craps() {


You were almost there already.

john

John, I've made the changes, the code don't seem to be compiling though.
This is what I get now:
I am using MC Visual C++ 6.0
--------------------Configuration: ex3_54_fixed - Win32
Debug--------------------
Compiling...
ex3_54_fixed.cpp
f:\programming\c++\how to program\chapter3\ex3_54_fixed.cpp(33) : error
C2440: '=' : cannot convert from 'int' to 'enum Status'
Conversion to enumeration type requires an explicit cast
(static_cast, C-style cast or function-style cast)
f:\programming\c++\how to program\chapter3\ex3_54_fixed.cpp(55) : error
C2556: 'enum Status __cdecl craps(void)' : overloaded function differs only
by return type from 'int __cdecl craps(void)'
f:\programming\c++\how to program\chapter3\ex3_54_fixed.cpp(8) : see
declaration of 'craps'
f:\programming\c++\how to program\chapter3\ex3_54_fixed.cpp(55) : error
C2371: 'craps' : redefinition; different basic types
f:\programming\c++\how to program\chapter3\ex3_54_fixed.cpp(8) : see
declaration of 'craps'
f:\programming\c++\how to program\chapter3\ex3_54_fixed.cpp(65) : error
C2065: 'gameStatus' : undeclared identifier
Error executing cl.exe.
What am I missing ..here's the modified code:
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int rollDice();
int craps();

enum Status {CONTINUE, WON, LOST};
//Status gameStatus; // line removed..inserted into main()

int main(){

Status gameStatus; // added here as opposed to global declaration

int bankBalance = 1000;
int wager;
char answer;



do {
cout<<"Place a wager: ";
cin>>wager;

while (wager >= bankBalance) {
cout<<"\nYour bank balance is: "<<bankBalance<<endl;
cout<<"Please enter a valid wager: "<<endl;
cin>>wager;
}
//craps(); // removed
gameStatus = craps(); // substituted for the above line

if (gameStatus == WON) {
bankBalance = bankBalance + wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;

}
else {
bankBalance = bankBalance - wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;
}

cout<<"Do you want to play again? (y/n)";
cin>>answer;

} while( answer =='y' || answer == 'Y'); // end of while


return 0;

}
// int crap() { // removed
Status craps() { // substituted for the above line
int sum, myPoint;

srand(time(0));
sum = rollDice();

switch (sum) {

case 7:
case 11:
gameStatus = WON;
break;

case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout<<"Point is: "<<myPoint<<endl;

}

while(gameStatus==CONTINUE) {
sum=rollDice();

if(sum==myPoint)
gameStatus = WON;
else if (sum==7)
gameStatus = LOST;
}

if (gameStatus == WON)
cout<<"Player wins"<<endl;
else
cout<<"Player Loses"<<endl;



return(gameStatus);
}

int rollDice() {

int die1, die2, workSum;

die1 = 1+rand() % 6;
die2 = 1+rand() % 6;

workSum = die1 + die2;
cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<endl;

return workSum;

}
 
S

Sonia

John Harrison said:
OK my fault, see below.


This prototype need to change as well

Status craps();

And also this line needs to be moved after the definition of Status below.


Forgot to add a variable declaration here

Status gameStatus;


That should do it.

john

John, That Did it :)
Thanks...
Good night now, Its way past my bed time :)
Sonia
 
J

John Harrison

Gianni Mariani said:
Sonia said:
Hi, Please look at the following code. This is a simple program for a game
of craps - very basic.
I have declared the gameStatus as global variable. I was wondering if there
is a better way of handling this (possibly via local variables)
I am using that variable to keep track of the status of the game and pass
it back to main() from craps() where I either increase or decrease a bank
balance.

Should this be handled with local parameters ? would that be a better way.
If so, how would I do that ?
[snip]


Try to create a few classes. See - no globals !


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


class Dice
{
public:

int rollDice() {

int die1, die2, workSum;

die1 = 1+rand() % 6;
die2 = 1+rand() % 6;

workSum = die1 + die2;
cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<endl;

return workSum;
}

};

class CrapsGame : public Dice
{
public:

enum Status {CONTINUE, WON, LOST};
Status gameStatus;

int craps() {

int sum, myPoint;

srand(time(0));
sum = rollDice();

switch (sum) {

case 7:
case 11:
gameStatus = WON;
break;

case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout<<"Point is: "<<myPoint<<endl;

}

while(gameStatus==CONTINUE) {
sum=rollDice();

if(sum==myPoint)
gameStatus = WON;
else if (sum==7)
gameStatus = LOST;
}

if (gameStatus == WON)
cout<<"Player wins"<<endl;
else
cout<<"Player Loses"<<endl;

return(gameStatus);
}

};

[snip]

Deriving CrapsGame from Dice? Poor design surely, there's no obvious
releationship.

Make Dice a freestanding function and class it from CrapsGame (or anywhere
else).

john
 
J

John Harrison

Sonia said:
Hi, Please look at the following code. This is a simple program for a game
of craps - very basic.
I have declared the gameStatus as global variable. I was wondering if there
is a better way of handling this (possibly via local variables)
I am using that variable to keep track of the status of the game and pass
it back to main() from craps() where I either increase or decrease a bank
balance.

Should this be handled with local parameters ? would that be a better way.
If so, how would I do that ?

You don;t need parameters you need craps() to return the game status, see
below.
Thanks

Following is the code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int rollDice();
int craps();

enum Status {CONTINUE, WON, LOST};
Status gameStatus;

Remove line above.
int main(){

int bankBalance = 1000;
int wager;
char answer;

Add this

Status gameStatus;
do {
cout<<"Place a wager: ";
cin>>wager;

while (wager >= bankBalance) {
cout<<"\nYour bank balance is: "<<bankBalance<<endl;
cout<<"Please enter a valid wager: "<<endl;
cin>>wager;
}


craps();

Replace with this

gameStatus = craps();
if (gameStatus == WON) {
bankBalance = bankBalance + wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;

}
else {
bankBalance = bankBalance - wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;
}

cout<<"Do you want to play again? (y/n)";
cin>>answer;

} while( answer =='y' || answer == 'Y'); // end of while

return 0;
}

int craps() {

Replace with this

Status craps() {
int sum, myPoint;

srand(time(0));
sum = rollDice();

switch (sum) {

case 7:
case 11:
gameStatus = WON;
break;

case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout<<"Point is: "<<myPoint<<endl;

}

while(gameStatus==CONTINUE) {
sum=rollDice();

if(sum==myPoint)
gameStatus = WON;
else if (sum==7)
gameStatus = LOST;
}

if (gameStatus == WON)
cout<<"Player wins"<<endl;
else
cout<<"Player Loses"<<endl;

return(gameStatus);
}

int rollDice() {

int die1, die2, workSum;

die1 = 1+rand() % 6;
die2 = 1+rand() % 6;

workSum = die1 + die2;
cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<endl;

return workSum;

}

You were almost there already.

john
 
J

John Harrison

Thank You all, John, Gianni and Mike. That helped a lot. I am working my
way to classes, this is only chapter 3 for me. But thanks in advance. It
makes it much clearer to see an example that's done without classes and them
seeing classes created on an example familiar to me.
Mike, when you say he derived a CrapsGame from dice are you referring to the
"class CrapsGame : public Dice" line of code ? Do you suggest that a dice
should be left as a function rather than a class ?
Thank You again.

I'm certainly suggesting that, not sure about Mike.

And yes, "class CrapsGame : public Dice" says that Craps game is a class
with derives from another class called Dice.

No problem with CrapsGame or CrapsPlayer being a class.

This sort of thing starts lots of arguments however, so don't take my word
for it.

john
 
J

John Harrison

John, I've made the changes, the code don't seem to be compiling though.

OK my fault, see below.
This is what I get now:
I am using MC Visual C++ 6.0
--------------------Configuration: ex3_54_fixed - Win32
Debug--------------------
Compiling...
ex3_54_fixed.cpp
f:\programming\c++\how to program\chapter3\ex3_54_fixed.cpp(33) : error
C2440: '=' : cannot convert from 'int' to 'enum Status'
Conversion to enumeration type requires an explicit cast
(static_cast, C-style cast or function-style cast)
f:\programming\c++\how to program\chapter3\ex3_54_fixed.cpp(55) : error
C2556: 'enum Status __cdecl craps(void)' : overloaded function differs only
by return type from 'int __cdecl craps(void)'
f:\programming\c++\how to program\chapter3\ex3_54_fixed.cpp(8) : see
declaration of 'craps'
f:\programming\c++\how to program\chapter3\ex3_54_fixed.cpp(55) : error
C2371: 'craps' : redefinition; different basic types
f:\programming\c++\how to program\chapter3\ex3_54_fixed.cpp(8) : see
declaration of 'craps'
f:\programming\c++\how to program\chapter3\ex3_54_fixed.cpp(65) : error
C2065: 'gameStatus' : undeclared identifier
Error executing cl.exe.
What am I missing ..here's the modified code:
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int rollDice();
int craps();

This prototype need to change as well

Status craps();

And also this line needs to be moved after the definition of Status below.
enum Status {CONTINUE, WON, LOST};
//Status gameStatus; // line removed..inserted into main()

int main(){

Status gameStatus; // added here as opposed to global declaration

int bankBalance = 1000;
int wager;
char answer;



do {
cout<<"Place a wager: ";
cin>>wager;

while (wager >= bankBalance) {
cout<<"\nYour bank balance is: "<<bankBalance<<endl;
cout<<"Please enter a valid wager: "<<endl;
cin>>wager;
}
//craps(); // removed
gameStatus = craps(); // substituted for the above line

if (gameStatus == WON) {
bankBalance = bankBalance + wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;

}
else {
bankBalance = bankBalance - wager;
cout<<"Your bank balance is $"<<bankBalance<<endl;
}

cout<<"Do you want to play again? (y/n)";
cin>>answer;

} while( answer =='y' || answer == 'Y'); // end of while


return 0;

}
// int crap() { // removed
Status craps() { // substituted for the above line
int sum, myPoint;

Forgot to add a variable declaration here

Status gameStatus;
srand(time(0));
sum = rollDice();

switch (sum) {

That should do it.

john
 
G

Gianni Mariani

John said:
....

Deriving CrapsGame from Dice? Poor design surely, there's no obvious
releationship.

Make Dice a freestanding function and class it from CrapsGame (or anywhere
else).

Craps is a game of dice. It's 100% obvious to me that this is a IS-A
relationship. For example if I wanted to simulate "loaded" dice ?

Anyhow, this particular function rolls twice and returns the sum. It's
very particular to craps and potentially other similar games. In
hindsight, it should give you both values and not the sum.

Sonia, this is an excellent example of how 2 people can come up with
wildly different opinions even though they've been at it for a while.
It really does not matter either way how you do this. There are more
obvious and clearer examples of where you need to inherit vs have a member.

I suggest you keey your threshhold for creating a class is very low.
Small interesting chunks are much easier to understand and maintain that
large spaghetti plates.
 
K

Karl Heinz Buchegger

Gianni said:
Craps is a game of dice. It's 100% obvious to me that this is a IS-A
relationship. For example if I wanted to simulate "loaded" dice ?

Huch.
Craps is a game. It certainly is *not* a dice
For it to play you need dice.
So craps *has-a* dice.

class craps
{
dice the_dice;
};

Same with cars and engines. A car is not an engine, but a car contains
an engine.
 
D

David White

Gianni Mariani said:
Craps is a game of dice.

But your class Dice is not a dice game. It's the dice themselves. A dice
game _uses_, or _has_, dice. It also has players and rules etc.
It's 100% obvious to me that this is a IS-A
relationship.For example if I wanted to simulate "loaded" dice ?

Then you might derive a class LoadedDice from Dice, and have your Craps
game - an object of an unrelated class - contain a LoadedDice object instead
of a Dice object.

DW
 
S

Sonia

Gianni Mariani said:
Craps is a game of dice. It's 100% obvious to me that this is a IS-A
relationship. For example if I wanted to simulate "loaded" dice ?

Anyhow, this particular function rolls twice and returns the sum. It's
very particular to craps and potentially other similar games. In
hindsight, it should give you both values and not the sum.

Sonia, this is an excellent example of how 2 people can come up with
wildly different opinions even though they've been at it for a while.
It really does not matter either way how you do this. There are more
obvious and clearer examples of where you need to inherit vs have a member.

I suggest you keey your threshhold for creating a class is very low.
Small interesting chunks are much easier to understand and maintain that
large spaghetti plates.

I understand what you are saying, and agree that small chunks are easier to
manipulate than "large plates". I will get to classes very soon. :) Thanks
 
G

Gianni Mariani

Karl said:
Huch.
Craps is a game. It certainly is *not* a dice

So certain are you ?
For it to play you need dice.
So craps *has-a* dice.

class craps
{
dice the_dice;
};

Same with cars and engines. A car is not an engine, but a car contains
an engine.

Cars are built around an engine. I can see how some designs would
inherit from engine. Why not ?

It depends on what you're trying to model with your design.
 
G

Gianni Mariani

David said:
But your class Dice is not a dice game. It's the dice themselves. A dice
game _uses_, or _has_, dice. It also has players and rules etc.

Well, it's obvious this design does not make sense because there are
multiple players in a game and as written, there is only one player.

That aside, there is nothing wrong with the model of the game. Dice is a
game by itself, any property of Dice is also a property of a Craps game.

Unfortunately this example is a really good to show the different ways
you can design somthing but very bad because there is no *right* answer.
Then you might derive a class LoadedDice from Dice, and have your Craps
game - an object of an unrelated class - contain a LoadedDice object instead
of a Dice object.

But how does one now manage LoadedCrapsGame ? A loaded dice will have
methods for managing how to "load" the game. I suspect that the same
interface to load dice could be used to load the game.

Well sure - I can make everything a member and never use inheritance,
it's not very useful to do that and it makes a whole bunch of work for
nothing.
 
P

Peter van Merkerk

Craps is a game. It certainly is *not* a dice
So certain are you ?


Cars are built around an engine. I can see how some designs would
inherit from engine. Why not ?

A car has not a "is a" relationship with an engine, but "has a"
relationship. Since a car is not a engine, inheriting Car from Engine
would be a rather illogical choice for most people I know.
It depends on what you're trying to model with your design.

If you disagree on the definition, than you will also disagree on the
design. However for the Car/Engine example the choice seems to be rather
clear to me.
 
J

jeffc

Gianni Mariani said:
So certain are you ?

I am certain.
Cars are built around an engine. I can see how some designs would
inherit from engine. Why not ?

Because it's incorrect use of inheritance from all accepted standards of OO
design. Your relationship is most definitely not IS-A.
 
G

Gianni Mariani

Peter said:
If you disagree on the definition, than you will also disagree on the
design. However for the Car/Engine example the choice seems to be rather
clear to me.

Clear ? Great, what are you trying to model ?

Since we have NO idea what is being modelled, your clarity perplexes me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,841
Latest member
WilmerBelg

Latest Threads

Top