Online Book Reader

Home Category

Professional C__ - Marc Gregoire [16]

By Root 1204 0
parameters. At first, that seems like a contradiction. Reference parameters allow you to change the value of a variable from within another context. const seems to prevent such changes.

The main value in const reference parameters is efficiency. When you pass a value into a function, an entire copy is made. When you pass a reference, you are really just passing a pointer to the original so the computer doesn’t need to make the copy. By passing a const reference, you get the best of both worlds — no copy is made but the original variable cannot be changed.

const references become more important when you are dealing with objects because they can be large and making copies of them can have unwanted side effects. Subtle issues like this are covered in Chapter 9.

C++ AS AN OBJECT-ORIENTED LANGUAGE


If you are a C programmer, you may have viewed the features covered so far in this chapter as convenient additions to the C language. As the name C++ implies, in many ways the language is just a “better C.” There is one major point that this view overlooks. Unlike C, C++ is an object-oriented language.

Object-oriented programming (OOP) is a very different, arguably more natural, way to write code. If you are used to procedural languages such as C or Pascal, don’t worry. Chapter 3 covers all the background information you need to know to shift your mindset to the object-oriented paradigm. If you already know the theory of OOP, the rest of this section will get you up to speed (or refresh your memory) on basic C++ object syntax.

Declaring a Class

A class defines the characteristics of an object. In C++, classes are usually declared in a header file and fully defined in a corresponding source file.

A basic class definition for an airline ticket class is shown below. The class can calculate the price of the ticket based on the number of miles in the flight and whether or not the customer is a member of the “Elite Super Rewards Program.” The definition begins by declaring the class name. Inside a set of curly braces, the data members (properties) of the class and its methods (behaviors) are declared. Each data member and method is associated with a particular access level: public, protected, or private. These labels can occur in any order and can be repeated.

#include

class AirlineTicket

{

public:

AirlineTicket();

~AirlineTicket();

int calculatePriceInDollars() const;

std::string getPassengerName() const;

void setPassengerName(std::string inName);

int getNumberOfMiles() const;

void setNumberOfMiles(int inMiles);

bool getHasEliteSuperRewardsStatus() const;

void setHasEliteSuperRewardsStatus(bool inStatus);

protected:

std::string mPassengerName;

int mNumberOfMiles;

bool bHasEliteSuperRewardsStatus;

};

Code snippet from AirlineTicket\AirlineTicket.h

The method that has the same name as the class with no return type is a constructor. It is automatically called when an object of the class is created. The method with a tilde (~) character followed by the class name is a destructor. It is automatically called when the object is destroyed.

To follow the const-correctness principle, it’s always a good idea to declare member functions that do not change any data member of the object as being const. These member functions are also called “inspectors,” compared to “mutators” for non-const member functions.

The sample program that follows makes use of the class declared in the previous example. This example shows the creation of a stack-based AirlineTicket object as well as a heap-based object.

AirlineTicket myTicket; // Stack-based AirlineTicket

myTicket.setPassengerName("Sherman T. Socketwrench");

myTicket.setNumberOfMiles(700);

int cost = myTicket.calculatePriceInDollars();

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

// heap-based AirlineTicket with smart pointer

shared_ptr myTicket2(new AirlineTicket());

myTicket2->setPassengerName("Laudimore M. Hallidue");

myTicket2->setNumberOfMiles(2000);

myTicket2->setHasEliteSuperRewardsStatus(true);

int cost2 = myTicket2->calculatePriceInDollars();

Return Main Page Previous Page Next Page

®Online Book Reader