class Product
{
public:
Product();
void read();
bool is_better_than(Product b) const;
void print() const;
private:
string name;
double price;
int score;
};
Scopes we have seen:
Scopes we have seen (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,
To access (some) hidden names:
int TravelClock::get_hours() const
{
int h = Clock::get_hours();
// Qualify which get_hours is intended
. . .
}
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
};
BankAccount::BankAccount(double balance)
{
this->balance = balance;
// Assign data field balance using parameter value
::balance = balance;
// Assign global variable using parameter value
}
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
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
}
class Chart
{
public:
virtual void draw() const;
. . .
protected:
vector data;
};
class PieChart : public Chart
{
public:
virtual void draw() const;
. . .
};
Syntax 20.1:
Protected Members
class ClassName
{
. . .
protected:
member functions and data fields
};
|
Syntax 20.1 (cont.)
|
class Node
{
public:
Node(string s);
private:
string data;
Node* previous;
Node* next;
friend class List;
friend class Iterator;
};
Syntax 20.2:
Friends
class ClassName
{
. . .
friend class ClassName;
friend return_type function_name(parameter list);
}
|
|
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);
};
ostream& operator<<(ostream& out, const Employee& e)
{
out << "Employee: " << e.name;
return out;
}
Iterator pos = staff.begin();
list<string>::iterator pos = staff.begin();
vector<double>::iterator p = a.begin();
list<string>::iterator q = b.begin();
Syntax 20.3:
Nested Class Declaration
class OuterClassName
{
. . .
class NestedClassName;
. . .
};
|
Syntax 20.3:
Nested Class Declaration (cont.)
|
To nest Iterator (chptr. 16) inside List:
List::Iterator pos = staff.begin();
class List
{
. . .
class Iterator; // Forward reference
. . .
};
class List::Iterator
{
public:
Iterator();
string get() const;
. . .
};
List::Iterator::Iterator()
{ . . . }
string List::Iterator::get() const
{ . . . }
Or, include the entire definition inside the outer class:
class List
{
. . .
class Iterator
{
public:
Iterator();
string get() const;
. . .
};
. . .
};
From chptr. 18:
class SharedString
{
. . .
private:
class StringReference;
. . .
};
class SharedString::StringReference
{
public:
int count;
char* buffer;
StringReference(const char* right);
~StringReference();
};
Manage Encapsulation
Syntax 20.4:
Private Inheritance
class DerivedClassName : private BaseClassName
{
features;
}
|
Syntax 20.4:
Private Inheritance (cont.)
|
class Set : private List
{
public:
void add(string s);
Iterator erase(Iterator pos);
Iterator begin();
Iterator end();
};
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
}
Iterator Set::erase(Iterator pos)
{
List::erase(pos);
}
Iterator Set::begin()
{
return List::begin();
}
Iterator Set::end()
{
return List::end();
}
Set a_set;
a_set.add("Sally"); // legal
a_set.push_back("Fred");
// Error--push_back not part of interface for Set
Syntax 20.5:
Name Space Definition
namespace name_space_name
{
feature1
feature2
. . .
featuren
}
|
Syntax 20.5:
Name Space Definition (cont.)
|
namespace acme
{
class map
{
. . .
};
void draw(map m);
}
namespace acme
{
class maze
{
. . .
};
}
using namespace std;
using std::cout; // Include only cout from the std namespace
Syntax 20.6:
Name Space Alias
namespace alias_name = name_space_name;
|