Interactive story using classes

This is a discussion on Interactive story using classes within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; class hell {public: int number2; void hot () {cout << "you like the fire, ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Buxton UK
    Posts
    14

    Interactive story using classes

    Code:
    #include <iostream>
     using namespace std;
    
     class hell
    {public:
          int number2;
          void hot ()
          {cout << "you like the fire, huh?\npress 1:yes or 2:no\n";
          cin >> number2;
          
          if (number2 == 1)
          {   system ("CLS");
              cout << "ice prison for you then\n";}
          else if (number2 == 2)
          {   system ("CLS");
              cout << "it gets hotter\n";}
          }};
    
     class heaven
    {public:
          int number3;
          void cold ()
          {cout << "Blow the mist?\npress 1:yes or 2:no\n";
          cin >> number3;
          
          if (number3 == 1)
          {   system ("CLS");
              cout << "You see paradise all around\n";}
          else if (number3 == 2)
          {   system ("CLS");
              cout << "Well then this is interesting!\n";}
          
          }};
    
    int main()
    {
         int number;
         hell h1;
         heaven h2;
    
         cout << "press 1 to go to hell or 2 to go to heaven:\n";
         cin >> number;
         system ("CLS");
         
         if (number == 1)
              h1.hot ();    
         else if (number == 2)
             h2.cold ();
    
             else {cout << "You stay in limbo\n";}
    
     return 0;
    }
    I have been trying to get my head around classes and why they are helpful. I originaly wrote the whole program in main then changed it.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    100
    It's not clear what point you're making or what question you're asking. If you're wanting a critique, keep the following in mind:

    #1. Be very careful about using system(). It's very non-portable, and you probably haven't addressed that issue in your code.

    #2. You said something about trying to figure out why classes are helpful. This is not a very good example. Most of the time they're very much used as variables, in addition to collections of functions. Here the program technically makes variables out of them, but they're just wrappers around the functionality. You'll see the benefit of classes a whole lot more when you start using them a lot to model nouns in your program, not simply verbs.

    #3. You don't really have to make a variable of either class until the last second in this case.

    Code:
    if (number == 1)
    {
         hell h1;
         h1.hot();
    }
    In other C-style languages, you can do stuff like:

    Code:
    if (number == 1)
    {
         new hell().hot();
    }
    or:

    Code:
    if (number == 1)
    {
         hell.hot(); // requires that the function be shared/static, which would be a good idea in this case
    }
    I don't remember if C++ has static ("shared" in some languages) functions or if you can use syntax like:

    Code:
    new hell().hot()
    but look into it. At the very least, since you aren't doing anything with the classes except in the if-else statement blocks, you can (and should for space and clarity) hold off on creating those variables until the last second, as in the first example. Which vaguely leads into the next point:

    #4. Be more mindful of scope. In the last point, a lot of what what was being discussed was implicitly a matter of you giving those variables a broader scope than is relevant. But another thing is that you have a variable called "number" in main(), one called "number2" in hell, and one called "number3" in heaven. This is unnecessary. Their scopes don't overlap at all in this case, so you can (and should for clarity/neatness) just call them all "number".

    You're getting there. Still look into these points though for future coding.
    Last edited by jsrig88; 06-12-2012 at 12:53 PM.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    100
    Just took a look into a couple of things about the third point. In C++ you apparently cannot do something like:

    Code:
    new hell().hot();
    It has to be:

    Code:
    hell h1;
    h1.hot();
    However you can use static functions. For instance declare hell::hot() as:

    Code:
    static void hot()
    { . . .
    Then use it like:

    Code:
    if (number == 1)
    {
        hell::hot();
    }
    But you also need to move number2's declaration into the definition of hot() and number3's declaration into the definition of cold(). This or making them static will be necessary if you make those functions static.
    Last edited by jsrig88; 06-12-2012 at 12:54 PM.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    Quote Originally Posted by jsrig88 View Post
    In C++ you apparently cannot do something like:

    Code:
    new hell().hot();
    It has to be:

    Code:
    hell h1;
    h1.hot();
    You can, but it won't be of much use.
    (I can think of only a few uses.)
    Code:
    #include<iostream>
    #include<memory>
    class Foo
    {
    public:
      void foo(){std::cout<<"foo";};
    };
    int main()
    {
      std::make_shared<Foo>()->foo();
    }
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    If you wanted to, you could also just invoke the hot() method on a temporary. Assuming that hell is a class with a parameterless default constructor, you could ...

    Code:
    hell().hot();
    where hell() creates the temporary that is then used to call the hot() method.

    What good that would do is debatable and depends very much on how your class / method is implemented and what you're trying to achieve.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Buxton UK
    Posts
    14
    Thank you all for sharing opinions. I understand some of what you say and it is helpful.

    #1. Be very careful about using system(). It's very non-portable, and you probably haven't addressed that issue in your code.
    I don't understand what you mean here. How should i go about fixing this?

  7. #7
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Well, the CLS command you're using obviously exists only on Windows systems. Is it really necessary to clear the screen after each question?

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    Quote Originally Posted by Ben Atkins View Post
    I don't understand what you mean here. How should i go about fixing this?
    Well, in this case, not clearning the screen is a possible solution.
    Console programs are supposed to share the console with others, and interact with a pipe like mechanism (hidden beneath the iostreams or stdio.h functions.)
    Or you could write enough '\n' (newlines) so that the screen appears to be cleared.
    Or you could use a console API like ncurses (or Windows APi, I have no idea what it provides for this, though).
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Buxton UK
    Posts
    14
    Iv gone for using lots of \n's in my next project. Thanks again for giving me advice. Im having lots of fun now with some different code, so will post that in the near future if/when i have problems.

    Cheers

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    100
    Glad y'all corrected me on that. It would've been stupid for C++ to not have something like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interactive vs non-interactive terminal session question
    By Overworked_PhD in forum Tech Board
    Replies: 2
    Last Post: 06-18-2009, 08:30 PM
  2. Interactive Programming
    By kavanakt in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2005, 02:56 AM
  3. Interactive Telnet
    By PenguinFeva in forum C Programming
    Replies: 4
    Last Post: 05-27-2005, 10:28 PM
  4. Interactive C
    By Kaiya in forum C Programming
    Replies: 1
    Last Post: 09-30-2004, 09:20 PM
  5. A story...
    By BMJ in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 08-17-2002, 12:55 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21