C++ Background: Designing and creating classes is a fundamental ability every C+
ID: 3570532 • Letter: C
Question
C++
Background:
Designing and creating classes is a fundamental ability every C++ programmer needs to develop. With this assignment, you will create classes from scratch. When working with C++, you need to be very detail-oriented. The structure of the .h and .cpp files are very important and small differences that you inadvertently type can lead to countless errors. In a past section, a student reported 12 errors from just a single character being wrong. So expect many errors. Or better yet, be very careful as you write C++ The goal of this project is to realize that member variables can be used to remember things. As client driver code calls the methods of the class, the object can remember what was recently done to it by setting private member variables. Another goal of this project to realize that not all methods are created equal. Sometimes, there is an order to the way the operations of a class must be called. Depending on the sequence of steps taken, the object may behave differently. In all the homework from here on out, the top part of the class box (the public methods) are what is being scored. You can implement them however you like, as long as they achieve what I am looking for. 99.5% of you will likely follow the bottom part of the box (the private members) which is my suggestion for how to get it done.
Project 1: Barbeque
The class Barbeque represents a useable cooking device. Implement the class Barbeque so that each instance of a Barbeque has its own string brand and model and three bool values which mark whether the barbeque has coals, is lighted or is cooking. As the sample driver shows, the loadCoals(), light() and cook() methods of Barbeque cannot be called twice or more times in succession. Read the driver code and sample output carefully to understand the ordering of the method calls and the appropriate output statements your class should generate.
The point behind this assignment is to realize that not all methods are created equal. In fact, very often, there is a logical sequence to order in which certain methods can be called. It is the class itself that enforces this ordering. Note the various errors that are printed when driver code doesn't use the Barbeque class correctly, like when it tries to cook without having been lit or when you try to light it without having any coals. As we learn more complex classes, this kind of sequence to the way methods are called is quite typical.
Following the class diagrams shown below, implement the Barbeque class. Embed your class in a suitable test program that provides the sample dialogue shown below. You may choose to create any number of additional methods, but you are required to implement all of the public methods shown below. You are free to ignore the private parts of the class I suggest below.
Implementation Details
Sample Driver
Barbeque
Sample Output
Implementation Details
Sample Driver
Barbeque
Barbeque( string brand, string model );void loadCoals();
void light();
void cook();
void reset();
bool hasCoals();
bool isLit();
bool isCooking(); string my_Brand;
string my_Model;
bool my_hasCoals;
bool my_isLit;
bool my_isCooking; Barbeque bbq( "Coleman", "Grill 101A" );
bbq.loadCoals(); // print output
if (bbq.hasCoals()) {
bbq.light(); // print output
if (bbq.isLit()) {
bbq.cook(); // print output
}
else {
cout << "bbq should be lit so"; cout << " this is an error in"; cout << " your logic!" << endl;
}
}
// clean off the bbq and start over
bbq.reset();
cout << "Let's make some errors..." << endl;
bbq.light(); // print error
bbq.cook(); // print error
bbq.loadCoals(); // print output
bbq.cook(); // print error
Sample Output
Loading Coleman Grill 101A with coals!Lighting the grill!
Cooking food!
Let's make some errors...
You can't light this Coleman Grill 101A until you load it with coals!
You can't cook on this Coleman Grill 101A until it has been lit!
Loading Coleman Grill 101A with coals!
You can't cook on this Coleman Grill 101A until it has been lit!
Explanation / Answer
Barbeque.h
#include<iostream>;
using namespace::std;
class Barbeque
{
public:
Barbeque(string brand, string model);
void loadCoals();
void light();
void cook();
void reset();
bool hasCoals();
bool isLit();
bool isCooking();
private:
string my_Brand;
string my_Model;
bool my_hasCoals ;
bool my_isLit ;
bool my_isCooking;
};
ImAGrill.cpp
#include<iostream>;
#include<string>;
#include "Barbeque.h";
using namespace::std;
Barbeque::Barbeque(string brand, string model)
{
my_Brand = brand;
my_Model = model;
my_hasCoals = false;
my_isCooking = false;
my_isLit = false;
}
//check is the coal is loaded
bool Barbeque::hasCoals(){
if (my_hasCoals == true)
{
return true;
}else
{
return false;
}
}
//load the coal
void Barbeque::loadCoals(){
my_hasCoals = true;
cout << "Loading " << my_Brand <<" "<< my_Model << " with coals!" << endl;
}
//light the coal if there is any
void Barbeque::light(){
if (hasCoals() == true)
{
my_isLit = true;
cout << "Lighting the grill! ";
}else
{
my_isLit = false;
cout << "You can't light this "
<< my_Brand << " "
<< my_Model
<< " until you load it with coals!" << endl;
}
}
//check if it's lit
bool Barbeque::isLit(){
if (my_isLit == true)
{
return true;
}else
{
return false;
}
}
//cooking the food if the coal is lit
void Barbeque::cook(){
if (isCooking() == true)
{
cout << "Cooking food! ";
}else
{
cout << "You can't cook on this "
<< my_Brand << " "
<< my_Model
<< " until it has coals and has been lit!" << endl;
}
}
//check to see if the grill is cooking
bool Barbeque::isCooking(){
if (my_isLit == true)
{
return true;
}else
{
return false;
}
}
//resets everything
void Barbeque::reset(){
my_hasCoals = false;
my_isCooking = false;
my_isLit = false;
}
//where the magic happens
int main(){
Barbeque bbq("Coleman", "Grill 101A" );
bbq.loadCoals(); // print output
if (bbq.hasCoals()) {
bbq.light(); // print output
if (bbq.isLit()) {
bbq.cook(); // print output
}
else {
cout << "bbq should be lit so";
cout << " this is an error in";
cout << " your logic!" << endl;
}
}
// clean off the bbq and start over
bbq.reset();
cout << "Let's make some errors..." << endl;
bbq.light(); // print error
bbq.cook(); // print error
bbq.loadCoals(); // print output
bbq.cook(); // print error
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.