473,774 Members | 2,206 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strategy Pattern - interface coding help

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
8 2154
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
CK
I am sorry. How do I view FAQ 5.2? Sorry. I am a new newbie. Thanks!

"Victor Bazarov" <v.********@com Acast.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
"CK" <c_**********@h otmail.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
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
CK
Thanks man. That was very helpful!

"Marcelo Pinto" <mp******@gmail .comwrote in message
news:11******** **************@ q69g2000hsb.goo glegroups.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
On Jun 15, 5:14 pm, "CK" <c_kettenb...@h otmail.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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 16 '07 #7

James Kanze <ja*********@gm ail.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
On Jun 16, 7:39 pm, "BobR" <removeBadB...@ worldnet.att.ne twrote:
James Kanze <james.ka...@gm ail.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 "Accelerate d 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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 16 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3256
by: syncman | last post by:
I think there are 2 options for how to implement the Strategy pattern. One is to use polymorphism; derived classes have the same interface and can be plugged in. The other is to use templates: Instantiate the class, passing in a functor (algorithm). Code samples below. The problem with the first way is that the interface must be the same (parameters passed in) for all strategies. The problem with the second way is that there is no...
5
1932
by: Ben Jeurissen | last post by:
Hello, I have to deal with the following issue in C++: Two threads are started from the main thread, both capturing images from a different firewire camera. Both threads take shots of 460800 bytes at a rate of 30 frames per second. This works fine, without frame loss, and I can display the two framestreams on screen. Now I would like to write these frames to my disk too (2 10000rpm in
4
1989
by: Claudio Jolowicz | last post by:
I am trying to find a solution to the following design problem (code at the bottom): We are implementing a trader agent that can trade with other traders on an electronical trading platform. To make the trader more extensible, we have defined a strategy interface and implemented this interface for different trading strategies. The problem relates to how to connect the trader and the strategy. The problem is tricky because the strategy...
1
9941
by: Derek | last post by:
Hi All, I am developing a Windows based application that consists of several different modules. These modules are effectively separate from each other yet share information in a common database. I would like to be able deliver specific modules depending on what the needs of the client are. Some clients may only need Module A and B, while others may need the functionality in Module D. What I am trying to figure out is the best way to go...
2
1797
by: philippe sillon | last post by:
Hi, I have a problem to implemente the strategy pattern. the problem come from that a method take different arguments type (object in interface and String / Int in implemented class). When price3 instance call getPrice, the method invoked is the one of PriceRule abstract class. But, I want to invoke the method of PriceruleForRegion. So I think the solution is somewhere on casting to original type (PriceRuleForRegion) but I don't how I...
6
4706
by: Daniel Santa Cruz | last post by:
Hello all, I've been trying to go over my OO Patterns book, and I decided to try to implement them in Python this time around. I figured this would help me learn the language better. Well, I've gotten stuck with my first go at OO patterns with Python. I guess it goes without say that some of the stuff that are taken for granted in most of the books (ie. Interfaces, Abstract classes) don't really apply to Python per say, but the idea...
5
2069
by: pythoncurious | last post by:
Hi python experts In C++ I can do something like this: class Base { public: void f() { this->f_(); } private: virtual void f_() = 0; };
0
1360
by: ltruett | last post by:
I'm almost done my series of design patterns using PHP 5. Today's pattern is the Strategy Pattern. http://www.fluffycat.com/PHP-Design-Patterns/Strategy/ In the Stratedy Pattern a "family of algorythms" is used interchangably by calling clients. This is the second pattern in as many days as I've used my older work with patterns in Java, and again I liked the old example when I first
3
1475
by: Mike TI | last post by:
Aug 2, 2007 12:00pm Hi all I have to design and program an application which will be used in three countries. It would be a fairly large application with lot of data entry. The database hence has to be consolidated and at one location. Now the internet is excellent in one country and reasonable in the other
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.