Strategy Pattern - interface coding help

Discussion in 'C++' started by CK, Jun 15, 2007.

  1. CK

    CK Guest

    Good Morning All,
    I am writing an app to calculate the cost of a hotel room. I am new to c++.
    I have decided to try using the Strategy Design Pattern. I want a superclass
    HotelRoom, and then make 3 classes that inherit from it. SuiteRoom,
    StandardRoom, and DeluxeRoom. The price of the room is based on 3 factors,
    if there is are more then 2 people in the room, there is a $10 charge per
    persons over 2, if they add an extra bed, and the room type. I was thinking
    of making a RoomType interface with a price() method, then making 3 classes
    that implement the interface, SuitePrice, StandardPrice, and DeluxePrice. I
    do not know how to code interfaces in c++. Does anyone have any samples to
    get me going? Ultimately this will be a console app. I want to ask the user
    how many rooms they would like and for how many days? Then make an array of
    HotelRooms with the number of elements (rooms) the user indicated. I would
    then loop through and ask how many people in each room, and if they wanted
    an extra bed in the room. The ultimate goal would be to display the total
    price for all rooms to the user. Any advice?

    TIA,
    ~ck
     
    CK, Jun 15, 2007
    #1
    1. Advertisements

  2. Like,

    int main() {
    return 0;
    }

    ? Or, maybe

    #include <iostream>
    int main() {
    std::cout << "Hello World\n";
    }

    ?
    I think it's mostly covered in FAQ 5.2. Read it and see if it helps.
    If it doesn't, come back and ask more specific questions.

    V
     
    Victor Bazarov, Jun 15, 2007
    #2
    1. Advertisements

  3. CK

    CK Guest

    I am sorry. How do I view FAQ 5.2? Sorry. I am a new newbie. Thanks!
     
    CK, Jun 15, 2007
    #3
  4. CK

    Daniel T. Guest

    This sounds suspiciously like a homework question (and not a very good
    one at that.) If the only difference between the rooms is the price,
    then create an array.
    int price( int numRooms, int numDays, int numPeople, bool extraBed )
    {
    // implement here
    }

    int main() {
    assert( 10 == price( 0, 0, 0, true ) );
    // add more asserts for different inputs
    }
     
    Daniel T., Jun 15, 2007
    #4
  5. Marcelo Pinto, Jun 15, 2007
    #5
  6. CK

    CK Guest

    CK, Jun 15, 2007
    #6
  7. CK

    James Kanze Guest

    Just like you'd code them in any language, really. In C++, use
    an abstract class. (And don't forget to give it a virtual
    destructor.)
    class SomeInterface
    {
    public:
    virtual ~SomeInterface() {}
    virtual double getSomething() const = 0 ;
    virtual void doSomething() = 0 ;
    // ...
    } ;

    In C++, we speak of a function being "pure virtual", instead of
    abstract, and declare it as such by appending a "= 0" after the
    function declaration.
    Get a good introductory text on C++, and a good introductory
    text on OO design and programming. From what I've heard,
    "Thinking in C++", by Bruce Eckel
    (http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html), is a
    good introduction to both. (I can only go by what I've heard
    here. It's been a long time since I read any introductory
    texts.)
     
    James Kanze, Jun 16, 2007
    #7
  8. CK

    BobR Guest

    /* """
    Get a good introductory text on C++, and a good introductory
    text on OO design and programming. From what I've heard,
    "Thinking in C++", by Bruce Eckel
    (http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html), is a
    good introduction to both. (I can only go by what I've heard
    here. It's been a long time since I read any introductory
    texts.)
    """*/

    "TiC++" is not a 'beginner' book. Mr. Eckel assumes some knowledge of some
    programming language.

    Mr. F. Glassborow has (what I've heard) a good 'beginner' book.
    OP: search for "You can do it". [ I can't find the ref/link right now. ]
     
    BobR, Jun 16, 2007
    #8
  9. CK

    James Kanze Guest

    Yes. I took a quick glance at it after I had posted. My (very
    superficial) impression is that it was just as good as I'd
    heard, but as you say, it presumes that you are already a
    programmer of some sort.
    I've heard both good and bad about both it and "Accelerated C++"
    (by Koenig). I suspect that for beginners text, different
    people need different approaches, and that both are very good
    books, for some people, and not so good for others. As far as I
    know, both concentrate purely on the C++ part, however, ignoring
    any of the programming aspects.

    FWIW: many years back, there was a book by Robert Martin that
    was one of the best I've seen. Somebody seems to have walked
    off with my copy, so I can't give a more detailed reference.
    But at any rate, it is extremely out of date, not mentionning
    any of the standard containers (which didn't exist when the book
    appeared), and using the old Booch cloud diagrams rather than
    UML.
     
    James Kanze, Jun 16, 2007
    #9
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.