Online Book Reader

Home Category

Professional C__ - Marc Gregoire [75]

By Root 1160 0

That line is confusing. Which value do you mean: the value that was passed as a parameter, or the value that is a member of the object?

The preceding ambiguous line will compile without any warnings or errors, but it will not produce the results that you are expecting.

In order to disambiguate the names you can use the this pointer:

void SpreadsheetCell::setValue(double value)

{

this->value = value;

mString = doubleToString(this->value);

}

Code snippet from SpreadsheetCellThis\unambiguous\SpreadsheetCell.cpp

However, if you use the naming conventions described in Chapter 5, you will never encounter this type of name collision.

You can also use the this pointer to call a function or method that takes a pointer to an object from within a method of that object. For example, suppose you write a printCell() stand-alone function (not method) like this:

void printCell(SpreadsheetCell* inCellp)

{

cout << inCellp->getString() << endl;

}

Code snippet from SpreadsheetCellThis\unambiguous\SpreadsheetCell.cpp

If you want to call printCell() from the setValue() method, you must pass this as the argument to give printCell() a pointer to the SpreadsheetCell on which setValue() operates:

void SpreadsheetCell::setValue(double value)

{

this->value = value;

mString = doubleToString(this->value);

printCell(this);

}

Code snippet from SpreadsheetCellThis\unambiguous\SpreadsheetCell.cpp

Instead of writing a printCell() function, it would be more convenient to overload the << operator, explained in Chapter 18. You can then use the following line to print a SpreadsheetCell:

cout << *this << endl;

Using Objects

The previous class definition says that a SpreadsheetCell consists of two member variables, four public methods, and two protected methods. However, the class definition does not actually create any SpreadsheetCells; it just specifies their shape and behavior. In that sense, a class is similar to architectural blueprints. The blueprints specify what a house should look like, but drawing the blueprints doesn’t build any houses. Houses must be constructed later based on the blueprints.

Similarly, in C++ you can construct a SpreadsheetCell “object” from the SpreadsheetCell class definition by declaring a variable of type SpreadsheetCell. Just as a builder can build more than one house based on a given set of blueprints, a programmer can create more than one SpreadsheetCell object from a SpreadsheetCell class. There are two ways to create and use objects: on the stack and on the heap.

Objects on the Stack

Here is some code that creates and uses SpreadsheetCell objects on the stack.

SpreadsheetCell myCell, anotherCell;

myCell.setValue(6);

anotherCell.setString("3.2");

cout << "cell 1: " << myCell.getValue() << endl;

cout << "cell 2: " << anotherCell.getValue() << endl;

Code snippet from SpreadsheetCellNumText\SpreadsheetCellTest.cpp

You create objects just as you declare simple variables, except that the variable type is the class name. The . in lines like myCell.setValue(6); is called the “dot” operator; it allows you to call methods on the object. If there were any public data members in the object, you could access them with the dot operator as well.

The output of the program is:

cell 1: 6

cell 2: 3.2

Objects on the Heap

You can also dynamically allocate objects by using new:

SpreadsheetCell* myCellp = new SpreadsheetCell();

myCellp->setValue(3.7);

cout << "cell 1: " << myCellp->getValue() <<

" " << myCellp->getString() << endl;

delete myCellp;

myCellp = nullptr;

Code snippet from SpreadsheetCellNumText\SpreadsheetCellHeap.cpp

When you create an object on the heap, you call its methods and access its members through the “arrow” operator:->. The arrow combines dereferencing (*) and method or member access (.). You could use those two operators instead, but doing so would be stylistically awkward:

SpreadsheetCell* myCellp = new SpreadsheetCell();

(*myCellp).setValue(3.7);

cout << "cell 1: " << (*myCellp).getValue() <<

" " << (*myCellp).getString() << endl;

delete myCellp;

Return Main Page Previous Page Next Page

®Online Book Reader