Create car.cpp
In this part of the assignment, you create the car.cpp
implementation file. We aren't implementing anything yet, just creating “stubs” for each function.
A function stub is code that has the correct signature for a function but doesn't have code to do anything (yet). Stubs help us make sure that we have the skeleton of the code in place and can compile our whole program before we start implementing the functionality. (This process is sometimes called “stubbing out”.)
Don't implement any functionality in this step, even if you feel like you know what it should be!
Why?
It's good software development practice to get the skeleton laid out before writing the code!
Implementation Steps
Create the Car
Implementation File
Start by creating a file called car.cpp
, which you will fill in with stubs. Do not write any interesting code in these function bodies!
The first thing your car.cpp
file should do is include the car.hpp
file that you created in the previous step as well as the system headers for any C++ functionality you rely on, including <iostream>
, <ostream>
, <stdexcept>
, and <cstddef>
.
Each function that you declared in the header file should get a stub in the implementation file. Your stubs should not generate any warnings when you compile your code.
You'll find more detailed advice about avoiding errors with stubs in the Help pages, but the basic idea is to have the stub throw
an exception to exit a function with an error, which allows us to get away without writing a valid return
statement.
So as an example, we might create a stub function for the isFull()
function as
bool Car::isFull() {
throw std::logic_error("Not implemented (yet!)"); // FIXME: Implement it!
}
where we throw
a std::logic_error
to print a warning about our unimplemented function.
The stubs help page has lots more detail.
Write Remaining Function Stubs
Write stubs for these functions:
Car::Car()
size_t Car::getUsage()
bool Car::isEmpty()
bool Car::isFull()
void Car::addPackage()
void Car::removePackage()
void Car::printToStream(ostream& outStream)
The operator<<
Function
Okay, we lied: You should define one function,
operator<<
.But only
operator<<
!And only because it's so simple and useful, even at this early stage.
Defining operator<<
gives you a convenient way to call your printToStream
member function, allowing you to type
cout << myCar << endl;
instead of
myCar.printToStream(cout);
cout << endl;
The function is so simple (and such a common pattern in C++ code), we'll just write it right now. Notice that operator<<
is a global function, not a member function inside the Cow
class.
std::ostream& operator<<(std::ostream& os, const Car& car) {
car.printToStream(os);
// To allow chaining of <<, we always return the stream we were given.
return os;
}
Compiling Your Skeleton Car
Class
Before you try compiling, add the following temporary code to the top of car.cpp
:
// Disable warnings that happen because our stubs don't do anything yet.
#pragma GCC diagnostic ignored "-Wunused-private-field" // FIXME: Remove soon.
#pragma GCC diagnostic ignored "-Wunused-parameter" // FIXME: Remove soon.
At this point, you should be able to compile your Car
class file, even though it doesn't do anything yet:
clang++ -Wall -Wextra -std=c++17 -c car.cpp -o car.o
If your stubs are written correctly, you should receive no errors or warnings. If you do have errors or warnings, fix them before moving on.
(When logged in, completion status appears here.)