Chapter 20: Name Scope Management


Chapter Goals



20.1 Encapsulation


20.1 Encapsulation (cont.)


20.2 Name Scopes

Scope
The part of a program where a name is visible and has meaning (sect. 5.9)

Scopes we have seen:


20.2 Name Scopes (cont.)

Scopes we have seen (cont.):


20.2 Name Scopes (cont.)

Scopes may nest

int count = 0; // Create a global variable
. . .
int count_items()
{
   int orig_count = count; // holds value in global variable
   int count = 0; // A different variable named count
   for (...)
      count++; // Increment the local variable
   return count + orig_count;
}
// Local count and orig_count are no longer available,

20.2.1 Shadows and Qualification


20.2.1 Shadows and Qualification

To access (some) hidden names:


20.2.1 Example

Perverse example:

// DONT WRITE CODE SUCH AS THIS!
double balance; // balance has global scope
class BankAccount
{
public:
   BankAccount(double balance);
      // Parameter variable balance has local scope
private:
   double balance; // Data field balance has class scope
};

20.2.1 Example (cont.)

BankAccount::BankAccount(double balance)
{
   this->balance = balance;
      // Assign data field balance using parameter value
   ::balance = balance;
      // Assign global variable using parameter value
}

Advanced Topic 20.1


Overriding, Shadowing, and Scopes

class Employee
{  . . .
   virtual void set_salary(int new_salary);
};
class Manager : public Employee
{  . . .
   virtual void set_salary(int new_salary, int yearly_bonus);
};
Manager m;
m.set_salary(45000); // Error - requires 2 args

Advanced Topic 20.1 (cont.)


class Manager : public Employee
{
   . . .
   virtual void set_salary(int new_salary);
   void set_salary(int new_salary, int yearly_bonus);
};

void Manager::set_salary(int new_salary)
{
   Employee::set_salary(new_salary); // Invoke the base class function
}

20.3 Protected Scope


20.3 Protected Scope (cont.)

class Chart
{
public:
   virtual void draw() const;
   . . .
protected:
   vector data;
};

class PieChart : public Chart
{
public:
   virtual void draw() const;
   . . .
};

20.3 Protected Scope (cont.)

Syntax 20.1: Protected Members
class ClassName
{
	. . .
protected:
	member functions and data fields
};
Purpose: Declare member functions and data fields that are visible to derived classes.

20.3 Protected Scope (cont.)

Syntax 20.1 (cont.)
Example:
class Chart
{
	. . .
protected:
	vector<double> data;
	double value_at(int index) const;
};

20.4 Friends


20.4 Friends (cont.)


20.4 Friends (cont.)


20.4 Friend - syntax

Syntax 20.2: Friends
class ClassName
{
	. . .
	friend class ClassName;
	friend return_type function_name(parameter list);
}
Purpose: Allow other classes and functions access to private features of a class.

20.4 Friend - syntax (cont.)

Example:
class Node
{
public:
   Node(string s);
private:
   string data;
   Node* previous;
   Node* next;
friend class List;
friend class Iterator;
};
class Employee
{
   . . .
   friend ostream& operator<<(ostream& out,
      const Employee& e)
};

20.4 Friends & Streams

Example: stream operators:

class Employee
{
public:
   Employee(string employee_name, double initial_salary);
   . . .
private:
   string name;
   double salary;
   friend ostream& operator<<(ostream& out, const Employee& e);
};

20.4 Friends & Streams (cont.)

ostream& operator<<(ostream& out, const Employee& e)
{
   out << "Employee: " << e.name;
   return out;
}

20.5 Nested Classes


20.5 Nested Classes (cont.)


20.5 Nested Classes - syntax

Syntax 20.3: Nested Class Declaration
class OuterClassName
{
   . . .
   class NestedClassName;
   . . .
};

20.5 Nested Classes - syntax (cont.)

Syntax 20.3: Nested Class Declaration (cont.)
Example:
class List
{
   . . .
   class Iterator;
};
Purpose: Declare a class whose scope is contained in the scope of another class

20.5 Nested Classes (cont.)

To nest Iterator (chptr. 16) inside List:


20.5 Nested Classes (cont.)


20.5 Nested Classes (cont.)

Or, include the entire definition inside the outer class:

class List
{
   . . .
   class Iterator
   {
   public:
      Iterator();
      string get() const;
      . . .
   };
   . . .
};

20.5 Nested Classes (cont.)


20.5 private Nested Class - Example

From chptr. 18:


20.5 private Nested Class - Example (cont.)

class SharedString
{
   . . .
private:
   class StringReference;
   . . .
};

class SharedString::StringReference
{
public:
   int count;
   char* buffer;
   StringReference(const char* right);
   ~StringReference();
};

Quality Tip 20.4


Manage Encapsulation


20.6 Private Inheritance and Names


20.6 Private Inheritance and Names - syntax

Syntax 20.4: Private Inheritance
class DerivedClassName : private BaseClassName
{
   features;
}
Purpose: To allow the derived class access to the functionality of the base class, without declaring the derived class as a specialized form of the base class.

20.6 Private Inheritance and Names - syntax (cont.)

Syntax 20.4: Private Inheritance (cont.)
Example:
class Set : private List
{
public:
   void add(string s);
   Iterator erase(Iterator pos);
   Iterator begin();
   Iterator end();
};

20.6 Private Inheritance - example

class Set : private List
{
public:
   void add(string s);
   Iterator erase(Iterator pos);
   Iterator begin();
   Iterator end();
};

20.6 Private Inheritance - example (cont.)

void Set::add(string s)
{
   Iterator iter = begin();
   Iterator stop = end();
   while (iter != stop)
   {
      if (s.equals(iter.get()))
         return; // Already in set, dont add
   }
   push_back(s); // Can use inherited push_back method
}

20.6 Private Inheritance - example (cont.)

Iterator Set::erase(Iterator pos)
{
   List::erase(pos);
}

Iterator Set::begin()
{
   return List::begin();
}

Iterator Set::end()
{
   return List::end();
}

20.6 Private Inheritance - example (cont.)


20.7 Name Spaces


20.7 Name Spaces - syntax

Syntax 20.5: Name Space Definition
namespace name_space_name
{
   feature1
   feature2
   . . .
   featuren
}

20.7 Name Spaces - syntax (cont.)

Syntax 20.5: Name Space Definition (cont.)
Example:
namespace ACME_Software_San_Jose_CA_US
{
   class map
   {
      . . .
   };
}
Purpose: Include a class, function, or variable in a name space.

20.7 Name Spaces (cont.)


20.7 Name Spaces (cont.)


20.7 Name Spaces - the using directive


20.7 Name Spaces Alias

Syntax 20.6: Name Space Alias
namespace alias_name = name_space_name;
Example:
namespace acme = ACME_Software_San_Jose_CA_US;
Purpose: Introduce a short alias for the long name of a name space

20.8 Case Study: Matrices, Continued


20.8 Case Study: Matrices (matrix4.h)


20.8 Case Study: Matrices (matrix4.cpp)


20.8 Case Study: Matrices (matrixtest4.cpp)


Chapter Summary


  1. Encapsulation helps reduce the number of visible names
  2. Scope refers to the portions of a program where a name is visible
  3. A variable that hides another variable of the same name in a different scope is said to shadow the hidden name
  4. protected members are accessible to derived classes
  5. A friend is a function or another class that is granted access to all features of a class
  6. A nested class is defined inside another class to limit the nested class' names to the outer class' scope