Professional C__ - Marc Gregoire [474]
Next, here is the implementation, including the initialization of the static data member:
#include "Simple.h"
int Simple::sStaticInt = 0; // Initialize static data member.
Simple::Simple()
{
// Implementation of constructor
}
Simple::~Simple()
{
// Implementation of destructor
}
void Simple::publicMethod()
{
// Implementation of public method
}
Code snippet from Simple\Simple.cpp
Note that with C++11, you can initialize sStaticInt within the class definition as explained in Chapter 6.
Chapters 6 and 7 provide all the details for writing your own classes.
... Subclass an Existing Class
To subclass, you declare a new class that is a public extension of another class. Here is the definition for a sample subclass called SubSimple:
#ifndef _subsimple_h_
#define _subsimple_h_
#include "Simple.h"
// A subclass of the Simple class
class SubSimple : public Simple
{
public:
SubSimple(); // Constructor
virtual ~SubSimple(); // Destructor
virtual void publicMethod(); // Overridden method
virtual void anotherMethod(); // Added method
};
#endif
Code snippet from Simple\SubSimple.h
The implementation:
#include "SubSimple.h"
SubSimple::SubSimple() : Simple()
{
// Implementation of constructor
}
SubSimple::~SubSimple()
{
// Implementation of destructor
}
void SubSimple::publicMethod()
{
// Implementation of overridden method
}
void SubSimple::anotherMethod()
{
// Implementation of added method
}
Code snippet from Simple\SubSimple.cpp
Consult Chapter 8 for details on inheritance techniques.
... Throw and Catch Exceptions
If you’ve been working on a team that doesn’t use exceptions (for shame!) or if you’ve gotten used to Java-style exceptions, the C++ syntax may escape you. Here’s a refresher, which uses the built-in exception class std::runtime_error. In most large programs, you will write your own exception classes.
#include #include void throwIf(bool inShouldThrow) throw (std::runtime_error) { if (inShouldThrow) { throw std::runtime_error("Here's my exception"); } } int main() { try { throwIf(false); // doesn't throw throwIf(true); // throws! } catch (const std::runtime_error& e) { std::cerr << "Caught exception: " << e.what() << std::endl; } return 0; } Code snippet from Exceptions\Exceptions.cpp Chapter 10 discusses exceptions in more detail. ... Read from a File Complete details for file input are included in Chapter 15. Here is a quick sample program for file reading basics. This program reads its own source code and outputs it one token at a time. #include #include #include using namespace std; int main() { ifstream inFile("FileRead.cpp"); if (inFile.fail()) { cerr << "Unable to open file for reading." << endl; return 1; } string nextToken; while (inFile >> nextToken) { cout << "Token: " << nextToken << endl; } inFile.close(); return 0; } Code snippet from FileRead\FileRead.cpp ... Write to a File The following program outputs a message to a file, then reopens the file and appends another message. Additional details can be found in Chapter 15. #include #include using namespace std; int main() { ofstream outFile("FileWrite.out"); if (outFile.fail()) { cerr << "Unable to open file for writing." << endl; return 1; } outFile << "Hello!" << endl; outFile.close(); ofstream appendFile("FileWrite.out", ios_base::app); if (appendFile.fail()) { cerr << "Unable to open file for appending." << endl; return 2; } appendFile << "Append!" << endl; appendFile.close(); return 0; } Code snippet from FileWrite\FileWrite.cpp ... Write a Template Class Template syntax is one of the messiest parts of the C++ language.