Thread: Changing from basic to class structure

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    12

    Changing from basic to class structure

    Basically I did some code for betting on horses here
    here the header with the function prototypes

    Code:
    int assignOdds (int horse) ; // function to assign odds
    int whichHorseToBetOn (void) ; // function to ask which horse to bet on
    int getAmount (void) ;  // function to ask how much to bet
    int runRace (void) ; // function to run the race
    Main file
    Code:
    //Horse Betting
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include "betting.h"
    
    using namespace std ;
    
    int Cash = 100;   // starting cash
    int const NUMBEROFHORSES = 8 ;  
    int Bet = 0 ;  // how much have they bet
    int horseWon = 0 ; 
    int x =0 ;  
    int odds[7] ; // array which holds each horses odds
    int tempOdds = 0 ; 
    int winner = 0; 
    int thisBet = 0; // holds horse bet on
    int Amount = 0; //. holds amount bet this race
    
    int main()
    	{
    
    	while (Cash > 0)	
    		{
    		cout << "You have " << Cash << " to spend. " ;  
    
    		for(x=0	;	x<NUMBEROFHORSES	;	x++)	//loop asign and display odds
    		{
    	odds[x]= assignOdds(x) ;
    	cout << "Horse " << x+1 << " Odds are " << odds[x] <<"/1 " ;
    		}																				
    	cout << "Which Horse do you want to bet on -  " ;   
    	Bet = whichHorseToBetOn() ;						
    thisBet=getAmount();								
    	cout << endl ;
    winner=runRace();// selects winning horse randomly with runRace() function
    
    	if (winner+1 == Bet) {							
    		Cash = Cash+(thisBet*odds[winner])+thisBet ;	// If you have picked the winner then calculate earnings
    	}
    }
    
    }
    
    
    
    int assignOdds (int horse) // assign random odds to horse
    {
    	tempOdds=(rand()%10)+1;
    	return tempOdds ;
    }
    
    int whichHorseToBetOn ()	
    {
    	cin >> Bet ;
    	return Bet ;
    }
    
    int getAmount ()	
    {
    	cout << "How much do you want to bet? 
     
     You currently have " << Cash << " to spend -  ";
    	cin >> Amount ;
    	Cash=Cash-Amount ;
    	return Amount ;
    }
    
    int runRace () // calculates randomnly which horse has won the race and displays it on screen
    {
    	horseWon=(rand()%8);
    	cout << "Horse " << horseWon+1 << " has won the race! 
    "  ;
    
    	cout << endl << endl << endl ;
    	return horseWon;
    }
    Now its a fairly simple program but what I want to do now is to do it with class's
    So i'm writing this off the top of my head
    race.h file
    Code:
    class race:
    {
           public:
              race();
             virtual ~race();
           int assignodds(int horse);
          int whichhorsetobeton(void);
          int getAmount(void);
          int RunRace(void);
    
          private:
    
    
    }
    then my race.cpp would be
    Code:
    race::race
    {
    
    }
    race::~race()
    {
    }
    
    int race::assignOdds(int horse)
    {
        tempOdds=(rand()%10)+1;
        return tempodds;
    }
    
    int race::getAmount()
    {
    cout << "How much do you want to bet? 
     You currently have " << Cash << " to spend -  ";
    	cin >> Amount ;
    	Cash=Cash-Amount ;
    	return Amount ;
    }
    
    int race::whichhorsetobeton()
    {
    cin >> Bet ;
    	return Bet ;
    }
    
    int race::runRace()
    {
    	horseWon=(rand()%8);
    	cout << "Horse " << horseWon+1 << " has won the race! "  ;
    	cout << endl << endl << endl ;
    	return horseWon;
    }
    Haven't time to write a main.cpp to call all that in,so is any of it right?
    I'm pretty sure the race.cpp is wrong,I think I have to put in setters and getters in there but I'm pretty iffy on those

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I am going to give some general advice.
    First off, improve indentation.
    Secondly, don't confuse getters with setters. Getters returns an existing value and setters set an existing value. See setters as naming someone and getters as getting that someone's name.
    Thirdly, don't use std::cin inside the class. Make it a habit to have the class functions accept arguments with the required data instead. Putting std::cin inside the class just hurts flexibility. A lot. Your main program logic should decide what data to get from where and then pass that on to the class. It's a lot more flexible this way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    class race:
    {
           public:
              race();
             virtual ~race();
           int assignodds(int horse);
          int whichhorsetobeton(void);
          int getAmount(void);
          int RunRace(void);
     
          private:
     
     
    }
    With syntax errors in tact....

    Haven't time to write a main.cpp to call all that in,so is any of it right?
    Define right. Are you required to write the race class? I don't like the design. It's a god object. I would fix it like this: take out all of the stuff related to gambling, and put that in a different class. In main, use the gambling object first. Then, run the race. Afterward, use the information from the race object to know who won the bet.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Well I'm meant to do is write a program using OOP principles and dynamic
    memory allocation.I thought it would be easier to write the program first,then bring in classes and then use memort allocation.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    So I started off it very basic,just trying to output my cash value in my main.cpp.I know I'm doing something very stupid below but I'm not sure.

    Code:
    #ifndef RACE_H
    #define RACE_H
    
    class race
    {
        public:
            race();
            virtual ~race();
            int getCash();
    
        private:
        int Cash ;   // starting cash
    };
    
    #endif // RACE_H
    Code:
    #include "race.h"
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    race::race()
    {
    
        int Cash = 100;
    }
    race::~race()
    {
        
    }
    int race::getCash()
    {
        return Cash;
    }
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include "race.h"
    
    using namespace std;
    
    int main()
    
    	{
        race ob;
        ob.getCash();
    cout << "You have " << Cash << " to spend. \n \n" ;
    }

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Fixed it
    int cash = 100 should be just cash =100;
    and my cout should read
    cout << "You have " << ob.getCash() << " to spend. \n \n" ;
    Silly mistakes really....can't wait to make some ore in the rest of the program

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by fallenangel View Post
    Well I'm meant to do is write a program using OOP principles and dynamic
    memory allocation.I thought it would be easier to write the program first,then bring in classes and then use memort allocation.
    That's kinda backwards, especially with regards to classes. OOP is an approach to program design, so you should use it to design. Classes are tools to help your OOP programs easier to write.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Quote Originally Posted by King Mir View Post
    That's kinda backwards, especially with regards to classes. OOP is an approach to program design, so you should use it to design. Classes are tools to help your OOP programs easier to write.
    Ya I know I'm just not that familiar with classes,I find it easier it just write the program in one big main even though that completely backwards.
    Anyway I'm having problems with my for loop now
    Orginally it was this
    Code:
    for(x=0	;	x<NUMBEROFHORSES	;	x++)	//loop asign and display odds
    		{
    	odds[x]= assignOdds(x) ;
    	cout << "Horse " << x+1 << " Odds are " << odds[x] <<"/1 " ;
    		}
    and I tried changing it to this
    Code:
    for(ob.getx()=0	;ob.getx()<ob.getnumhorses();ob.getx()++)	
    		{
    
    		}
    this is my current header and cpp file that goes with it.
    Code:
    #ifndef RACE_H
    #define RACE_H
    
    
    class race
    {
        public:
            race();
            virtual ~race();
           int getCash();
           int getx();
           int getnumhorses();
        private:
        int Cash ;   // starting cash
        int numberofhorses ;  // total number of horses to bet on
        int x  ;  // used in "for" loop as a counter
    
    };
    #endif // RACE_H
    Code:
    #include "race.h"
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    race::race()
    {
    
        Cash = 100;
       // int tempOdds = 0 ;
         x =0 ;
         numberofhorses = 8 ;
    
    }
    race::~race()
    {
        //dtor
    }
    int race::getCash()
    {
        return Cash;
    }
    int race::getx()
    {
        return x;
    }
    int race::getnumhorses()
    {
        return numberofhorses;
    }
    I'm thinking the error had something to do with not being able to increment a function

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm thinking the error had something to do with not being able to increment a function
    That's a good guess, but doesn't your compiler give you an error message? It's better if you give us that information.

    Anyway, think of how many times you need to get x. You could do a couple things. One is You could change a copy of x, which means call getx() once to get a value, and store it in a variable. Then use the variable. Although, I don't like that the for loops variable is a member of the race class for no reason.

    Code:
    for (int x = 0; x != ob.getnumhorses(); x++)
    Isn't this the same thing?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>for(ob.getx()=0 ;ob.getx()<ob.getnumhorses();ob.getx()++)
    This is not going to work. getx() and getnumhorses() returns a copy of the current value it has stored, so whatever changes you make it are lost to the void.
    What you coulddo is make setters. Which way is the right depends entirely on what you want to accomplish. If you can get away with no setters, then don't do it.
    Also, getnumhorses is a bad name. It's difficult to read. I recommend you separate the parts somehow, such as get_num_horses or GetNumHorses. Much easier to read.

    Remember this also: design is important to have gotten right from the beginning. Of course, that's entirely not possible, but the better the "foundation," the worse will the effect of evolving the program will be.
    That is to say, you will save yourself a lot of time by figuring out the design first and implementing than by adding code and later changing that into a foundation.
    If you're going to do OOP, it's much better to actually do the OOP part from the beginning than later. Learn by doing!
    Last edited by Elysia; 12-15-2011 at 03:19 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Remember this also: design is important to have gotten right from the beginning. Of course, that's not possible,
    It's completely possible. Especially important is design in nontrivial work.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's supposed to say "not entirely possible." Forgot about that.
    Since we are imperfect, there will always be moments where we'll find that our design just isn't enough because we forgot about some things.
    But the more we minimize such moments, the better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Ack I give up,I just don't know enough about class's and setters and getters.Gonna try and re-write my orginal code using dynamic memory

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by fallenangel View Post
    Ack I give up,I just don't know enough about class's and setters and getters...
    Don't give up!
    If you give up, you will never become a programmer! Life is full of hardships, and one of them is problem solving!

    ...Gonna try and re-write my orginal code using dynamic memory
    That's just going to increase the complexity of your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Easily frustrated I guess
    So on my orginal code if I were to replace the key word Cash with
    int *ptr;
    ptr = new int;
    *ptr = 100;
    so would replacing cash with *ptr work?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic help with the class structure
    By hobscrk777 in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2006, 04:31 PM
  2. Changing variable's type ... basic
    By dean11 in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2005, 10:16 PM
  3. Replies: 1
    Last Post: 11-23-2003, 08:51 AM
  4. reading structured arrays, changing their structure
    By _kevin007 in forum C Programming
    Replies: 6
    Last Post: 05-15-2003, 03:49 PM
  5. Changing a derived class to a base class?
    By roktsyntst in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2002, 09:40 PM