Im working in C++. I have to build a header file with two classes. I\'ve include
ID: 3852090 • Letter: I
Question
Im working in C++. I have to build a header file with two classes. I've included the instructions below. Also I need the execution code to match the picture. There must also be an implementation file to run the code called from the two classes. I've included the driver file. The class for the roster must be a linked list that orders the members in level order from highest to lowest with the use of insertion sort.
// Instructions for Assignment
Your program will create and manage a Guild Roster. It will require the following
classes, both of which should be declared in guild.h
Character: The Character class is a simple class for storing a name and level for
characters. It should have a means of retrieving these values after creation, but neither
should be mutable.
Roster: The roster class stores Characters in level order—the first is the highest level,
the second second highest and so forth. It does this via insertion sort: The order is
updated whenever a new character is added. A Roster is initially created with no
members, just a name. It should, in addition to adding members, have the ability to print
out its roster via a function print. This class must use a linked list to store the
Characters.
You may need or use other classes. You must implement your own linked list class, not
use C++’s standard std::list.
// Driver file
// driver.cpp
// Testing driver for Assignment 3
#include
#include
#include "guild.h"
using std::cin;
using std::getline;
using std::cout;
using std::endl;
using std::string;
int main()
{
string name;
cout << "What is your guild's name? " << endl;
getline(cin, name);
Roster guild = Roster(name);
string prompt;
do
{
string charName;
cout << "What is the character's name? ";
getline(cin, charName);
int level;
cout << "What is the character's level? ";
cin >> level;
guild.add(Character(charName, level));
cout << "Are there more members (Y/N)? " << endl;
cin.ignore(std::numeric_limits::max(), ' '); // Flush newlines
getline(cin, prompt);
}
while(prompt[0] == 'y' || prompt[1] == 'Y');
guild.print();
}
// Sample Execution
Document anguage: English (U.5.) Change What is your guild's name? Savage Tide What is the character's name? Col Tobinson What is the character's level? 21 Are there more members (Y/N)? y What is the character's name? Lyddie What is the character's level? 20 Are there more members (Y/N)? y What is the character's name? Bin What is the character's level? 20 Are there more members (Y/N)? y What is the character's name? Churtle What is the character's level? 18 Are there more members (Y/N)? y What is the character's name? Roundabout What is the character's level? 19 Are there more members (Y/N)? n Guild Name: Savage Tide Convert Create PDF Comment Combine Files organize Pages aill & Sign Send for Signature Send & Track More Tools Col Tobinson, Level 21 Bin, Level 20 le, Leve Roundabout, Level 19 Churtle, Level 18 Store and share files in the Document Cloud Lean More Type here to search 657 AM 6/26/2017 q:Explanation / Answer
My personal style with C++ has always to put class declarations in an include file, and definitions in a .cpp file, very much like stipulated in Loki's answer to C++ Header Files, Code Separation. Admittedly, part of the reason I like this style probably has to do with all the years I spent coding Modula-2 and Ada, both of which have a similar scheme with specification files and body files.
I have a coworker, much more knowledgeable in C++ than I, who is insisting that all C++ declarations should, where possible, include the definitions right there in the header file. He's not saying this is a valid alternate style, or even a slightly better style, but rather this is the new universally-accepted style that everyone is now using for C++.
I'm not as limber as I used to be, so I'm not really anxious to scrabble up onto this bandwagon of his until I see a few more people up there with him. So how common is this idiom really?
Just to give some structure to the answers: Is it now The Way, very common, somewhat common, uncommon, or bug-out crazy?
Your coworker is wrong, the common way is and always has been to put code in .cpp files (or whatever extension you like) and declarations in headers.
There is occasionally some merit to putting code in the header, this can allow more clever inlining by the compiler. But at the same time, it can destroy your compile times since all code has to be processed every time it is included by the compiler.
Finally, it is often annoying to have circular object relationships (sometimes desired) when all the code is the headers.
Bottom line, you were right, he is wrong.
EDIT: I have been thinking about your question. There is one case where what he says is true. templates. Many newer "modern" libraries such as boost make heavy use of templates and often are "header only." However, this should only be done when dealing with templates as it is the only way to do it when dealing with them.
EDIT: Some people would like a little more clarification, here's some thoughts on the downsides to writing "header only" code:
If you search around, you will see quite a lot of people trying to find a way to reduce compile times when dealing with boost. For example: How to reduce compilation times with Boost Asio, which is seeing a 14s compile of a single 1K file with boost included. 14s may not seem to be "exploding", but it is certainly a lot longer than typical and can add up quite quickly. When dealing with a large project. Header only libraries do affect compile times in a quite measurable way. We just tolerate it because boost is so useful.
Additionally, there are many things which cannot be done in headers only (even boost has libraries you need to link to for certain parts such as threads, filesystem, etc). A Primary example is that you cannot have simple global objects in header only libs (unless you resort to the abomination that is a singleton) as you will run into multiple definition errors. NOTE: C++17's inline variables will make this particular example doable in the future.
As a final point, when using boost as an example of header only code, a huge detail often gets missed.
Boost is library, not user level code. so it doesn't change that often. In user code, if you put everything in headers, every little change will cause you to have to recompile the entire project. That's a monumental waste of time (and is not the case for libraries that don't change from compile to compile). When you split things between header/source and better yet, use forward declarations to reduce includes, you can save hours of recompiling when added up across a day.