Thread: new to OOP

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    101

    new to OOP

    well I was wondering if I was doing everything right, can you do this: variable[class[x].er], if not then I know where some errors are coming from..

    During comp. sci. I thought up a good way to make a game where you own a buisness, txt based, but I figured if I made it then I'd learn some more about OOP. It's not that I don't know OOP it's that I've ben coding structured for ever.
    Some of the variables are unused at this point but there going to be used later. I know this is rudundent but I figured on having the program compleate by tomowrow. But I don't wan't to continue with this many errors!
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    class mn; //main class
    
    class store //holds each stor's values
    {
          int l, security, size, ad; //location, .., advertisment/popularity
          float h;
       public:
          store();
    };
    
    store::store()
    {
       security = 1;
       size = 1;
       ad = 1;
    }
    
    class mn
    {
          double m, mm, buildc, addc, emwadge[3];//money, last turn money, c=cost
          int numemployed[3], numstores;        //em = emploey - num = number
          double crime[4], cost[4], p[4]; //..., cost to build, population
          double tax[4];
          char ls[4]; //string.. this variable gives me troubles farther down
       public:
          mn();
          int ats();
          int bs();
          //int a();
          //int h();
          int c();
    };
    
    mn::mn()
    {
       m = 50000.00;
       buildc = 10000;
       addc = 10000; //add - aditions.. cost                 salsmen, system admen
       emwadge[0] = 7;  emwadge[1] = 10;  emwadge[2] = 15;//maniger
       numemployed[0] = 0;  numemployed[1] = 0; numemployed[2] = 0;
       numstores = 0;
       //the folowing lines are giving errors: invalid conversion from...
       crime[0] = 2; cost[0] = 1; tax[0] = 1.06; ls[0] = "Charleston(WV)";
       p[0] = 30;
       
       crime[1] = 5; cost[1] = 2.5; tax[1] = 1.08; ls[1] = "Chicago"; p[1] = 60;
       crime[2] = 7; cost[2] = 5; tax[2] = 1.15; ls[2] = "New Yourk"; p[2] = 100;
       crime[3] = 10; cost[3] = 4; tax[3] = 1.1; ls[3] = "Los Angelas"; p[3] = 100;
       //I'm aware these figure aren't acurate, I'll do research later
       store store[9];
    }
    
    int mn::ats()//add on - make the store bigger
    {
       if (numstores == 0){
          cout <<"No stores yet/n";  return 0;}
          
       int x, xx;
       cout <<"/nCost: "<<addc<<"/nWhich one?/n";
       
       for (x = 1;  x <= numstores; x++)
       {
          cout <<"Store "<<x <<"is "<<store[x-1].size <<" big and is located "
               <<store[x-1].ls <<endl; //parse error before '[' token
       }
       
       cin >> x;
       
       if (x > numstores){
          cout <<"Store doesn't exist/n";  return 0;}
       
       if (store[x].size >15){  //parse error before '[' token
          cout <<"Sore too big/n";  return 0;}
          
       cout <<"How much?/n"; //global scope: sytax error before '<<' token
       
       cin >> xx; //global scope: sytax error before '<<' token
       
       if (xx * addc * tax[store[x].l] > m){
          cout <<"Not Enough Money/n";  return 0;}
       
       store[x].size += xx;
       m -= xx * addc * tax[store[x].l;//global scope: sytax error before '-=' token
       
       numemployed[0] += xx * 2;//global scope: sytax error before '+=' token
       numemployed[1] += xx;//global scope: sytax error before '+=' token
       
       if (store[x].size == 5)
          numemployed[2]++;
       if (store[x].size == 10)
          numemployed[2]++;
       if (store[x].size == 15)
          numemployed[2]++;
          
       return 0;
    }
    
    int mn::bs()//build new store
    {
       int x;
       
       cout <<"Where?/n";
       
       for (x = 0; x < 4; x++)
       {
          cout <<ls[x] <<" " <<x <<"-   Crime: "<<crime[x] <<" Cost: "
               <<cost[x] * buildc <<" Tax: "<< tax[x] <<endl;
       }
       
       cin >> x;
       
       if (x > 3){
          cout <<"Location doesn't exist/n";  return 0;}
       
       if (cost[x] * buildc > m){
          cout <<"Not enough money/n";  return 0;}
       
       numstores++;
       
       store[numstores].l = x;//parse error before '[' token
       
       m -= cost[x] * buildc;
       numemployed[0] += 3;
       numemployed[1] += 1;
       numemployed[2] += 1;
    }
    
    /*int mn::a()
    {
    
    }*/
    
    int mn::c()//calculates profits and such
    {
       mm = m;
       
       if (numstores == 0)
          return 0;
       
       float r1;
       
       srand(time(0));
       
       r1 = 1+rand() %80;
       int x;
    
       for (x = 0; x < 3; x++)
          m -= emwadge[x] * numemployed[x];
       
       for (x = 0; x < numstores + 1; x++)
          m -= store[x].size * 2000 * tax[store[x].l] * cost[store[x].l];//parse [
    
       for (x = 0; x < numstores + 1; x++)
          m += r1 * store[x].ad * p[store[x].l] * store[x].size;//pase before [
    }
    
    
    int main()
    {
       int choice = 0;//choice is initialized!!
       
       mn mn;
       
       while(1)
       {
          system("CLS");
          cout <<mm<<endl;//previous balence
          cout <<m <<endl <<endl;//curent balence
          cout <<"(1)Add to store, (2)Build Store, (3)Advertise, (4)Continue/n";
          cin >> choice; //choice undeclared..
          if (choice == 1)
             mn.ats();//parse error before ';'
          else{
             if (choice == 2)
                mn.bs();
             else{
                //if (choice == 3)
                   //mn.a();
                else{
                   //if (choice == 4)
                      //mn.h();
                   else
                      mn.c();}}}//parse error before '}'
       }
    }
    The errors are marked, it should be just a couple lines causing all my problems, then one method of doing somethign: asigning a char a string..
    --

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    My suggestion: start over completely...Try writing a little code and compiling, and then a little more then compile it again and repeat until you are finished.

    Maybe start with your classes:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    class store //holds each stor's values
    {
          int loc; //just one example, 'l' is not very easy to understand
          //you might want to change a lot more in class mn as well
          int security;
          int size;
          int  ad; 
    //make it readable
          float h;
       public:
          store();
    };
    
    store::store()
    {
    
    }
    
    class mn
    {
    //...
    //what does mn stand for anyway?
    };
    
    int main
    {
    store MyStore;
    
    //No compile errors?
    //Ok then you can write a little more...
    
    }
    I would recommend compiling after you finish writing each function, or even more often if it is a large function.

    Edit: I think my post may have been a bit vague. What I really think you should do is write clearer code, and compile much more often. If you compile early on you could save yourself a lot of trouble.

    Also, you don't seem to get the concept of classes. A class is a type like int, char or double (in a way) so just like you do
    Code:
    int aInteger;
    //you can also do
    store aStore;
    Hope that helps
    Last edited by JaWiB; 01-16-2004 at 07:46 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    mn mn;
    is roughly equivalent to saying
    Code:
    int int;
    Give your object a unique name just as you would any variable.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    101
    I understand that, I just couldn't think of anything better.
    --

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Couldn't think of anything better? You could do Mn, mN, mn_, and any number of variations like that. Or if you actually want people to understand your code, you could provide descriptive identifiers. Imagine you had to debug a program where a class or important variable was named c. You'd wanna kill 'im.

    Echo previous posts. Take baby steps. Have you ever written a class before? Start small, and make sure your programs have a clearly defined purpose.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by 1veedo
    I understand that, I just couldn't think of anything better.
    Well, a descriptive name is good, but a unique identifier for your variable is mandatory. That line of code will generate an error, and the rest of the uses of that object become ambiguous (that is, the compiler has no clue what you're doing).

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    101
    Ok, I'll start over..
    --

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP flu
    By Hussain Hani in forum General Discussions
    Replies: 15
    Last Post: 06-27-2009, 02:02 AM
  2. Should OOP be any new language priority??
    By Hussain Hani in forum General Discussions
    Replies: 80
    Last Post: 06-13-2009, 10:56 AM
  3. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 10:32 AM
  4. recommendation for a good OOP book
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2006, 04:28 PM
  5. OOP Theory Question
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2005, 08:37 AM