By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,203 Members | 1,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,203 IT Pros & Developers. It's quick & easy.

Strategy Pattern - interface coding help

P: n/a
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
Jun 15 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
CK wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 15 '07 #2

P: n/a
CK
I am sorry. How do I view FAQ 5.2? Sorry. I am a new newbie. Thanks!

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f4**********@news.datemas.de...
CK wrote:
>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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Jun 15 '07 #3

P: n/a
"CK" <c_**********@hotmail.comwrote:
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
}
Jun 15 '07 #4

P: n/a
I am sorry. How do I view FAQ 5.2? Sorry. I am a new newbie. Thanks!

Do not top-post.
http://www.parashift.com/c++-faq-lit...t.html#faq-5.2

HTH,

Marcelo Pinto

Jun 15 '07 #5

P: n/a
CK
Thanks man. That was very helpful!

"Marcelo Pinto" <mp******@gmail.comwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
>I am sorry. How do I view FAQ 5.2? Sorry. I am a new newbie. Thanks!

Do not top-post.
http://www.parashift.com/c++-faq-lit...t.html#faq-5.2

HTH,

Marcelo Pinto

Jun 15 '07 #6

P: n/a
On Jun 15, 5:14 pm, "CK" <c_kettenb...@hotmail.comwrote:
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/...gInCPP2e.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 (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 16 '07 #7

P: n/a

James Kanze <ja*********@gmail.comwrote 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/...gInCPP2e.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. ]

--
Bob R
POVrookie
Jun 16 '07 #8

P: n/a
On Jun 16, 7:39 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
James Kanze <james.ka...@gmail.comwrote 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/...gInCPP2e.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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 16 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.