Online Book Reader

Home Category

Professional C__ - Marc Gregoire [73]

By Root 1525 0
before the first access specifier have the private access specification. For example, moving the public access specifier below the setValue() method declaration gives the setValue() method private access instead of public:

class SpreadsheetCell

{

void setValue(double inValue); // now has private access

public:

double getValue() const;

protected:

double mValue;

};

Code snippet from SpreadsheetCellNumOnly\AccessControl\SpreadsheetCell.h

In C++, a struct and a union can have methods just like a class. In fact, the only difference is that the default access specifier for a struct and union is public while the default for a class is private. For example, the SpreadsheetCell class can be rewritten using a struct as follows:

struct SpreadsheetCell

{

void setValue(double inValue);

double getValue() const;

protected:

double mValue;

};

The following table summarizes the meanings of the three access specifiers:

ACCESS SPECIFICATION MEANING WHEN TO USE

public Any code can call a public method or access a public member of an object. Behaviors (methods) that you want clients to use.

Access methods for private and protected data members.

protected Any method of the class can call a protected method and access a protected member.

Methods of a subclass (see Chapter 8) can call a protected method or access a protected member of a superclass. “Helper” methods that you do not want clients to use.

Most, or better still, all, data members.

private Only methods of the class can call a private method and access a private member.

Methods in subclasses cannot access private methods or members in a superclass. Only if you want to restrict access from subclasses.

Order of Declarations

You can declare your methods, members, and access control specifiers in any order: C++ does not impose any restrictions, such as methods before members or public before private. Additionally, you can repeat access specifiers. For example, the SpreadsheetCell definition could look like the following:

class SpreadsheetCell

{

public:

void setValue(double inValue);

protected:

double mValue;

public:

double getValue() const;

};

Code snippet from SpreadsheetCellNumOnly\DeclarationOrder\SpreadsheetCell.h

However, for clarity it is a good idea to group public, protected, and private declarations, and to group methods and members within those declarations.

Defining Methods

The preceding definition for the SpreadsheetCell class is enough for you to create objects of the class. However, if you try to call the setValue() or getValue() methods, your linker will complain that those methods are not defined. That’s because the class definition specifies the prototypes for the methods, but does not define their implementations. Just as you write both a prototype and a definition for a stand-alone function, you must write a prototype and a definition for a method. Note that the class definition must precede the method definitions. Usually the class definition goes in a header file, and the method definitions go in a source file that includes that header. Here are the definitions for the two methods of the SpreadsheetCell class:

#include "SpreadsheetCell.h"

void SpreadsheetCell::setValue(double inValue)

{

mValue = inValue;

}

double SpreadsheetCell::getValue() const

{

return mValue;

}

Code snippet from SpreadsheetCellNumOnly\SpreadsheetCell.cpp

Note that the name of the class followed by two colons precedes each method name:

void SpreadsheetCell::setValue(double inValue)

The :: is called the scope resolution operator. In this context, the syntax tells the compiler that the coming definition of the setValue() method is part of the SpreadsheetCell class. Note also that you do not repeat the access specification when you define the method.

If you are using the Microsoft Visual C++ IDE, you will notice that all cpp files start with:

#include "stdafx.h"

In a VC++ project, every file should start with this line and your own includes must follow this. If you place your own includes before the stdafx.h include, they will appear

Return Main Page Previous Page Next Page

®Online Book Reader