SlideShare a Scribd company logo
A Condensed
Crash Course on C++
ECE 417/617:
Elements of Software Engineering
Stan Birchfield
Clemson University
Recommended C++ resources
• Bjarne Stroustrup,
The C++ Programming
Language
• Scott Meyers,
Effective C++
Why C++?
• Popular and relevant (used in nearly every application domain):
– end-user applications (Word, Excel, PowerPoint, Photoshop, Acrobat,
Quicken, games)
– operating systems (Windows 9x, NT, XP; IBM’s K42; some Apple OS X)
– large-scale web servers/apps (Amazon, Google)
– central database control (Israel’s census bureau; Amadeus; Morgan-
Stanley financial modeling)
– communications (Alcatel; Nokia; 800 telephone numbers; major
transmission nodes in Germany and France)
– numerical computation / graphics (Maya)
– device drivers under real-time constraints
• Stable, compatible, scalable
C vs. C++
• C++ is C incremented
(orig., “C with classes”)
• C++ is more expressive
(number of C++ source lines less than C for same program)
• C++ is just as permissive
(anything you can do in C can also be done in C++)
• C++ can be just as efficient
(most C++ expressions need no run-time support;
C++ allows you to
– manipulate bits directly and interface with hardware without regard for safety
or ease of comprehension, BUT
– hide these details behind a safe, clean, elegant interface)
• C++ is more maintainable
(1000 lines of code – even brute force, spaghetti code will work;
100,000 lines of code – need good structure, or new errors will be
introduced as quickly as old errors are removed)
Efficiency and Maintainability
90/10 rule: 10% of your program will take 90% of the
processor time to run
optimize what needs to be optimized, but no more
focus on design
Design goals of C++
• Backward compatibility with C
(almost completely – every program in K&R is a C++ program)
• Simplicity, elegance
(few built-in data types, e.g., no matrices)
• Support for user-defined data types
(act like built-in types; N.B. Standard Template Library (STL))
• No compromise in efficiency, run-time or memory
(unless “advanced features” used)
• Compliation analysis to prevent accidental corruption
of data
(type-checking and data hiding)
• Support object-oriented style of programming
Compatibility with C
C++ does not allow
• old-style C function declarations
void f(a) int a; {}
• generic function declarations
void f();
void g() { f(2); }
• setting enum to int
enum Dir {Up, Down};
Dir d=1;
• multiple declarations
int i; int i;
• assigning to void *
int* p = malloc(10);
• “implicit int”
signed a = 7;
Other differences:
• const global variables have
internal linkage in C++,
external in C
• extra keywords in C++
void main()
{ int catch = 5; }
• bizarre comments
int f(int a, int b)
{
return a//**/b
;
}
How is C++ not backward compatible with C (C89)?
(For these, C++ is backward
compatible with C99)
Purpose of a programming
language
• Programming languages serve two
purposes:
– vehicle for specifying actions to be executed
“close to the machine”
– set of concepts for thinking about what can
be done
“close to the problem being solved”
• Object-oriented C++ excels at both
Learning C++
• Goal: Don’t just learn new syntax, but become a
better programmer and designer; learn new and
better ways of building systems
• Be willing to learn C++’s style; don’t force
another style into it
• C++ supports gradual learning
– Use what you know
– As you learn new features and techniques, add those
tools to your toolbox
• C++ supports variety of programming paradigms
Programming paradigms
• procedural – implement algorithms via
functions (variables, functions, etc.)
• modular – partition program into modules
(separate compilation)
• object-oriented – divide problem into classes
(data hiding, inheritance)
• abstract – separate interface from
implementation (abstract classes)
• generic – manipulate arbitrary data types
(STL: containers, algorithms)
• Encapsulation
“black box” – internal data hidden
• Inheritance
related classes share implementation
and/or interface
• Polymorphism
ability to use a class without knowing its type
What is object-oriented?
© SDC
“C++ is an object-oriented language” =
C++ provides mechanisms that support object-oriented style of programming
Some C++ concepts
• constructor / destructor / copy constructor
• initialization list
• inheritance
• exceptions
• overloading operators (e.g., assignment operator)
• namespace
• const
• virtual function
• pure virtual (abstract) function
• friend
• template
• standard template library (STL)
• pass by value, pass by reference
• composition versus derivation
A simple C++ program
#include <iostream> // std::cout
#include <cstdio> // printf
int main()
{
int a = 5; // 'a' is L-value
float b = 0.9f;
printf("Hello world %d %3.1f n", a, b);
std::cout << "Hello world" << a << " "
<< b << " " << std::endl;
return 0;
}
Declarations and definitions
• Declaration:
– extern char c;
– struct User;
– double sqrt(double);
 Okay to have many
• Definition:
– char c;
– int count = 1;
– double abs(double a) {
a>0 ? a : -a;
}
 Must have exactly one
Fundamental types
• bool (true  1, false  0)
• char (could be signed or unsigned
– implementation-defined)
• int (signed by default)
• double
• void (“pseudo-type”)
• enum
• class
• also short, long, struct, float,
wchar_t, etc.)
Do not rely on sizes of these!
(Implementation-dependent)
INTEGRAL
ARITHMETIC
USER-DEFINED
Macros
• Dangerous:
– compiler never sees them
source code  translation unit
– global
• Instead, use
– const
– inline
– template
– enum
• Ok to use for include guards (“header wrappers”)
• If you must use a macro, give it a long ugly name with
lots of capital letters
Example:
template<typename T>
inline T max(T t){
t>0 ? t : -t;
}
Memory allocation
• “on the stack”
– block delimited by {}
– object alive till it falls out of scope
– calls constructor / destructor
• “on the heap”
– new and delete replace malloc, calloc, free
– object exists independently of scope in which it was created
– also “on the free store” or “allocated in dynamic memory”
– be careful: new  delete, new[]  delete[]
– for safety, same object should both allocate and deallocate
• “local static store” void foo() {
static int i=0;
}
Global variables
• Built-in types initialized to 0
(but local variables uninitialized)
• Initialized before main() invoked
• Initialization order:
– within translation unit, same as definition
– between translation units, arbitrary order
file1.cpp
double pi = 3.14;
file2.cpp
double twopi = 2*pi;
A class
class Date {
public:
enum Month {Jan, Feb, Mar, ...}
Date(int year, Month month, int day);
int GetDay() const;
void SetDay(int day);
Date& operator+=(int days);
private:
Month m_month;
int m_year, m_day;
};
member
functions
member
variables
Names
• Maintain consistent naming style
– long names for large scope
– short names for small scope
• Don’t start with underscore; reserved for
special facilities
• Avoid similar-looking names: l and 1
• Choosing good names is an art
Access control
• Public: visible to everyone
• Private: visible only to the
implementer of this particular class
• Protected: visible to this class and
derived classes
• Good rule of thumb:
– member functions public or protected
– member variables private
Default class functions
• By default, each class has member functions:
– constructor Date();
– destructor ~Date();
– copy constructor
Date(const Date& other);
– assignment operator
Date& operator=(const Date& other);
• These call the appropriate functions on each
member variable
• Be careful: If this is not what you want, then
either override or disallow (by making private)
Constructor and destructor
• (Copy) constructor creates object
• Destructor destroys (“cleans up”) object
• Be aware of temporary objects
class Matrix {
Matrix(const Matrix& other);
Matrix& operator+(const Matrix& other);
Matrix& operator=(const Matrix& other);
};
void foo() {
Matrix a, b, c, d;
a = b + c + d;
}
What functions get called?
(Note: There are ways to speed this up while preserving the syntax)
Initializer lists
Matrix::Matrix(const Matrix& other)
: m_n(0), m_a(0)
{
}
Matrix::Matrix(const Matrix& other)
{
m_n = 0;
m_a = 0;
}
Use initializer list:
Assign values inside constructor:
Concrete classes
• A concrete class
– does a single, relatively small thing well and
efficiently
– hides data members (encapsulation)
– provides clean interface
– acts like a built-in type
– is a “foundation of elegant programming” –
Stroustrup
• Don’t underestimate the importance of this
basic C++/OO feature!
Class relationships
• OK:
– A calls function from B
– A creates B
– A has a data member of type B
• Bad:
– A uses data directly from B
(without using B’s interface)
• Even worse:
– A directly manipulates data in B
Pointers, arrays, references
• Use 0, not NULL (stronger type checking)
• Name of array is equivalent to pointer to
initial element
• Access array using * or []; same
efficiency with modern compiler
• use std::vector, not built-in array,
when possible
• Reference is like a pointer
References
• Reference: alternate
name for an object
• There is no null
reference
• No pointer to a reference
• No reference to a
temporary
• Syntax confusing
• Basically a const
dereferenced pointer
with no operations
int &a;
int a; int& b=a; b++;
int* c = &a;
int &a = b; int* c= &a;
int& a = 1;
Argument passing / return
• Pass / return by value
– calls copy constructor
– ok for built-in types
int foo(int a) { return 0; }
– performance penalty for structs and classes (temporary objects)
• Pass / return by reference or pointer
– does not call copy constructor
– pass inputs by const reference
– never pass inputs by “plain” reference
void update(int& a); update(2); // error
– pass outputs by pointer
int x = 1; next(x); // should not change x
int x = 1; next(&x); // may change x
– ok to return a ref, or const ref
C++ function mechanisms
• Overloaded function names
– Cleaner and safer
print(int);
print(float);
– But beware
print(int); print(int*); print(0);
• Default parameters
void print(int a, int b=0, int c=0);
• Operator overloading
Matrix& operator+=(const Matrix& other);
• Implicit conversion operator
operator int() const {} // converts to int
– Provides convenient syntax, but potentially dangerous so use
sparingly
Explicit type conversion
• C++ casts
– static_cast between 2 related types
(int/float, int/enum, 2 pointers in class hierarchy)
– reinterpret_cast between 2 unrelated types
(int/ptr, pointers to 2 unrelated classes)
– const_cast cast away constness
– dynamic_cast used for polymorphic types
Run-time type info (RTTI)
• Avoid casts, but use these instead of C casts
– e.g., compiler can perform minimal checking for
static_cast, none for reinterpret_cast
Namespaces
• Namespace expresses logical grouping
• using declaration
– Don’t use global using except for transition to
older code
– Ok in namespace for composition
– Ok in function for notational convenience
• Namespaces are open
• Unnamed namespaces restrict code to local
translation unit
• Aliases
Const
• Const prevents object from being modified (orig., readonly)
• Avoid magic numbers
char a[128];
const int maxn = 128;
char a[maxn];
• Logical constness vs. physical constness
• Const is your friend; use it extensively and consistently
• can cast away constness, but be sure to use mutable
• const pointers:
– const int * const ptr = &a[0]; // const ptr to a const int
– int const * const ptr = &a[0]; // ”
– int * const p2 = &a[0]; // const ptr to an int
– const int * p1 = &a[0]; // ptr to a const int
– int const * p2 = &a[0]; // ”
Inheritance
• Subclass derived from base class
• Two classes should pass the “ISA” test:
derived class is a base class
class Shape {
};
class Circle : public Shape {
};
• Class hierarchy: means of building classes
incrementally, using building blocks
(subclass becomes base class for someone else)
• Facilitates code reuse
Inheritance vs. composition
• Inheritance: “is a”
class Circle : public Shape {
};
• Composition: “has a”
class Circle {
private:
Shape m_shape;
};
• Decision should be based on commonality of
interface
Virtual functions
• Function of derived class is called even if
you have only a pointer to the base class
File.h
class Shape
{
virtual void Draw();
};
class Circle : public Shape
{
virtual void Draw();
};
File.cpp
void Func1()
{
Circle mycirc;
Func2(&mycirc);
}
void Func2(Shape* s)
{
s->Draw();
}
// calls Circle::Draw()
How a virtual function works
Shape vtable
vfunc1 addr
vfunc2 addr
...
vfuncN addr
vfunc1 addr
vfunc2 addr
...
vfuncN addr
Circle vtable
var1
...
varM
vtable ptr
mycirc
What is the penalty of a virtual
function?
• Space:
– one vtable per class with virtual function(s)
– one pointer per instance
• Time:
– one extra dereference if type not known at
compile time
– no penalty if type known at compile time
(ok to inline a virtual function)
Pure virtual function
• Pure virtual function
– Function intentionally undefined
– Same penalty as regular virtual function
• Abstract class
– Contains at least one pure virtual function
– Cannot instantiate; must derive from base class and override
pure virtual function
– Provides an interface
(separates interface from implementation)
class Shape {
virtual void Draw() = 0;
};
Multiple inheritance
• C++ allows you to inherit from multiple
base classes
class CalculatorWatch :
public Calculator, Watch {};
• Works best if
– exactly one base class passes ISA test
– all other base classes are interfaces
• Advanced feature that you will not need
in this course
Polymorphism
• Polymorphism
– “ability to assume different forms”
– one object acts like many different types of objects
(e.g., Shape*)
– getting the right behavior without knowing the type
– manipulate objects with a common set of operations
• Two types:
– Run-time (Virtual functions)
– Compile-time (Templates)
Exceptions
• Error handling in C:
– Half of code is error handling
– Dangerous: Easy for programmer to forget
to check return value
void Func() {
int ret;
ret = OpenDevice();
if (ret != 0) error(“Unable to open device”);
ret = SetParams();
if (ret != 0) error(“Unable to set params”);
}
Exceptions (cont.)
• Error handling in C++:
– try-catch blocks safer
– separate “real code” from error handling code
void Func() {
try {
OpenDevice();
SetParams();
} catch (const MyException& e) {
e.ReportToUser();
} catch (...) {
abort(1);
}
}
void OpenDevice()
{
if (bad) throw MyException(“Cannot open device”);
}
Templates
• Define a class or function once, to work with a variety
of types
• Types may not be known until future
• Better type checking and faster (cf. qsort)
• Specialization can be used to reduce code bloat
• Templates support generic programming
template<typename T>
T Max(T a, T b) { return a>b ? a : b; }
template<typename T>
class Vector {
Vector(int n, T init_val);
T* m_vals;
};
Generic programming
• Drawbacks of qsort in <stdlib.h>
– requires a compare function, even if trivial
– loss of efficiency b/c dereferencing pointer
– lost type safety b/c void*
– only works with contiguous arrays
– no control over construction / destruction /
assignment; all swapping done by raw
memory moves
Standard Template Library (STL)
• Containers:
– Sequences
• vector – array in contiguous memory (replaces realloc)
• list – doubly-linked list (insert/delete fast)
• deque (“deck”) – double-ended queue
• stack, queue, priority queue
– Associative
• map – dictionary; balanced tree of (key,value) pairs
like array with non-integer indices
• set – map with values ignored (only keys important)
• multimap, multiset (allow duplicate keys)
– Other
• string, basic_string – not necessarily contiguous
• valarray – vector for numeric computation
• bitset – set of N bits
STL (cont.)
• Algorithms (60 of them):
– Nonmodifying
• find, search, mismatch, count, for_each
– Modifying
• copy, transform/apply, replace, remove
– Others
• unique, reverse, random_shuffle
• sort, merge, partition
• set_union, set_intersection, set_difference
• min, max, min_element, max_element
• next_permutation, prev_permutation
std::string
• Example:
#include <string>
void Func()
{
std::string s, t;
char c = 'a';
s.push_back(c); // s is now “a”;
const char* cc = s.c_str(); // get ptr to “a”
const char dd[] = "afaf";
t = dd; // t is now “afaf”;
t = t + s; // append “a” to “afaf”
}
std::vector
#include <vector>
void Func()
{
std::vector<int> v(10);
int a0 = v[3]; // unchecked access
int a1 = v.at(3); // checked access
v.push_back(2); // append element to end
v.pop_back(); // remove last element
size_t howbig = v.size(); // get # of elements
v.insert(v.begin()+5, 2); // insert 2 after 5th element
}
• Example:
std::vector (cont.)
#include <vector>
#include <algorithm>
void Func()
{
std::vector<int> v(10);
v[5] = 3; // set fifth element to 3
std::vector<int>::const_iterator it
= std::find(v.begin(), v.end(), 3);
bool found = it != v.end();
if (found) {
int three = *it;
int indx = it - v.begin();
int four = 4;
}
}
• Example:
Iterators
• iterator – generalized pointer
• Each container has its own type of
iterator
void Func() {
stl::vector<int> v;
stl::vector<int>::const_iterator it = v.begin();
for (it = v.begin() ; it != v.end() ; it++) {
int val = *it;
}
}
Types of iterators
template<class InputIterator, class Type>
InputIterator
find( InputIterator _First,
InputIterator _Last,
const Type& _Val );
• Each container provides a
different type
input
forward
bidirectional
random access
output
Types
Allocators
• STL written for maximum flexibility
• Each container has an allocator
• Allocator is responsible for memory
management (new/delete)
template < class Type,
class Allocator = allocator<Type> >
class vector {
...
};
• Advice: Ignore allocators
Streams
• C
– flush, fprintf, fscanf, sprintf, sscanf
– fgets, getc
• C++
– cout, cin, cerr
Numerics
• valarray
– matrix and vector (not std::vector)
– slices and gslices
• complex
• random numbers

More Related Content

Similar to UsingCPP_for_Artist.ppt (20)

Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
Steve Johnson
 
c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
c++ Unit III - PPT.pptx
Kongunadu College of Engineering and Technology
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
OpenSource Technologies Pvt. Ltd.
 
C++ overview
C++ overviewC++ overview
C++ overview
Prem Ranjan
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and Classes
Michael Heron
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
Michael Heron
 
C++ Basics
C++ BasicsC++ Basics
C++ Basics
Himanshu Sharma
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
rehan16091997
 
Bjarne essencegn13
Bjarne essencegn13Bjarne essencegn13
Bjarne essencegn13
Hunde Gurmessa
 
Return of c++
Return of c++Return of c++
Return of c++
Yongwei Wu
 
Object Oriented Programming Using C++.pptx
Object Oriented Programming  Using C++.pptxObject Oriented Programming  Using C++.pptx
Object Oriented Programming Using C++.pptx
bscit6
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
c++ Unit I.pptx
Kongunadu College of Engineering and Technology
 
C++ & Data Structure - Unit - first.pptx
C++ & Data Structure - Unit - first.pptxC++ & Data Structure - Unit - first.pptx
C++ & Data Structure - Unit - first.pptx
KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY
 
Louis Loizides iOS Programming Introduction
Louis Loizides iOS Programming IntroductionLouis Loizides iOS Programming Introduction
Louis Loizides iOS Programming Introduction
Lou Loizides
 
iOS Programming Intro
iOS Programming IntroiOS Programming Intro
iOS Programming Intro
Lou Loizides
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
Adeel Hamid
 
Cs1123 11 pointers
Cs1123 11 pointersCs1123 11 pointers
Cs1123 11 pointers
TAlha MAlik
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Chapter10_Data_Abstraction_and_Object_Orientation_4e.ppt
Chapter10_Data_Abstraction_and_Object_Orientation_4e.pptChapter10_Data_Abstraction_and_Object_Orientation_4e.ppt
Chapter10_Data_Abstraction_and_Object_Orientation_4e.ppt
techtrainer11
 

Recently uploaded (20)

Printable Tongan Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tongan Gospel Tract - ONLY JESUS CHRIST SAVES.pdfPrintable Tongan Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tongan Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Tsonga Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tsonga Gospel Tract - ONLY JESUS CHRIST SAVES.pdfPrintable Tsonga Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tsonga Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Filipino Tracts and Literature Society Inc.
 
The Gifts of Saint Louise de Marillac - McCullen
The Gifts of Saint Louise de Marillac - McCullenThe Gifts of Saint Louise de Marillac - McCullen
The Gifts of Saint Louise de Marillac - McCullen
Famvin: the Worldwide Vincentian Family
 
Sabbath School Lesson 7, 2nd Quarter 2025.pptx
Sabbath School Lesson 7, 2nd Quarter 2025.pptxSabbath School Lesson 7, 2nd Quarter 2025.pptx
Sabbath School Lesson 7, 2nd Quarter 2025.pptx
DavidSyahputra4
 
Taste Me Whole - A Sensual Journey Through Hunger, Flesh and Becoming.
Taste Me Whole - A Sensual Journey Through Hunger, Flesh and Becoming.Taste Me Whole - A Sensual Journey Through Hunger, Flesh and Becoming.
Taste Me Whole - A Sensual Journey Through Hunger, Flesh and Becoming.
ssuser83613b
 
Printable Tamazight (Tifinagh) Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tamazight (Tifinagh) Gospel Tract - ONLY JESUS CHRIST SAVES.pdfPrintable Tamazight (Tifinagh) Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tamazight (Tifinagh) Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Tatar Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tatar Gospel Tract - ONLY JESUS CHRIST SAVES.pdfPrintable Tatar Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tatar Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Filipino Tracts and Literature Society Inc.
 
Connecting and Serving at Vintage: 1 Corinthians 12
Connecting and Serving at Vintage: 1 Corinthians 12Connecting and Serving at Vintage: 1 Corinthians 12
Connecting and Serving at Vintage: 1 Corinthians 12
Vintage Church
 
Printable Tamil Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tamil Gospel Tract - ONLY JESUS CHRIST SAVES.pdfPrintable Tamil Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tamil Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Filipino Tracts and Literature Society Inc.
 
Printable Tshiluba Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tshiluba Gospel Tract - ONLY JESUS CHRIST SAVES.pdfPrintable Tshiluba Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tshiluba Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Filipino Tracts and Literature Society Inc.
 
Monthly Khazina-e-Ruhaniyaat May’2025 (Vol.16, Issue 1)
Monthly Khazina-e-Ruhaniyaat May’2025 (Vol.16, Issue 1)Monthly Khazina-e-Ruhaniyaat May’2025 (Vol.16, Issue 1)
Monthly Khazina-e-Ruhaniyaat May’2025 (Vol.16, Issue 1)
Darul Amal Chishtia
 
Printable Tiv Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tiv Gospel Tract - ONLY JESUS CHRIST SAVES.pdfPrintable Tiv Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tiv Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Filipino Tracts and Literature Society Inc.
 
Walking with the Shepherd 110525 Bangsar LC.pptx
Walking with the Shepherd 110525 Bangsar LC.pptxWalking with the Shepherd 110525 Bangsar LC.pptx
Walking with the Shepherd 110525 Bangsar LC.pptx
John Cheah, CAMS
 
Married to the Forest...................
Married to the Forest...................Married to the Forest...................
Married to the Forest...................
ssuser83613b
 
Kaia Ra Sedona - An Inspirational Author
Kaia Ra Sedona - An Inspirational AuthorKaia Ra Sedona - An Inspirational Author
Kaia Ra Sedona - An Inspirational Author
Kaia Ra Sedona
 
Viral: A Study of Acts_Acts 5.1-11_Slides.pptx
Viral: A Study of Acts_Acts 5.1-11_Slides.pptxViral: A Study of Acts_Acts 5.1-11_Slides.pptx
Viral: A Study of Acts_Acts 5.1-11_Slides.pptx
Stephen Palm
 
Printable Tibetan Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tibetan Gospel Tract - ONLY JESUS CHRIST SAVES.pdfPrintable Tibetan Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Tibetan Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Filipino Tracts and Literature Society Inc.
 
The Codex of Nine - Maths Papers - Gallery
The Codex of Nine - Maths Papers - GalleryThe Codex of Nine - Maths Papers - Gallery
The Codex of Nine - Maths Papers - Gallery
ssuser83613b
 
Virtue Ethics & Social Media Situational Analysis.pdf
Virtue Ethics & Social Media Situational Analysis.pdfVirtue Ethics & Social Media Situational Analysis.pdf
Virtue Ethics & Social Media Situational Analysis.pdf
keisshacanios
 
Printable Swedish Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Swedish Gospel Tract - ONLY JESUS CHRIST SAVES.pdfPrintable Swedish Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Printable Swedish Gospel Tract - ONLY JESUS CHRIST SAVES.pdf
Filipino Tracts and Literature Society Inc.
 
Sabbath School Lesson 7, 2nd Quarter 2025.pptx
Sabbath School Lesson 7, 2nd Quarter 2025.pptxSabbath School Lesson 7, 2nd Quarter 2025.pptx
Sabbath School Lesson 7, 2nd Quarter 2025.pptx
DavidSyahputra4
 
Taste Me Whole - A Sensual Journey Through Hunger, Flesh and Becoming.
Taste Me Whole - A Sensual Journey Through Hunger, Flesh and Becoming.Taste Me Whole - A Sensual Journey Through Hunger, Flesh and Becoming.
Taste Me Whole - A Sensual Journey Through Hunger, Flesh and Becoming.
ssuser83613b
 
Connecting and Serving at Vintage: 1 Corinthians 12
Connecting and Serving at Vintage: 1 Corinthians 12Connecting and Serving at Vintage: 1 Corinthians 12
Connecting and Serving at Vintage: 1 Corinthians 12
Vintage Church
 
Monthly Khazina-e-Ruhaniyaat May’2025 (Vol.16, Issue 1)
Monthly Khazina-e-Ruhaniyaat May’2025 (Vol.16, Issue 1)Monthly Khazina-e-Ruhaniyaat May’2025 (Vol.16, Issue 1)
Monthly Khazina-e-Ruhaniyaat May’2025 (Vol.16, Issue 1)
Darul Amal Chishtia
 
Walking with the Shepherd 110525 Bangsar LC.pptx
Walking with the Shepherd 110525 Bangsar LC.pptxWalking with the Shepherd 110525 Bangsar LC.pptx
Walking with the Shepherd 110525 Bangsar LC.pptx
John Cheah, CAMS
 
Married to the Forest...................
Married to the Forest...................Married to the Forest...................
Married to the Forest...................
ssuser83613b
 
Kaia Ra Sedona - An Inspirational Author
Kaia Ra Sedona - An Inspirational AuthorKaia Ra Sedona - An Inspirational Author
Kaia Ra Sedona - An Inspirational Author
Kaia Ra Sedona
 
Viral: A Study of Acts_Acts 5.1-11_Slides.pptx
Viral: A Study of Acts_Acts 5.1-11_Slides.pptxViral: A Study of Acts_Acts 5.1-11_Slides.pptx
Viral: A Study of Acts_Acts 5.1-11_Slides.pptx
Stephen Palm
 
The Codex of Nine - Maths Papers - Gallery
The Codex of Nine - Maths Papers - GalleryThe Codex of Nine - Maths Papers - Gallery
The Codex of Nine - Maths Papers - Gallery
ssuser83613b
 
Virtue Ethics & Social Media Situational Analysis.pdf
Virtue Ethics & Social Media Situational Analysis.pdfVirtue Ethics & Social Media Situational Analysis.pdf
Virtue Ethics & Social Media Situational Analysis.pdf
keisshacanios
 

UsingCPP_for_Artist.ppt

  • 1. A Condensed Crash Course on C++ ECE 417/617: Elements of Software Engineering Stan Birchfield Clemson University
  • 2. Recommended C++ resources • Bjarne Stroustrup, The C++ Programming Language • Scott Meyers, Effective C++
  • 3. Why C++? • Popular and relevant (used in nearly every application domain): – end-user applications (Word, Excel, PowerPoint, Photoshop, Acrobat, Quicken, games) – operating systems (Windows 9x, NT, XP; IBM’s K42; some Apple OS X) – large-scale web servers/apps (Amazon, Google) – central database control (Israel’s census bureau; Amadeus; Morgan- Stanley financial modeling) – communications (Alcatel; Nokia; 800 telephone numbers; major transmission nodes in Germany and France) – numerical computation / graphics (Maya) – device drivers under real-time constraints • Stable, compatible, scalable
  • 4. C vs. C++ • C++ is C incremented (orig., “C with classes”) • C++ is more expressive (number of C++ source lines less than C for same program) • C++ is just as permissive (anything you can do in C can also be done in C++) • C++ can be just as efficient (most C++ expressions need no run-time support; C++ allows you to – manipulate bits directly and interface with hardware without regard for safety or ease of comprehension, BUT – hide these details behind a safe, clean, elegant interface) • C++ is more maintainable (1000 lines of code – even brute force, spaghetti code will work; 100,000 lines of code – need good structure, or new errors will be introduced as quickly as old errors are removed)
  • 5. Efficiency and Maintainability 90/10 rule: 10% of your program will take 90% of the processor time to run optimize what needs to be optimized, but no more focus on design
  • 6. Design goals of C++ • Backward compatibility with C (almost completely – every program in K&R is a C++ program) • Simplicity, elegance (few built-in data types, e.g., no matrices) • Support for user-defined data types (act like built-in types; N.B. Standard Template Library (STL)) • No compromise in efficiency, run-time or memory (unless “advanced features” used) • Compliation analysis to prevent accidental corruption of data (type-checking and data hiding) • Support object-oriented style of programming
  • 7. Compatibility with C C++ does not allow • old-style C function declarations void f(a) int a; {} • generic function declarations void f(); void g() { f(2); } • setting enum to int enum Dir {Up, Down}; Dir d=1; • multiple declarations int i; int i; • assigning to void * int* p = malloc(10); • “implicit int” signed a = 7; Other differences: • const global variables have internal linkage in C++, external in C • extra keywords in C++ void main() { int catch = 5; } • bizarre comments int f(int a, int b) { return a//**/b ; } How is C++ not backward compatible with C (C89)? (For these, C++ is backward compatible with C99)
  • 8. Purpose of a programming language • Programming languages serve two purposes: – vehicle for specifying actions to be executed “close to the machine” – set of concepts for thinking about what can be done “close to the problem being solved” • Object-oriented C++ excels at both
  • 9. Learning C++ • Goal: Don’t just learn new syntax, but become a better programmer and designer; learn new and better ways of building systems • Be willing to learn C++’s style; don’t force another style into it • C++ supports gradual learning – Use what you know – As you learn new features and techniques, add those tools to your toolbox • C++ supports variety of programming paradigms
  • 10. Programming paradigms • procedural – implement algorithms via functions (variables, functions, etc.) • modular – partition program into modules (separate compilation) • object-oriented – divide problem into classes (data hiding, inheritance) • abstract – separate interface from implementation (abstract classes) • generic – manipulate arbitrary data types (STL: containers, algorithms)
  • 11. • Encapsulation “black box” – internal data hidden • Inheritance related classes share implementation and/or interface • Polymorphism ability to use a class without knowing its type What is object-oriented? © SDC “C++ is an object-oriented language” = C++ provides mechanisms that support object-oriented style of programming
  • 12. Some C++ concepts • constructor / destructor / copy constructor • initialization list • inheritance • exceptions • overloading operators (e.g., assignment operator) • namespace • const • virtual function • pure virtual (abstract) function • friend • template • standard template library (STL) • pass by value, pass by reference • composition versus derivation
  • 13. A simple C++ program #include <iostream> // std::cout #include <cstdio> // printf int main() { int a = 5; // 'a' is L-value float b = 0.9f; printf("Hello world %d %3.1f n", a, b); std::cout << "Hello world" << a << " " << b << " " << std::endl; return 0; }
  • 14. Declarations and definitions • Declaration: – extern char c; – struct User; – double sqrt(double);  Okay to have many • Definition: – char c; – int count = 1; – double abs(double a) { a>0 ? a : -a; }  Must have exactly one
  • 15. Fundamental types • bool (true  1, false  0) • char (could be signed or unsigned – implementation-defined) • int (signed by default) • double • void (“pseudo-type”) • enum • class • also short, long, struct, float, wchar_t, etc.) Do not rely on sizes of these! (Implementation-dependent) INTEGRAL ARITHMETIC USER-DEFINED
  • 16. Macros • Dangerous: – compiler never sees them source code  translation unit – global • Instead, use – const – inline – template – enum • Ok to use for include guards (“header wrappers”) • If you must use a macro, give it a long ugly name with lots of capital letters Example: template<typename T> inline T max(T t){ t>0 ? t : -t; }
  • 17. Memory allocation • “on the stack” – block delimited by {} – object alive till it falls out of scope – calls constructor / destructor • “on the heap” – new and delete replace malloc, calloc, free – object exists independently of scope in which it was created – also “on the free store” or “allocated in dynamic memory” – be careful: new  delete, new[]  delete[] – for safety, same object should both allocate and deallocate • “local static store” void foo() { static int i=0; }
  • 18. Global variables • Built-in types initialized to 0 (but local variables uninitialized) • Initialized before main() invoked • Initialization order: – within translation unit, same as definition – between translation units, arbitrary order file1.cpp double pi = 3.14; file2.cpp double twopi = 2*pi;
  • 19. A class class Date { public: enum Month {Jan, Feb, Mar, ...} Date(int year, Month month, int day); int GetDay() const; void SetDay(int day); Date& operator+=(int days); private: Month m_month; int m_year, m_day; }; member functions member variables
  • 20. Names • Maintain consistent naming style – long names for large scope – short names for small scope • Don’t start with underscore; reserved for special facilities • Avoid similar-looking names: l and 1 • Choosing good names is an art
  • 21. Access control • Public: visible to everyone • Private: visible only to the implementer of this particular class • Protected: visible to this class and derived classes • Good rule of thumb: – member functions public or protected – member variables private
  • 22. Default class functions • By default, each class has member functions: – constructor Date(); – destructor ~Date(); – copy constructor Date(const Date& other); – assignment operator Date& operator=(const Date& other); • These call the appropriate functions on each member variable • Be careful: If this is not what you want, then either override or disallow (by making private)
  • 23. Constructor and destructor • (Copy) constructor creates object • Destructor destroys (“cleans up”) object • Be aware of temporary objects class Matrix { Matrix(const Matrix& other); Matrix& operator+(const Matrix& other); Matrix& operator=(const Matrix& other); }; void foo() { Matrix a, b, c, d; a = b + c + d; } What functions get called? (Note: There are ways to speed this up while preserving the syntax)
  • 24. Initializer lists Matrix::Matrix(const Matrix& other) : m_n(0), m_a(0) { } Matrix::Matrix(const Matrix& other) { m_n = 0; m_a = 0; } Use initializer list: Assign values inside constructor:
  • 25. Concrete classes • A concrete class – does a single, relatively small thing well and efficiently – hides data members (encapsulation) – provides clean interface – acts like a built-in type – is a “foundation of elegant programming” – Stroustrup • Don’t underestimate the importance of this basic C++/OO feature!
  • 26. Class relationships • OK: – A calls function from B – A creates B – A has a data member of type B • Bad: – A uses data directly from B (without using B’s interface) • Even worse: – A directly manipulates data in B
  • 27. Pointers, arrays, references • Use 0, not NULL (stronger type checking) • Name of array is equivalent to pointer to initial element • Access array using * or []; same efficiency with modern compiler • use std::vector, not built-in array, when possible • Reference is like a pointer
  • 28. References • Reference: alternate name for an object • There is no null reference • No pointer to a reference • No reference to a temporary • Syntax confusing • Basically a const dereferenced pointer with no operations int &a; int a; int& b=a; b++; int* c = &a; int &a = b; int* c= &a; int& a = 1;
  • 29. Argument passing / return • Pass / return by value – calls copy constructor – ok for built-in types int foo(int a) { return 0; } – performance penalty for structs and classes (temporary objects) • Pass / return by reference or pointer – does not call copy constructor – pass inputs by const reference – never pass inputs by “plain” reference void update(int& a); update(2); // error – pass outputs by pointer int x = 1; next(x); // should not change x int x = 1; next(&x); // may change x – ok to return a ref, or const ref
  • 30. C++ function mechanisms • Overloaded function names – Cleaner and safer print(int); print(float); – But beware print(int); print(int*); print(0); • Default parameters void print(int a, int b=0, int c=0); • Operator overloading Matrix& operator+=(const Matrix& other); • Implicit conversion operator operator int() const {} // converts to int – Provides convenient syntax, but potentially dangerous so use sparingly
  • 31. Explicit type conversion • C++ casts – static_cast between 2 related types (int/float, int/enum, 2 pointers in class hierarchy) – reinterpret_cast between 2 unrelated types (int/ptr, pointers to 2 unrelated classes) – const_cast cast away constness – dynamic_cast used for polymorphic types Run-time type info (RTTI) • Avoid casts, but use these instead of C casts – e.g., compiler can perform minimal checking for static_cast, none for reinterpret_cast
  • 32. Namespaces • Namespace expresses logical grouping • using declaration – Don’t use global using except for transition to older code – Ok in namespace for composition – Ok in function for notational convenience • Namespaces are open • Unnamed namespaces restrict code to local translation unit • Aliases
  • 33. Const • Const prevents object from being modified (orig., readonly) • Avoid magic numbers char a[128]; const int maxn = 128; char a[maxn]; • Logical constness vs. physical constness • Const is your friend; use it extensively and consistently • can cast away constness, but be sure to use mutable • const pointers: – const int * const ptr = &a[0]; // const ptr to a const int – int const * const ptr = &a[0]; // ” – int * const p2 = &a[0]; // const ptr to an int – const int * p1 = &a[0]; // ptr to a const int – int const * p2 = &a[0]; // ”
  • 34. Inheritance • Subclass derived from base class • Two classes should pass the “ISA” test: derived class is a base class class Shape { }; class Circle : public Shape { }; • Class hierarchy: means of building classes incrementally, using building blocks (subclass becomes base class for someone else) • Facilitates code reuse
  • 35. Inheritance vs. composition • Inheritance: “is a” class Circle : public Shape { }; • Composition: “has a” class Circle { private: Shape m_shape; }; • Decision should be based on commonality of interface
  • 36. Virtual functions • Function of derived class is called even if you have only a pointer to the base class File.h class Shape { virtual void Draw(); }; class Circle : public Shape { virtual void Draw(); }; File.cpp void Func1() { Circle mycirc; Func2(&mycirc); } void Func2(Shape* s) { s->Draw(); } // calls Circle::Draw()
  • 37. How a virtual function works Shape vtable vfunc1 addr vfunc2 addr ... vfuncN addr vfunc1 addr vfunc2 addr ... vfuncN addr Circle vtable var1 ... varM vtable ptr mycirc
  • 38. What is the penalty of a virtual function? • Space: – one vtable per class with virtual function(s) – one pointer per instance • Time: – one extra dereference if type not known at compile time – no penalty if type known at compile time (ok to inline a virtual function)
  • 39. Pure virtual function • Pure virtual function – Function intentionally undefined – Same penalty as regular virtual function • Abstract class – Contains at least one pure virtual function – Cannot instantiate; must derive from base class and override pure virtual function – Provides an interface (separates interface from implementation) class Shape { virtual void Draw() = 0; };
  • 40. Multiple inheritance • C++ allows you to inherit from multiple base classes class CalculatorWatch : public Calculator, Watch {}; • Works best if – exactly one base class passes ISA test – all other base classes are interfaces • Advanced feature that you will not need in this course
  • 41. Polymorphism • Polymorphism – “ability to assume different forms” – one object acts like many different types of objects (e.g., Shape*) – getting the right behavior without knowing the type – manipulate objects with a common set of operations • Two types: – Run-time (Virtual functions) – Compile-time (Templates)
  • 42. Exceptions • Error handling in C: – Half of code is error handling – Dangerous: Easy for programmer to forget to check return value void Func() { int ret; ret = OpenDevice(); if (ret != 0) error(“Unable to open device”); ret = SetParams(); if (ret != 0) error(“Unable to set params”); }
  • 43. Exceptions (cont.) • Error handling in C++: – try-catch blocks safer – separate “real code” from error handling code void Func() { try { OpenDevice(); SetParams(); } catch (const MyException& e) { e.ReportToUser(); } catch (...) { abort(1); } } void OpenDevice() { if (bad) throw MyException(“Cannot open device”); }
  • 44. Templates • Define a class or function once, to work with a variety of types • Types may not be known until future • Better type checking and faster (cf. qsort) • Specialization can be used to reduce code bloat • Templates support generic programming template<typename T> T Max(T a, T b) { return a>b ? a : b; } template<typename T> class Vector { Vector(int n, T init_val); T* m_vals; };
  • 45. Generic programming • Drawbacks of qsort in <stdlib.h> – requires a compare function, even if trivial – loss of efficiency b/c dereferencing pointer – lost type safety b/c void* – only works with contiguous arrays – no control over construction / destruction / assignment; all swapping done by raw memory moves
  • 46. Standard Template Library (STL) • Containers: – Sequences • vector – array in contiguous memory (replaces realloc) • list – doubly-linked list (insert/delete fast) • deque (“deck”) – double-ended queue • stack, queue, priority queue – Associative • map – dictionary; balanced tree of (key,value) pairs like array with non-integer indices • set – map with values ignored (only keys important) • multimap, multiset (allow duplicate keys) – Other • string, basic_string – not necessarily contiguous • valarray – vector for numeric computation • bitset – set of N bits
  • 47. STL (cont.) • Algorithms (60 of them): – Nonmodifying • find, search, mismatch, count, for_each – Modifying • copy, transform/apply, replace, remove – Others • unique, reverse, random_shuffle • sort, merge, partition • set_union, set_intersection, set_difference • min, max, min_element, max_element • next_permutation, prev_permutation
  • 48. std::string • Example: #include <string> void Func() { std::string s, t; char c = 'a'; s.push_back(c); // s is now “a”; const char* cc = s.c_str(); // get ptr to “a” const char dd[] = "afaf"; t = dd; // t is now “afaf”; t = t + s; // append “a” to “afaf” }
  • 49. std::vector #include <vector> void Func() { std::vector<int> v(10); int a0 = v[3]; // unchecked access int a1 = v.at(3); // checked access v.push_back(2); // append element to end v.pop_back(); // remove last element size_t howbig = v.size(); // get # of elements v.insert(v.begin()+5, 2); // insert 2 after 5th element } • Example:
  • 50. std::vector (cont.) #include <vector> #include <algorithm> void Func() { std::vector<int> v(10); v[5] = 3; // set fifth element to 3 std::vector<int>::const_iterator it = std::find(v.begin(), v.end(), 3); bool found = it != v.end(); if (found) { int three = *it; int indx = it - v.begin(); int four = 4; } } • Example:
  • 51. Iterators • iterator – generalized pointer • Each container has its own type of iterator void Func() { stl::vector<int> v; stl::vector<int>::const_iterator it = v.begin(); for (it = v.begin() ; it != v.end() ; it++) { int val = *it; } }
  • 52. Types of iterators template<class InputIterator, class Type> InputIterator find( InputIterator _First, InputIterator _Last, const Type& _Val ); • Each container provides a different type input forward bidirectional random access output Types
  • 53. Allocators • STL written for maximum flexibility • Each container has an allocator • Allocator is responsible for memory management (new/delete) template < class Type, class Allocator = allocator<Type> > class vector { ... }; • Advice: Ignore allocators
  • 54. Streams • C – flush, fprintf, fscanf, sprintf, sscanf – fgets, getc • C++ – cout, cin, cerr
  • 55. Numerics • valarray – matrix and vector (not std::vector) – slices and gslices • complex • random numbers
  翻译: