Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Professional C++ [eng].pdf
Скачиваний:
284
Добавлен:
16.08.2013
Размер:
11.09 Mб
Скачать

Chapter 25

Many of the previous chapters referred to these concepts without providing detailed code examples. In this chapter, you will see concrete examples of these concepts with code that you can use in your programs.

This chapter concludes with an introduction to frameworks, a coding technique that greatly eases the development of large applications.

“I Can Never Remember How to . . .”

Chapter 1 compared the size of the C standard to the size of the C++ standard. It is possible, and somewhat common, for a C programmer to memorize the entire C language. The keywords are few, the language features are minimal, and the behaviors are well defined. This is not the case with C++. Even the authors of this book, who are self-proclaimed geniuses, need to look things up. With that in mind, we present the following examples of coding techniques that are used in almost all C++ programs. When you remember the concept but forget the syntax, turn to these pages for a refresher.

. . . Write a Class

Don’t remember how to get started? No problem — here is the definition of a simple class:

/**

*Simple.h

*A simple class that illustrates class definition syntax.

*/

#ifndef _simple_h_ #define _simple_h_

class Simple {

 

public:

 

Simple();

// Constructor

virtual ~Simple();

// Destructor

virtual void publicMethod();

// Public method

int mPublicInteger;

// Public data member

protected:

 

int mProtectedInteger;

// Protected data member

private:

 

int mPrivateInteger;

// Private data member

static const int mConstant = 2; // Private constant

static int sStaticInt;

// Private static data member

// Disallow assignment and pass-by-value

730

Incorporating Techniques and Frameworks

Simple(const Simple& src);

Simple& operator=(const Simple& rhs);

};

#endif

Next, here is the implementation, including the initialization of the static data member:

/**

*Simple.cpp

*Implementation of a simple class

*/

#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

}

. . . 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:

/**

*SubSimple.h

*A subclass of the Simple class

*/

#ifndef _subsimple_h_ #define _subsimple_h_

#include “Simple.h”

class SubSimple : public Simple

{

731

Chapter 25

public:

 

SubSimple();

// Constructor

virtual ~SubSimple();

// Destructor

virtual void publicMethod();

// Overridden method

virtual void anotherMethod(); // Added method

};

#endif

The implementation:

/**

*SubSimple.cpp

*Implementation of a simple subclass

*/

#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

}

. . . 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 simple refresher, which uses the built-in exception class std::runtime_error. In most large programs, you will write your own exception classes.

#include <stdexcept>

#include <iostream>

732

Incorporating Techniques and Frameworks

void throwIf(bool inShouldThrow) throw (std::runtime_error)

{

if (inShouldThrow) {

throw std::runtime_error(“Here’s my exception”);

}

}

int main(int argc, char** argv)

{

try {

throwIf(false); // doesn’t throw throwIf(true); // throws!

} catch (const std::runtime_error& e) {

std::cerr << “Caught exception: “ << e.what() << std::endl;

}

}

. . . Read from a File

Complete details for file input are included in Chapter 14. Below is a quick sample program for file reading basics. This program reads its own source code and outputs it one token at a time.

/**

* readfile.cpp */

#include <iostream> #include <fstream> #include <string>

using namespace std;

int main()

{

ifstream inFile(“readfile.cpp”);

if (inFile.fail()) {

cerr << “Unable to open file for reading.” << endl; exit(1);

}

string nextToken;

while (inFile >> nextToken) {

cout << “Token: “ << nextToken << endl;

}

inFile.close();

return 0;

}

733