SlideShare uma empresa Scribd logo
1 de 61
Baixar para ler offline
1
C++ Advanced
Features
Trenton Computer Festival
March 15, 2014
Michael P. Redlich
@mpredli
about.me/mpredli/
Sunday, March 16, 14
Who’s Mike?
• BS in CS from
• “Petrochemical Research Organization”
• Ai-Logix, Inc. (now AudioCodes)
• Amateur Computer Group of New Jersey
• Publications
• Presentations
2
Sunday, March 16, 14
Objectives
• Overloaded Operators
• Templates
• Exception Handling
• Namespaces
• Introduction to the Standard Template
Library (STL)
3
Sunday, March 16, 14
Overloaded Operators
4
Sunday, March 16, 14
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
Sunday, March 16, 14
6
// String.h - a simple string class
class String {
private:
char *string;
public:
String(char const *str) {
string = new char[strlen(str) + 1];
strcpy(string,str);
}
~String(void) {
delete[] string;
}
char *getString(void) const {
return string;
}
};
Sunday, March 16, 14
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
Sunday, March 16, 14
Overloaded Operators
(1)
• An overloaded operator has the form:
• T [class::]operatorω(paramList)
8
Sunday, March 16, 14
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
Sunday, March 16, 14
10
// Equality Operator
bool string::operator==(string const &s) {
return(strcmp(
getString(),s.getString()) == 0);
}
Sunday, March 16, 14
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
Sunday, March 16, 14
Overloaded Operators
(4)
12
“Hello, C++ Users Group!”
“Hello, Java Users Group!”
string1.string
string2.string
Sunday, March 16, 14
13
// compiler-generated Assignment Operator
string &string::operator=(string const &s) {
string = s.string;
return *this;
}
// main application
string1 = string2;
Sunday, March 16, 14
Overloaded Operators
(5)
14
“Hello, C++ Users Group!”
“Hello, Java Users Group!”
string1.string
string2.string
Sunday, March 16, 14
15
// user-defined Assignment Operator
string &string::operator=(string const &s) {
delete[] string;
string = new char[strlen(s) + 1];
strcpy(string,s);
return *this;
}
// application
string1 = string2;
Sunday, March 16, 14
Overloaded Operators
(6)
16
“Hello, Java Users Group!”
“Hello, Java Users Group!”
string1.string
string2.string
Sunday, March 16, 14
Overloaded Operators
(7)
17
Operators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be Overloaded
+ - * / % ^ & | ~ !
= < > += -+ *= /= %= ^= &=
|= << >> >>= <<= != <= >= &&
|| ++ -- ‘ ->* -> () []
ne
w
de
le
te
Sunday, March 16, 14
Overloaded Operators
(8)
18
Operators That Cannot Be OverloadedOperators That Cannot Be OverloadedOperators That Cannot Be OverloadedOperators That Cannot Be Overloaded
. .* :: ?:
Sunday, March 16, 14
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
Sunday, March 16, 14
Templates
20
Sunday, March 16, 14
What are Templates?
• Used for generic programming
• Two kinds:
• class templates
• member function templates
21
Sunday, March 16, 14
Templates (1)
• Prototypes:
• template <class T> returnType
functionName(paramList) {}
• template <class T> class
className {}
22
Sunday, March 16, 14
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;
}
Sunday, March 16, 14
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;
}
Sunday, March 16, 14
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
Sunday, March 16, 14
Exception Handling
26
Sunday, March 16, 14
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
Sunday, March 16, 14
Exception Handling (1)
• Throw Expression:
• raises the exception
• Try Block:
• contains a throw expression or a member
function that throws an exception
28
Sunday, March 16, 14
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
Sunday, March 16, 14
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
Sunday, March 16, 14
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...
Sunday, March 16, 14
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;
}
Sunday, March 16, 14
C++ Exception Class
Hierarchy (1)
• exception
• logic_error (client program errors)
• runtime_error (external errors)
• bad_alloc (memory allocation errors)
33
Sunday, March 16, 14
C++ Exception Class
Hierarchy (2)
• exception
• bad_cast (dynamic casting errors)
• bad_exception (unexpected())
• bad_typeid (RTTI errors)
34
Sunday, March 16, 14
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...
}
Sunday, March 16, 14
Exception Handling (3)
• Do not throw exceptions:
• to indicate special return values
• in copy constructors and assignment operators
• stroustrup.com/3rd_safe.pdf
36
Sunday, March 16, 14
Namespaces
37
Sunday, March 16, 14
What are Namespaces?
• Used to prevent global naming conflicts
• All C++ standard library components are
contained within a single namespace called
std
38
Sunday, March 16, 14
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
Sunday, March 16, 14
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!
Sunday, March 16, 14
Namespaces (1)
• Fully-qualified member names:
• namespace name
• scope resolution operator (::)
• member name
• baseball::strike
• bowling::strike
41
Sunday, March 16, 14
Aliases
• Provides shorthand for the fully-qualified
namespace name
• Has the form:
• namespace m = N;
• namespace bb = baseball;
• namespace bw = bowling;
42
Sunday, March 16, 14
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
Sunday, March 16, 14
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
Sunday, March 16, 14
Standard Template
Library (STL)
45
Sunday, March 16, 14
What is the STL?
• A subset of Standard C++
• First developed by HP Labs in 1994
• Three main parts:
• containers
• iterators
• algorithms
46
Sunday, March 16, 14
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
Sunday, March 16, 14
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
Sunday, March 16, 14
Containers (2)
49
vector deque list set/map
insert O(n) O(n) O(1) O(nlogn)
prepend O(n) O(1) O(1) O(nlogn)
find O(n) O(n) O(n) O(nlogn)
X[i] O(1) O(1) O(n) O(n)
pointers 0 1 2 3
Sunday, March 16, 14
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
Sunday, March 16, 14
Iterators
• Five types:
• input
• output
• forward
• bi-directional
• random access
51
Sunday, March 16, 14
52
#include <vector>
typedef vector<int>::iterator iterator
vector<int> v(10);
for(int i = 0;i < 9;++i) {
v.push_back(i);
}
iterator current = v.begin();
iterator last = v.end();
while(current != last) {
cout << *current << “n”;
++current;
}
0 1 2 3 4 5 6 7 8
current last
Sunday, March 16, 14
What are Algorithms?
• Perform various operations on containers
such as:
• searching
• sorting
• transforming
53
Sunday, March 16, 14
Algorithms
• Non-Mutating
• binary_search
• count
• equal
• find
• Mutating
• copy
• generate
• remove
• reverse
• sort
54
Sunday, March 16, 14
Popular C++
Compilers
55
• Embarcadero C++ Builder XE5
• embarcadero.com/products/
cbuilder
• MicrosoftVisual C++
• microsoft.com
• Open Watcom 1.9
• openwatcom.org
Sunday, March 16, 14
Local C++ User
Groups
• ACGNJ C++ Users Group
• facilitated by Bruce Arnold
• acgnj.barnold.us
56
Sunday, March 16, 14
Further Reading (1)
57
• C & C++ Code Capsules
• Chuck Allison
• freshsources.com
• The C++ Programming Language
• Bjarne Stroustrup
• stroustrup.com/4th.html
Sunday, March 16, 14
Further Reading (2)
58
• 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
Sunday, March 16, 14
Upcoming Events (1)
• Trenton Computer Festival
• March 14-15, 2014
• tcf-nj.org
• Emerging Technologies for the Enterprise
• April 22-23, 2014
• phillyemergingtech.com
59
Sunday, March 16, 14
60
Upcoming Events (2)
Sunday, March 16, 14
61
Thanks!
mike@redlich.net
@mpredli
javasig.org
Sunday, March 16, 14

Mais conteúdo relacionado

Destaque

Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handlingsanya6900
 
Data Structures by Yaman Singhania
Data Structures by Yaman SinghaniaData Structures by Yaman Singhania
Data Structures by Yaman SinghaniaYaman Singhania
 
Clarke slideshare
Clarke slideshareClarke slideshare
Clarke slidesharemleigh7
 
Kerajaankalingga
KerajaankalinggaKerajaankalingga
KerajaankalinggaPak Yayak
 
POWR POINT PRESENTATION
POWR POINT PRESENTATIONPOWR POINT PRESENTATION
POWR POINT PRESENTATIONSumesh SV
 
Paginas libres
Paginas libresPaginas libres
Paginas libresINGRID
 
EFFECTS OF SOCIAL MEDIA ON YOUTH
EFFECTS OF SOCIAL MEDIA ON YOUTHEFFECTS OF SOCIAL MEDIA ON YOUTH
EFFECTS OF SOCIAL MEDIA ON YOUTHYaman Singhania
 
Lm catering services
Lm catering servicesLm catering services
Lm catering servicesPaulaMaeRamos
 
University Assignment Literacy Assessment
University Assignment Literacy AssessmentUniversity Assignment Literacy Assessment
University Assignment Literacy Assessmentmforrester
 
Classification of Elements Powerpoint Presentation by Computer Careers
Classification of Elements Powerpoint Presentation by Computer CareersClassification of Elements Powerpoint Presentation by Computer Careers
Classification of Elements Powerpoint Presentation by Computer CareersYaman Singhania
 
Madhusha B Ed
Madhusha B Ed Madhusha B Ed
Madhusha B Ed Sumesh SV
 
Powerpoint Presentation
Powerpoint PresentationPowerpoint Presentation
Powerpoint PresentationSumesh SV
 
POWER POINT PRESENTATION
POWER POINT PRESENTATIONPOWER POINT PRESENTATION
POWER POINT PRESENTATIONSumesh SV
 
Power point Presentation
Power point PresentationPower point Presentation
Power point PresentationSumesh SV
 
Getting Started with MongoDB
Getting Started with MongoDBGetting Started with MongoDB
Getting Started with MongoDBMichael Redlich
 
Art exibition
Art exibitionArt exibition
Art exibitionmaleemoha
 

Destaque (20)

class c++
class c++class c++
class c++
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
Data Structures by Yaman Singhania
Data Structures by Yaman SinghaniaData Structures by Yaman Singhania
Data Structures by Yaman Singhania
 
Menupra1
Menupra1Menupra1
Menupra1
 
Clarke slideshare
Clarke slideshareClarke slideshare
Clarke slideshare
 
The Sleeping Beauty
The Sleeping BeautyThe Sleeping Beauty
The Sleeping Beauty
 
Kerajaankalingga
KerajaankalinggaKerajaankalingga
Kerajaankalingga
 
huruf prasekolah
huruf prasekolahhuruf prasekolah
huruf prasekolah
 
POWR POINT PRESENTATION
POWR POINT PRESENTATIONPOWR POINT PRESENTATION
POWR POINT PRESENTATION
 
Paginas libres
Paginas libresPaginas libres
Paginas libres
 
EFFECTS OF SOCIAL MEDIA ON YOUTH
EFFECTS OF SOCIAL MEDIA ON YOUTHEFFECTS OF SOCIAL MEDIA ON YOUTH
EFFECTS OF SOCIAL MEDIA ON YOUTH
 
Lm catering services
Lm catering servicesLm catering services
Lm catering services
 
University Assignment Literacy Assessment
University Assignment Literacy AssessmentUniversity Assignment Literacy Assessment
University Assignment Literacy Assessment
 
Classification of Elements Powerpoint Presentation by Computer Careers
Classification of Elements Powerpoint Presentation by Computer CareersClassification of Elements Powerpoint Presentation by Computer Careers
Classification of Elements Powerpoint Presentation by Computer Careers
 
Madhusha B Ed
Madhusha B Ed Madhusha B Ed
Madhusha B Ed
 
Powerpoint Presentation
Powerpoint PresentationPowerpoint Presentation
Powerpoint Presentation
 
POWER POINT PRESENTATION
POWER POINT PRESENTATIONPOWER POINT PRESENTATION
POWER POINT PRESENTATION
 
Power point Presentation
Power point PresentationPower point Presentation
Power point Presentation
 
Getting Started with MongoDB
Getting Started with MongoDBGetting Started with MongoDB
Getting Started with MongoDB
 
Art exibition
Art exibitionArt exibition
Art exibition
 

Semelhante a C++ Advanced Features (TCF 2014)

Scalable JavaScript
Scalable JavaScriptScalable JavaScript
Scalable JavaScriptYnon Perek
 
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)Michael Redlich
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structuresKuber Chandra
 
Intro to JavaScript Testing
Intro to JavaScript TestingIntro to JavaScript Testing
Intro to JavaScript TestingRan Mizrahi
 
Java Advanced Features (TCF 2014)
Java Advanced Features (TCF 2014)Java Advanced Features (TCF 2014)
Java Advanced Features (TCF 2014)Michael Redlich
 
Getting Started with MongoDB (TCF ITPC 2014)
Getting Started with MongoDB (TCF ITPC 2014)Getting Started with MongoDB (TCF ITPC 2014)
Getting Started with MongoDB (TCF ITPC 2014)Michael Redlich
 
Getting Started with Java (TCF 2014)
Getting Started with Java (TCF 2014)Getting Started with Java (TCF 2014)
Getting Started with Java (TCF 2014)Michael Redlich
 
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]Tom Lee
 
Value objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressValue objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressBrendan Eich
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and OperatorsSunil OS
 
Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming languageKumar Gaurav
 
r,rstats,r language,r packages
r,rstats,r language,r packagesr,rstats,r language,r packages
r,rstats,r language,r packagesAjay Ohri
 

Semelhante a C++ Advanced Features (TCF 2014) (20)

C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
 
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
 
Scalable JavaScript
Scalable JavaScriptScalable JavaScript
Scalable JavaScript
 
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
Intro to JavaScript Testing
Intro to JavaScript TestingIntro to JavaScript Testing
Intro to JavaScript Testing
 
Fluent14
Fluent14Fluent14
Fluent14
 
Java Advanced Features (TCF 2014)
Java Advanced Features (TCF 2014)Java Advanced Features (TCF 2014)
Java Advanced Features (TCF 2014)
 
Getting Started with MongoDB (TCF ITPC 2014)
Getting Started with MongoDB (TCF ITPC 2014)Getting Started with MongoDB (TCF ITPC 2014)
Getting Started with MongoDB (TCF ITPC 2014)
 
Getting Started with Java (TCF 2014)
Getting Started with Java (TCF 2014)Getting Started with Java (TCF 2014)
Getting Started with Java (TCF 2014)
 
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
 
ELEMENTARY DATASTRUCTURES
ELEMENTARY DATASTRUCTURESELEMENTARY DATASTRUCTURES
ELEMENTARY DATASTRUCTURES
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
1 c prog1
1 c prog11 c prog1
1 c prog1
 
Java 8
Java 8Java 8
Java 8
 
Value objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressValue objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progress
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and Operators
 
Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming language
 
r,rstats,r language,r packages
r,rstats,r language,r packagesr,rstats,r language,r packages
r,rstats,r language,r packages
 

Mais de Michael Redlich

Getting Started with GitHub
Getting Started with GitHubGetting Started with GitHub
Getting Started with GitHubMichael Redlich
 
Building Microservices with Micronaut: A Full-Stack JVM-Based Framework
Building Microservices with Micronaut:  A Full-Stack JVM-Based FrameworkBuilding Microservices with Micronaut:  A Full-Stack JVM-Based Framework
Building Microservices with Micronaut: A Full-Stack JVM-Based FrameworkMichael Redlich
 
Building Microservices with Helidon: Oracle's New Java Microservices Framework
Building Microservices with Helidon:  Oracle's New Java Microservices FrameworkBuilding Microservices with Helidon:  Oracle's New Java Microservices Framework
Building Microservices with Helidon: Oracle's New Java Microservices FrameworkMichael Redlich
 
Introduction to Object Oriented Programming & Design Principles
Introduction to Object Oriented Programming & Design PrinciplesIntroduction to Object Oriented Programming & Design Principles
Introduction to Object Oriented Programming & Design PrinciplesMichael Redlich
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++Michael Redlich
 
Introduction to Object Oriented Programming &amp; Design Principles
Introduction to Object Oriented Programming &amp; Design PrinciplesIntroduction to Object Oriented Programming &amp; Design Principles
Introduction to Object Oriented Programming &amp; Design PrinciplesMichael Redlich
 
Getting Started with Java
Getting Started with JavaGetting Started with Java
Getting Started with JavaMichael Redlich
 
Getting started with C++
Getting started with C++Getting started with C++
Getting started with C++Michael Redlich
 
Building Realtime Access to Data Apps with jOOQ
Building Realtime Access to Data Apps with jOOQBuilding Realtime Access to Data Apps with jOOQ
Building Realtime Access to Data Apps with jOOQMichael Redlich
 
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)Michael Redlich
 
Building Realtime Web Apps with Angular and Meteor
Building Realtime Web Apps with Angular and MeteorBuilding Realtime Web Apps with Angular and Meteor
Building Realtime Web Apps with Angular and MeteorMichael Redlich
 
Getting Started with Meteor (TCF ITPC 2014)
Getting Started with Meteor (TCF ITPC 2014)Getting Started with Meteor (TCF ITPC 2014)
Getting Started with Meteor (TCF ITPC 2014)Michael Redlich
 
Getting Started with Meteor
Getting Started with MeteorGetting Started with Meteor
Getting Started with MeteorMichael Redlich
 

Mais de Michael Redlich (14)

Getting Started with GitHub
Getting Started with GitHubGetting Started with GitHub
Getting Started with GitHub
 
Building Microservices with Micronaut: A Full-Stack JVM-Based Framework
Building Microservices with Micronaut:  A Full-Stack JVM-Based FrameworkBuilding Microservices with Micronaut:  A Full-Stack JVM-Based Framework
Building Microservices with Micronaut: A Full-Stack JVM-Based Framework
 
Building Microservices with Helidon: Oracle's New Java Microservices Framework
Building Microservices with Helidon:  Oracle's New Java Microservices FrameworkBuilding Microservices with Helidon:  Oracle's New Java Microservices Framework
Building Microservices with Helidon: Oracle's New Java Microservices Framework
 
Introduction to Object Oriented Programming & Design Principles
Introduction to Object Oriented Programming & Design PrinciplesIntroduction to Object Oriented Programming & Design Principles
Introduction to Object Oriented Programming & Design Principles
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
 
Java Advanced Features
Java Advanced FeaturesJava Advanced Features
Java Advanced Features
 
Introduction to Object Oriented Programming &amp; Design Principles
Introduction to Object Oriented Programming &amp; Design PrinciplesIntroduction to Object Oriented Programming &amp; Design Principles
Introduction to Object Oriented Programming &amp; Design Principles
 
Getting Started with Java
Getting Started with JavaGetting Started with Java
Getting Started with Java
 
Getting started with C++
Getting started with C++Getting started with C++
Getting started with C++
 
Building Realtime Access to Data Apps with jOOQ
Building Realtime Access to Data Apps with jOOQBuilding Realtime Access to Data Apps with jOOQ
Building Realtime Access to Data Apps with jOOQ
 
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)
Building Realtime Access Data Apps with Speedment (TCF ITPC 2017)
 
Building Realtime Web Apps with Angular and Meteor
Building Realtime Web Apps with Angular and MeteorBuilding Realtime Web Apps with Angular and Meteor
Building Realtime Web Apps with Angular and Meteor
 
Getting Started with Meteor (TCF ITPC 2014)
Getting Started with Meteor (TCF ITPC 2014)Getting Started with Meteor (TCF ITPC 2014)
Getting Started with Meteor (TCF ITPC 2014)
 
Getting Started with Meteor
Getting Started with MeteorGetting Started with Meteor
Getting Started with Meteor
 

Último

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 

Último (20)

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 

C++ Advanced Features (TCF 2014)

  • 1. 1 C++ Advanced Features Trenton Computer Festival March 15, 2014 Michael P. Redlich @mpredli about.me/mpredli/ Sunday, March 16, 14
  • 2. Who’s Mike? • BS in CS from • “Petrochemical Research Organization” • Ai-Logix, Inc. (now AudioCodes) • Amateur Computer Group of New Jersey • Publications • Presentations 2 Sunday, March 16, 14
  • 3. Objectives • Overloaded Operators • Templates • Exception Handling • Namespaces • Introduction to the Standard Template Library (STL) 3 Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 6. 6 // String.h - a simple string class class String { private: char *string; public: String(char const *str) { string = new char[strlen(str) + 1]; strcpy(string,str); } ~String(void) { delete[] string; } char *getString(void) const { return string; } }; Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 8. Overloaded Operators (1) • An overloaded operator has the form: • T [class::]operatorω(paramList) 8 Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 10. 10 // Equality Operator bool string::operator==(string const &s) { return(strcmp( getString(),s.getString()) == 0); } Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 12. Overloaded Operators (4) 12 “Hello, C++ Users Group!” “Hello, Java Users Group!” string1.string string2.string Sunday, March 16, 14
  • 13. 13 // compiler-generated Assignment Operator string &string::operator=(string const &s) { string = s.string; return *this; } // main application string1 = string2; Sunday, March 16, 14
  • 14. Overloaded Operators (5) 14 “Hello, C++ Users Group!” “Hello, Java Users Group!” string1.string string2.string Sunday, March 16, 14
  • 15. 15 // user-defined Assignment Operator string &string::operator=(string const &s) { delete[] string; string = new char[strlen(s) + 1]; strcpy(string,s); return *this; } // application string1 = string2; Sunday, March 16, 14
  • 16. Overloaded Operators (6) 16 “Hello, Java Users Group!” “Hello, Java Users Group!” string1.string string2.string Sunday, March 16, 14
  • 17. Overloaded Operators (7) 17 Operators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be OverloadedOperators Than Can Be Overloaded + - * / % ^ & | ~ ! = < > += -+ *= /= %= ^= &= |= << >> >>= <<= != <= >= && || ++ -- ‘ ->* -> () [] ne w de le te Sunday, March 16, 14
  • 18. Overloaded Operators (8) 18 Operators That Cannot Be OverloadedOperators That Cannot Be OverloadedOperators That Cannot Be OverloadedOperators That Cannot Be Overloaded . .* :: ?: Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 21. What are Templates? • Used for generic programming • Two kinds: • class templates • member function templates 21 Sunday, March 16, 14
  • 22. Templates (1) • Prototypes: • template <class T> returnType functionName(paramList) {} • template <class T> class className {} 22 Sunday, March 16, 14
  • 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; } Sunday, March 16, 14
  • 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; } Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 28. Exception Handling (1) • Throw Expression: • raises the exception • Try Block: • contains a throw expression or a member function that throws an exception 28 Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 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... Sunday, March 16, 14
  • 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; } Sunday, March 16, 14
  • 33. C++ Exception Class Hierarchy (1) • exception • logic_error (client program errors) • runtime_error (external errors) • bad_alloc (memory allocation errors) 33 Sunday, March 16, 14
  • 34. C++ Exception Class Hierarchy (2) • exception • bad_cast (dynamic casting errors) • bad_exception (unexpected()) • bad_typeid (RTTI errors) 34 Sunday, March 16, 14
  • 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... } Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 38. What are Namespaces? • Used to prevent global naming conflicts • All C++ standard library components are contained within a single namespace called std 38 Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 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! Sunday, March 16, 14
  • 41. Namespaces (1) • Fully-qualified member names: • namespace name • scope resolution operator (::) • member name • baseball::strike • bowling::strike 41 Sunday, March 16, 14
  • 42. Aliases • Provides shorthand for the fully-qualified namespace name • Has the form: • namespace m = N; • namespace bb = baseball; • namespace bw = bowling; 42 Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 46. What is the STL? • A subset of Standard C++ • First developed by HP Labs in 1994 • Three main parts: • containers • iterators • algorithms 46 Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 49. Containers (2) 49 vector deque list set/map insert O(n) O(n) O(1) O(nlogn) prepend O(n) O(1) O(1) O(nlogn) find O(n) O(n) O(n) O(nlogn) X[i] O(1) O(1) O(n) O(n) pointers 0 1 2 3 Sunday, March 16, 14
  • 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 Sunday, March 16, 14
  • 51. Iterators • Five types: • input • output • forward • bi-directional • random access 51 Sunday, March 16, 14
  • 52. 52 #include <vector> typedef vector<int>::iterator iterator vector<int> v(10); for(int i = 0;i < 9;++i) { v.push_back(i); } iterator current = v.begin(); iterator last = v.end(); while(current != last) { cout << *current << “n”; ++current; } 0 1 2 3 4 5 6 7 8 current last Sunday, March 16, 14
  • 53. What are Algorithms? • Perform various operations on containers such as: • searching • sorting • transforming 53 Sunday, March 16, 14
  • 54. Algorithms • Non-Mutating • binary_search • count • equal • find • Mutating • copy • generate • remove • reverse • sort 54 Sunday, March 16, 14
  • 55. Popular C++ Compilers 55 • Embarcadero C++ Builder XE5 • embarcadero.com/products/ cbuilder • MicrosoftVisual C++ • microsoft.com • Open Watcom 1.9 • openwatcom.org Sunday, March 16, 14
  • 56. Local C++ User Groups • ACGNJ C++ Users Group • facilitated by Bruce Arnold • acgnj.barnold.us 56 Sunday, March 16, 14
  • 57. Further Reading (1) 57 • C & C++ Code Capsules • Chuck Allison • freshsources.com • The C++ Programming Language • Bjarne Stroustrup • stroustrup.com/4th.html Sunday, March 16, 14
  • 58. Further Reading (2) 58 • 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 Sunday, March 16, 14
  • 59. Upcoming Events (1) • Trenton Computer Festival • March 14-15, 2014 • tcf-nj.org • Emerging Technologies for the Enterprise • April 22-23, 2014 • phillyemergingtech.com 59 Sunday, March 16, 14