Introduction To C++: CS-2303 System Programming Concepts
Introduction To C++: CS-2303 System Programming Concepts
CS-2303
System Programming Concepts
(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and
from C: How to Program, 5th and 6th editions, by Deitel and Deitel)
Introduction to
C++
Reading
Deitel & Deitel, 5th edition Chapter 18
Deitel & Deitel, 6th edition Chapter 15
For reference:
Bjarne Stroustrup, The C++ Programming
Language: Special Edition
Nicolai M. Josuttis, The C++ Standard
Library: A Tutorial and Reference
CS-2303, C-Ter
m 2010
Introduction to
C++
Introduction to
C++
What Is C++?
(Mostly) an extension of C to include:
Classes
Templates
Inheritance and Multiple Inheritance
Function and Operator Overloading
New (and better) Standard Library
References and Reference Parameters
Default Arguments
Note: Objective C was invented
at about the same time, with
Inline Functions
similar goals.
Introduction to
C++
Compiling C++
Use gcc, Visual Studio, etc.
File types
.cc, .cp, .cpp, .CPP, .cxx, .c++, .C
.h, .H Some of these have special properties.
CS-2303, C-Ter
m 2010
Introduction to
C++
In this Topic
Syntax differences between C and C++
A Simple C++ Example
C++ Input/Output
C++ Libraries
C++ Header Files
Introduction to
C++
Background
C++ was developed by Bjarne Stroustrup at
Bell Laboratories
Originally called C with classes
The name C++ is based on Cs increment
operator (++)
Indicating that C++ is an enhanced version of C
Introduction to
C++
stream manipulator
Concatenating insertion operators
return 0;
CS-2303, C-Ter
m 2010
Introduction to
C++
Introduction to
C++
10
Introduction to
C++
11
Introduction to
C++
12
Continues for
three pages!
CS-2303, C-Ter
m 2010
Introduction to
C++
13
<cmath>
sqrt()
CS-2303, C-Ter
m 2010
Introduction to
C++
14
CS-2303, C-Ter
m 2010
Introduction to
C++
18
CS-2303, C-Ter
m 2010
Introduction to
C++
19
References in C++
Definition Reference: An Alternative Name
for an Object
BIG difference from Java
References are only created in declarations
and parameters
A reference can only appear where the
object itself could have appeared
CS-2303, C-Ter
m 2010
Introduction to
C++
21
Simple References
void f() {
int j = 1;
int &r = j;
int x = r;
r = 2;
} //f
// j now is 2
Sometimes, reference
declarations are written as
int& r1 = k
int k;
int &r1 = k;
// okay: r1 is initialized
int &r2; // error; initializer missing
extern int &r3;
//okay; r3 defined
elsewhere
CS-2303, C-Ter
m 2010
Introduction to
C++
22
Introduction to
C++
23
CS-2303, C-Ter
m 2010
Introduction to
C++
24
Reference Parameters
An alias for its corresponding argument in a
function call.
& placed after the parameter type in the function
prototype and function header
Example
int &count in a function header
Pronounced as count is a reference to an int
Introduction to
C++
25
C++ version
void swap (int &a, int &b) {
int temp = a;
Non-hazard:
a = b;
b = temp;
} //
void swap()
CS-2303, C-Ter
m 2010
Introduction to
C++
no pointer here
26
CS-2303, C-Ter
m 2010
Introduction to
C++
27
Introduction to
C++
28
Introduction to
C++
29
Questions?
CS-2303, C-Ter
m 2010
Introduction to
C++
30