2. Who’s Mike?
• BS in CS from
• “Petrochemical Research Organization”
• Java Queue News Editor, InfoQ
• Ai-Logix, Inc. (now AudioCodes)
• Amateur Computer Group of New Jersey
2
5. What are Overloaded
Operators?
• Define basic operations for objects of user-
defined types
• as if they were built-in types
• Often referred to as “syntactic sugar”
5
7. 7
// main application
#include “String.h”
// create two different objects of type String
String string1 = “Hello, C++ Users Group!”;
String string2 = “Hello, Java Users Group!”;
// a conditional *without* an overloaded equality operator
if(strcmp(string1.getString(),string2.getString()) == 0)
// do this
else
// do that
// a conditional *with* an overloaded equality operator
if(string1 == string2)
// do this
else
// do that
9. Overloaded Operators
(2)
•string1 == string2
• interpreted as
string1.operator==(string2);
• string1 is the object in control
• string2 is the argument passed into the
operator
9
11. Overloaded Operators
(3)
• Attractive, but can be dangerous!!
• deep vs. shallow copy
• assignment operator side effects
• The compiler automatically generates an
assignment operator if one is not explicitly
defined
• memberwise assignments
11
19. Overloaded Operators
(9)
• Limitations:
• the meaning of an operator cannot be changed
• the number of operands for an operator cannot
be changed
• operator precedence and associativity cannot be
changed
• no personal operators!!
19
23. 23
// swap member functions to handle different data types
void swap(int &first,int &second) {
int temp = second;
second = first;
first = temp;
}
void swap(float &first,float &second) {
float temp = second;
second = first;
first = temp;
}
void swap(char *&first,char *&second) {
char *temp = second;
second = first;
first = temp;
}
24. 24
/*
* function template to swap two elements
* of any data type
*/
template <class T>
void swap(T &first,T &second) {
T temp = second;
second = first;
first = temp;
}
25. Templates (2)
• A template specialization is the specific use
of a template member function or class
• swap<int>(1,2);
• swap<float>(1.7,3.5);
• swap<char>(‘a’,’b’);
• swap<char *>(“Mets”,”Jets”);
25
27. What is Exception
Handling?
• A more robust method for handling errors
than fastidiously checking for error codes
• error code checking is tedious and can obscure
program logic
27
28. Exception Handling (1)
• Throw Expression:
• raises the exception
• Try Block:
• contains a throw expression or a member
function that throws an exception
28
29. Exception Handling (2)
• Catch Clause(s):
• handles the exception
• defined immediately after the try block
• multiple catch clauses allowed
• no implicit data type conversions
• catch(...) catches any exception type
29
30. C++ Exception Model
• Destructors invoked for all live objects as
the stack “unwinds”
• Exception Specification
• specify what type of exception(s) a member
function will throw
• Termination vs. Resumption semantics
30
31. 31
// exception handling example
#include <string>
#include <stdexcept>
void fred(void) {
FILE *file = fopen(“filename.txt”,”rt”);
try {
if(file == NULL) { // file could not be opened
throw 1;
}
int g = george(-1);
}
catch(int e) {
cout << “ERROR: Could not open file...” << endl;
}
catch(string const message) {
cout << message << endl;
}
// other statements...
}
// continued on next slide...
32. 32
// continued from previous slide...
int george(int n) {
if(n < 0) {
string message = “ERROR: Value less than zero...”;
throw message;
}
// other statements...
return n;
}
33. C++ Exception Class
Hierarchy (1)
• exception
• logic_error (client program errors)
• runtime_error (external errors)
• bad_alloc (memory allocation errors)
33
35. 35
// exception handling example (revised)
#include <string>
#include <stdexcept>
void fred(void) {
FILE *file = fopen(“filename.txt”,”rt”);
try {
if(file == NULL) { // file could not be opened
throw runtime_error(“ERROR: Could not open file...”);
}
int g = george(-1);
}
catch(runtime_error &re) {
cout << re.what() << endl;
}
catch(string const message) {
cout << message << endl;
}
// other statements...
}
36. Exception Handling (3)
• Do not throw exceptions:
• to indicate special return values
• in copy constructors and assignment operators
• stroustrup.com/3rd_safe.pdf
36
38. What are Namespaces?
• Used to prevent global naming conflicts
• All C++ standard library components are
contained within a single namespace called
std
38
39. 39
// an example of using header files from different sources
// baseball.h
...
int strike = 0;
...
// bowling.h
...
bool strike = false;
...
// main application
#include baseball.h
#include bowling.h // ERROR: strike already declared
40. 40
// an example of using header files from different sources
// baseball.h
namespace baseball {
...
int strike = 0;
...
}
// bowling.h
namespace bowling {
...
bool strike = false;
...
}
// main application
#include baseball.h
#include bowling.h // OK!
41. Namespaces (1)
• Fully-qualified member names:
• namespace name
• scope resolution operator (::)
• member name
• baseball::strike
• bowling::strike
41
42. Aliases
• Provides shorthand for the fully-qualified
namespace name
• Has the form:
• namespace m = N;
• namespace bb = baseball;
• namespace bw = bowling;
42
43. Using Directive
• Provides access to all members of a
namespace without having to write the
fully-qualified namespace member names
• Has the form:
• using namespace N;
• using namespace baseball;
• using namespace bowling;
43
44. Using Declaration
• Provides access to individual members of a
namespace without having to write the
fully-qualified namespace member names
• Has the form:
• using N::m;
• using baseball::strike;
• using bowling::strike;
44
46. What is the STL?
• A subset of Standard C++
• First developed by HP Labs in 1994
• Three main parts:
• containers
• iterators
• algorithms
46
47. What are Containers?
• A data structure that contains a sequence
of elements
• Sequential containers:
• organize elements linearly
• Sorted associative containers:
• organize elements based on a key
47
48. Containers (1)
• Primarily chosen by how well it can
perform certain operations, such as:
• add elements to the container
• remove elements from the container
• rearrange elements within the container
• inspect elements within the container
48
50. What are Iterators?
• A generalization of a C/C++ pointer
• Used to access elements within an ordered
sequence
• Considered the “glue” that tie together
containers and algorithms
50
55. Popular C++
Compilers
55
• Embarcadero C++ Builder XE7
• embarcadero.com/products/cbuilder
• MicrosoftVisual C++
• microsoft.com
• Intel System Studio
• software.intel.com/en-us/c-
compilers
57. Local C++ User
Groups
• ACGNJ C++ Users Group
• facilitated by Bruce Arnold
• acgnj.barnold.us
57
58. Further Reading (1)
58
• C & C++ Code Capsules
• Chuck Allison
• freshsources.com
• The C++ Programming Language
• Bjarne Stroustrup
• stroustrup.com/4th.html
59. Further Reading (2)
59
• The Annotated C++ Reference Manual
• Margaret Ellis and Bjarne Stroustrup
• stroustrup.com/arm.html
• 1997 C++ Public Review Document
• C++ ISO JTC1/SC22/WG21 Committee
• open-std.org/jtc1/sc22/open/n2356
60. Upcoming Events
• ACGNJ Java Users Group
• Dr. Venkat Subramaniam
• Monday, March 19, 2018
• DorothyYoung Center for the Arts, Room 106
• Drew University
• 7:30-9:00pm
• “Twelve Ways to Make Code Suck Less”
60