Programming Language C++ Xulong Peng CSC415 Programming Languages
Outline History Design, Syntax and Semantics Evaluation Code demonstration
History of C++ C++ was developed from C Written by Dr. Bjarne Stroustrup from Bell Labs in 1979 “C and classes” and then named C++ Standardized by ISO in C++98, C++03, C++11 and C++14 New features: OOP, addition safety, operator overloading, templates, exception handling
Name, Binding and Scopes Name(Identifiers) Letter, digits and underscore case-sensitive Keywords(i.e. int, while, using, for, namespace) Binding Static binding and dynamic binding C++ supports OOP and implements dynamic binding
Name, Binding and Scopes (continues) C++’s scopes Local scope Class scope Namespace scope Global scope
Data Type (C++ is a typed language) numeric types (integer, floating and double) Boolean (true and false) character void type pointer (*pt) array User-defined (struct, enum, class)
Data Type Conversion Narrowing conversion (double -> float) Widening conversion (int -> float) Implicit or explicit conversion
Expression and Assignment Statements C++ provides a rich set of operators unary, binary and ternary operators ++, --, +, - and ? : support % operator (only apply to integer) relational operators operator associativity and precedence
Associativity and Precedence (source: Deitel and Deitel, 2012)
Assignment Statements Expression int count = 10; Compound statements One or more individual statements in {} Control statements If …else, loop and switch
Control Structure Sequence Statement Selection Statement Iterative (or loop) Statement C++ provides break, continue, goto and return to alter the flow of control
Selection Statements Selection statements offer a choice between two or more execution. If statement If…else statement Switch statement
Repetition Statements for (counter init; condition; counter update) While (condition expression){statements} do { statements to execute} while (condition); C++11 also provide a rang-based for loop
Functions C++ uses prototype as a function declaration Function definition: typeName functionName (parameter list) { statements; return;}
Functions C++ parameters can be passed by: value, reference or const-reference Example: double avg (const vector & arr, int n, bool & errorFlag); An inline function to speed up execution Function overloading template
C++’s OOP features Encapsulation Supports build-in data type and user-define ADT(class) Inheritance Provides single and multiple inheritance Polymorphism Supports dynamic binding
Exception Handling try … catch block In C++, an exception is either a user or a library defined class Must be explicitly raised using throw
Concurrency Original C++ standard only supports single thread programming C++11 adds concurrency in the form of C++11 model C++11 uses a thread library for starting and managing threads
Evaluation of C++ Readability C++ is a large language which complicates its readability User defined operator overloading (aids and harms) Supports numeric as Boolean expression affects readability. Case sensitive, good control structures, more flexible iteration statements helps readability Use of OOP improves its readability and writability
Evaluation of C++ Writability Its writability is highly associated with its readability C++ has rich data type and supports abstraction improves writability Efficient expression helps writability Flexible and efficient control statements C++’s template improves writability
Evaluation of C++ Reliability It is reliable if used carefully flexibility and efficiency vs. reliability (not strongly typed and does not have strict type checking) The use of pointer type is very flexible but could cause dangling pointer and memory leaking Abstraction and information hiding improves its reliability Exception handling improves its reliability
Evaluation of C++ Cost Hard to learn as it is a large language. The cost of writing C++ depends on applications. Generally, it is easy to write as syntax is very straightforward. Backward compatible with C (low cost for C programmer) Cheaper or free IDE reduces cost for training Inexpensive compliers decrease the cost of compilation and execution Flexibility and efficiency speed up execution The cost on reliability and maintenance with trade-offs.
Some Code Examples
Shape 2 D 3 D Circle Square Sphere Cube
Q & A