00b Review 2 C++ Programming 2
00b Review 2 C++ Programming 2
Classes
Principles of OOP
Separate compilation
Constructor
Destructor
3
POINTERS (CONT.)
Pointers are “typed”
You can store a pointer in a variable, but
a pointer is not a type (int, double, etc.)
A pointer points to an int, double, etc.
int *p;
6
THE & OPERATOR
The “ address of ” operator &
Also used to specify call-by-reference parameter
Operator &
Determines “ address of ” variable
Read like:
"p1 equals address of v1" OR
"p1 points to v1"
7
POINTING TO… (CONT.)
Given the following:
p1 = &v1;
8
POINTER ASSIGNMENTS
Pointer variables can be "assigned"
p1 = p2;
10
ANOTHER EXAMPLE
cout << v;
cout << p1;
cout << p2; What is the output?
cout << *p1;
cout << *p2;
cout << &p1;
11
ANOTHER EXAMPLE
12
ANOTHER EXAMPLE
13
ANOTHER EXAMPLE
14
ANOTHER EXAMPLE
15
ANOTHER EXAMPLE
16
ANOTHER EXAMPLE
17
POINTERS AND FUNCTIONS
Pointers are full-fledged types
Can be used just like other types
Can be function parameters
Can be returned from functions
18
THE new OPERATOR
Since pointers can refer to variables…
No “real” need to have a standard identifier
Can dynamically allocate variables
Operator new creates variables
No identifiers to refer to them
Just a pointer!
int *p1;
p1 = new int;
20
THE new OPERATOR (CONT.)
Now, the question is:
Why would you declare a dynamic variable?
21
MEMORY MANAGEMENT
The heap
Also called “freestore”
Reserved for dynamically-allocated variables
22
MEMORY MANAGEMENT (CONT.)
Because you have control of the heap
1. You need to reclaim the memory not used anymore
AND
2. You need to make sure the pointer does not point to
that section of memory any longer
23
DE-ALLOCATING MEMORY
Always de-allocate dynamic memory
When no longer needed
To restore memory in the heap
// other code...
delete p;
p = nullptr;
24
DE-ALLOCATING MEMORY
Always de-allocate dynamic memory
When no longer needed
To restore memory in the heap
delete p;
p = nullptr;
26
DYNAMIC ARRAYS
Limitations of static arrays
Must specify capacity first can be a waste of memory
May not know until program runs
Dynamic arrays
Capacity not specified at programming time
Determined while program is running
Use new operator
27
DELETING DYNAMIC ARRAYS
Always de-allocate dynamic arrays
When no longer needed
To restore memory in the heap
// other code...
Principles of OOP
Information Hiding
Details of how operations work not known to “user” of class
Data Abstraction
Details of how data is manipulated within
Abstract Data Type (ADT) and class not known to “user”
Encapsulation
Bring together data and operations 31
SEPARATE COMPILATION
With separate interface and compilation:
“User” of class does not need to see details of how class
is implemented
“User” only needs rules (interface) for the class
In C++ public member functions and
associated comments
Implementation (compilation) of class hidden
Member function definitions elsewhere
File: cpp_separate_compilation.pdf
32
CONSTRUCTORS
Key principle of OOP
Initialize objects
Initialize some or all member variables
Other actions possible as well
Validate data to ensure appropriate data is assigned to class
private member variables
Must be in public section of the class
Can overload constructors just like other functions
Default constructor
Constructor w/ no arguments
Should always be defined
33
THE const MODIFIER ON FUNCTIONS
When to make a member function const?
When the function does not modify any member variables
class MyClass
{
public: Function print does not modify
... the member variable myInt
void print( ) const; Make the function const
...
private:
int myInt;
void MyClass::print( ) const
};
{
cout << myInt << endl;
}
34
THE const MODIFIER ON FUNCTIONS (CONT.)
Trying to make a function const when the function modifies
any of the member variables, will result in an error
class MyClass
{
public: Function add will modify the
... member variable myInt
void add(int value); Cannot make the function const
...
private:
int myInt;
};
void MyClass::add(int value)
{
myInt += value; 35
}
DESTRUCTOR
Destructor
Automatically called when object is out of scope
Default version removes only ordinary variables,
not dynamic variables
If pointers are private member data
Then you are creating dynamic variables (new)
Need to de-allocate dynamic data (delete)
Need to NULL pointer
This last step is not really necessary, because the pointer is
an ordinary variable and will be destroyed automatically,
BUT it is good practice
36
CLASSES - EXAMPLE
File: Cpp_separate_compilation
Project: Employee Class
37
THE STL string CLASS
38
THE STL string CLASS
Defined in library
#include <string>
using namespace std;
40
END REVIEW 2
41 (no exercise)