Chapter 14:
More About
Classes
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
14.1
Instance and Static Members
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Instance and Static Members
Instance variable: a member variable in a class.
Each object has its own copy.
Static variable: one variable shared among all
objects of a class. Static variables have a
lifetime of the whole program.
Static member function: can be used to
access static member variable; can be called
before any objects are defined
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
static member variable
Contents of Tree.h
1 // Tree class Static member declared here.
2 class Tree
3 {
4 private:
5 static int objectCount; // Static member variable.
6 public:
7 // Constructor
8 Tree()
9 { objectCount++; }
10
11 // Accessor function for objectCount
12 int getObjectCount() const
13 { return objectCount; } Static member initialized here.
14 };
15
16 // Initialization of the static member variable, written
17 // outside the class.
18 int Tree::objectCount = 0;
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Three Instances of the Tree Class, But Only
One objectCount Variable
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
static member function
Declared with static before return type:
static int getObjectCount()
{ return objectCount; }
Static member functions can only access static
member data
Can be called independent of objects:
int num = Tree::getObjectCount();
Or through any object:
Tree oak;
int num = oak.getObjectCount();
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Modified Version of Tree.h
1 // Tree class
2 class Tree
3 {
4 private:
5 static int objectCount; // Static member variable.
6 public:
7 // Constructor
8 Tree()
9 { objectCount++; }
10
11 // Accessor function for objectCount
12 static int getObjectCount()
13 { return objectCount; }
14 };
15
16 // Definition of the static member variable, written
17 // outside the class.
18 int Tree::objectCount = 0;
Now we can call the function like this:
cout << "There are " << Tree::getObjectCount()
<< " objects.\n";
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Exercise
• Define a class Person with private member
variables name and age. The class should have
a constructor that takes the name and age as
parameters. Define only getters for both
member variables. The class has a static
member variable that stores the minimum age
of any person object defined using this class.
Define a static member function that returns the
value of the minimum age.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Exercise/Homework
• Define a class Employee with private member
variables name and salary. Define getters and
setters for the member variables. The class
should have 2 constructors; one default
constructor and another that takes the name
and salary as parameters. The class has a
static member variable that stores the total
sum of the salaries of all employees. Two static
variables are also needed to store the highest
salary amongst all employees, and name of the
employee with that salary. Define a static
member function that returns the value of the
highest salary and the name of the employee
with the highest salary.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
14.2
Friends of Classes
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Friends of Classes
Friend: a function or class that is not a member
of a class, but has access to private members of
the class
A friend function is declared inside the class.
A friend function can be a stand-alone function
or a member function of another class
It is declared a friend of a class with friend
keyword in the function prototype
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
friend Function Declarations
Stand-alone function:
friend void setAVal(int& , int);
// declares setAVal function to be
// a friend of this class
Member function of another class:
friend void SomeClass::setNum(int num)
// setNum function from SomeClass
// setNum is a friend of this class
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
friend Function Declarations
Function as a friend of a class:
class NewClass
{
int x;
public:
NewClass(int param){x=param;}
friend void setAVal(int& Val, int);
friend void SomeClass::setNum(int num);
};
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
friend Class Declarations
Class as a friend of a class:
class OtherClass
{
...
};
class NewClass
{
public:
friend class OtherClass; // declares
// entire class OtherClass, including all of
// its member functions, is a friend
// of this class
…
};
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Friend Function
class InventoryItem
{
char* description;
int units;
double cost;
public:
InventoryItem(char* desc, int u, double c)
{
description= new char[strlen(desc)+1];
strcpy(description, desc);
units = u;
cost = c;
}
~InventoryItem(){ delete[] description;}
void displayInfo();
friend void displayInfo(InventoryItem*);
};
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Friend Function
void InventoryItem::displayInfo()
{
cout<<description<<" "<<units<<" "<<cost;
}
void displayInfo(InventoryItem* l)
{
cout<<l->description<<" "<<l->units<<" "<<l->cost;
}
int main() {
char s[10]="Wrench";
InventoryItem iv(s,10,3.5);
iv.displayInfo();
displayInfo(&iv);
return 0;
}
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
14.3
Memberwise Assignment
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Memberwise Assignment
Can use = to assign one object to another, or to
initialize an object with an object’s data
Copies member to member. e.g.,
instance2 = instance1; means:
copy all member values from instance1 and assign
to the corresponding member variables of
instance2
Use at initialization:
Rectangle r2 = r1;
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
14.4
Copy Constructors
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Copy Constructors
Special constructor used when a newly created
object is initialized to the data of another object
of same class
Default copy constructor copies field-to-field …
copies value of member variable of object1 to
the same member variable of object2
Default copy constructor works fine in many
cases
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Copy Constructors
Problem: what if object contains a pointer?
class SomeClass
{
public:
SomeClass(int val = 0)
{value=new int; *value = val;}
int getVal () const;
void setVal(int);
~SomeClass();
private:
int *value;
};
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Copy Constructors
int SomeClass::getVal() const
{
return *value;
}
void SomeClass::setVal(int val)
{
*value = val;
}
SomeClass::~SomeClass()
{ delete value;}
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Copy Constructors
What we get using memberwise copy with
objects containing dynamic memory:
SomeClass object1(5);
SomeClass object2 = object1;
object2.setVal(13);
cout << object1.getVal(); // also 13
13
object1 object2
value value
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Programmer-Defined
Copy Constructor
Allows us to solve problem with objects
containing pointers:
SomeClass::SomeClass(const SomeClass &obj)
{
value = new int;
*value = *(obj.value);
}
Copy constructor takes a constant reference
parameter to an object of the class
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Programmer-Defined
Copy Constructor
Each object now points to separate
dynamic memory:
SomeClass object1(5);
SomeClass object2 = object1;
object2.setVal(13);
cout << object1.getVal(); // still 5
5 13
object1 object2
value value
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Programmer-Defined
Copy Constructor
Since copy constructor has a reference to
the object it is copying from,
SomeClass::SomeClass(SomeClass &obj)
it can modify that object.
To prevent this from happening, make the
object parameter const:
SomeClass::SomeClass
(const SomeClass &obj)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Write the main function
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Copy Constructor Exercise
• Add a copy constructor to the following
class:
class Student
{
private:
string name;
int* grades;
int numOfSubjects;
public:
Student(string nameParam, int numOfSubs)
{
name = nameParam;
numOfSubjects = numOfSubs;
grades = new int[numOfSubjects];
}
void setStudentGrade(int g, int ind)
{
grades[ind]= g;
}
};
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Copy Constructor Exercise
• Add a copy constructor to the following
class: class
{
Father
private:
string name;
char** children;
int numOfCh;
public:
Father(string nameParam, int numOfChildren)
{
name = nameParam;
numOfCh = numOfChildren;
children = new char*[numOfCh];
for(int i=0;i<numOfCh;i++)
children[i] = new char[20];
}
void addChild(char childName[], int ind)
{
strcpy(children[ind], childName);
}
};
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.