Strategy Pattern - interface coding help

C

CK

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
 
V

Victor Bazarov

CK said:
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?

Like,

int main() {
return 0;
}

? Or, maybe

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

?
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?

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
 
D

Daniel T.

CK said:
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.

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.
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?

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
}
 
J

James Kanze

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++.

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.)
Does anyone have any samples to get me going?

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.
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?

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.)
 
B

BobR

/* """
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. ]
 
J

James Kanze

James Kanze <[email protected]> wrote in message...
/* """
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.

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.
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. ]

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.
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top