Online Book Reader

Home Category

Professional C__ - Marc Gregoire [17]

By Root 1243 0

cout << "This other ticket will cost $" << cost2 << endl;

// No need to delete myTicket2, happens automatically

// heap-based AirlineTicket without smart pointer

AirlineTicket* myTicket3 = new AirlineTicket();

// ... Use ticket 3

delete myTicket3; // delete the heap object

Code snippet from AirlineTicket\AirlineTicketTest.cpp

The definitions of the AirlineTicket class methods are shown below.

AirlineTicket::AirlineTicket()

{

// Initialize data members

bHasEliteSuperRewardsStatus = false;

mPassengerName = "Unknown Passenger";

mNumberOfMiles = 0;

}

AirlineTicket::~AirlineTicket()

{

// Nothing much to do in terms of cleanup

}

int AirlineTicket::calculatePriceInDollars() const

{

if (getHasEliteSuperRewardsStatus()) {

// Elite Super Rewards customers fly for free!

return 0;

}

// The cost of the ticket is the number of miles times

// 0.1. Real airlines probably have a more complicated formula!

return static_cast(getNumberOfMiles() * 0.1);

}

string AirlineTicket::getPassengerName() const

{

return mPassengerName;

}

void AirlineTicket::setPassengerName(string inName)

{

mPassengerName = inName;

}

// Other get and set methods omitted for brevity.

Code snippet from AirlineTicket\AirlineTicket.cpp

The preceding example exposes you to the general syntax for creating and using classes. Of course, there is much more to learn. Chapters 6 and 7 go into more depth about the specific C++ mechanisms for defining classes.

THE STANDARD LIBRARY


C++ comes with a standard library, which contains a lot of useful classes that can easily be used in your code. The benefit of using classes from the standard library is that you don’t need to reinvent certain classes and you don’t need to waste time on implementing things that have already been implemented for you. Another benefit is that the classes available in the standard library are heavily tested and verified for correctness by thousands of users. The standard library classes are also tuned for high performance, so using them will most likely result in better performance compared to making your own implementation.

The amount of functionality available to you in the standard library is pretty big. Chapter 11 and later chapters provide more details about the standard library. When you start working with C++ it is a good idea to grasp immediately what the standard library can do for you. This is especially important if you are a C programmer. As a C programmer, you might try to solve problems in C++ the same way you would solve them in C. However, in C++ there is probably an easier and safer solution to the problem by using standard library classes.

You already saw some standard library classes earlier in this chapter; for example, the std::string and std::cout classes. Another example of functionality provided by the standard library is the concept of containers, which will be used further in this chapter. Take the std::vector, as an example. The vector replaces the concept of C arrays with a much more flexible and safer mechanism. As a user, you need not worry about memory management, as the vector will automatically allocate enough memory to hold its elements. A vector is dynamic, meaning that elements can be added and removed at run time. To make it easy to loop over the contents of containers, the standard library provides a concept called iterators. Chapter 12 goes into more details regarding containers and iterators.

The following example demonstrates the basic functionality of the std::vector class and the concept of iterators.

#include

#include

#include

using namespace std;

int main()

{

// Create a vector of strings, using C++11 uniform initialization

vector myVector = {"A first string", "A second string"};

// Add some strings to the vector using push_back

myVector.push_back("A third string");

myVector.push_back("The last string in the vector");

// Iterate over the elements in the vector and print them

for (auto iterator = myVector.cbegin();

iterator != myVector.cend(); ++iterator)

Return Main Page Previous Page Next Page

®Online Book Reader