Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc. Introduction to C++
Dec 26, 2015
C++ is based on and an extension to CKnowing similarities and dissimilarities
between C and C++ will help to select the right language for your
application development
C and C++
Comments: Header File: #include <iostream>Namespace: A defined collection of symbols
and variable names within an applicationThe main () function: must have in a programParentheses: A function/code block name is
followed by Parentheses ()Braces: Boundary of code blocks
C++ Program Elements
Variables: Used to contain values. Think about variables in Mathematics
Statements: An instruction to the computer hardware to perform specific action.
Statement Terminator: a semicolonFunction calls:
One function can call another function. It's just a transfer of control from one code
block to another code block. However, the control comes back to the caller
again.
C++ Program Elements
#include <iostream>using namespace std;int main() { int studentId; count << "what is your student ID" <<
endl; cin>> studentId; count << "Student Id " << studentId <<
endl;}
How does a C++ program look like?
A C++ program must have a namespace;C program ends with .c where C++ program ends with .cppInformation communication to and from c
program is treated as stream. cout and cin are used for the purposecout and cin are practically objects
C++ Namespace
To store valuescase sensitivemust start with letterSupportted datatypes: char, int, float, double,
boolean, wchar_tfloat: usually 32 bits - 6 digit after decimam, double: 64 bits - 10 digit after decimal,
C++ Variables
Can be declared anywhereLocal:
inside functionsStatic:
Inside functions but do not get destroyed after Global:
Declared outside the functions can be accessed from anywhere in the program
Formal/ParameterHidden variable:
if a local variable use the same name of a global variable the local variable = hidden variable
use scope operator :: to access the global variable inside that function
Variable Scope
Group of variableA group of variables of the same typein c strings are null terminated arraysC++ also supports a predefined string
class/data type;include <string>
Array:
Enables Code ResuseEnables Data Security from Unauthorized AccessObject:
Attribute + behaviorsPrimary OOP Concepts
Encapsulation: Inheritance:
- reduce development time - reuse -increase maintainability of the code
Polymorphism: assign different uses to an entity based onthe context
Abstraction: simple representation of an objet. hide not essential attributes and behaviors
OOP Concepts
Object Oriented Paradigm:Define objects - toughestdefine messagesdefine properties --define attributes --define behaviors
Object behavior analysis:Understand the applicationDerive ObjectsCategorize ObjectsModel process flow
Object Oriented Paradigm:
C++ Compliers Must Support: ANSI Standard
InternationalizationTemplateLocalesNamespaces
Modetrn C++ Compliers
Friend ClassFriend functions and inline functions provide
faster, and efficient applicationA function can be friend to any number of
functionsUse function declaration to declare friend
functionsStatic Data Members and Static Members
Function - create the common members of classes (across objects)
Friend Class
Constructor no return types called at object creation
Destructor no return type
Constructors and Destructors
Operator OverloadingCompile time polymorphism runtime poly
- inheritance virtual functions
Operator Overloading:-unary-binary-arithmetic-assignmentNote: C++ does not support overloading based
on return type
Operator Overloading
inheritance and destructorsDiamond Problem: Virtual Base ClassOverridingPure Virtual Function
Inheritance and Destructors
manipulators COUT, setw, right, setfill, flush, endl
fstreambasefstreamifstreamofstream
I/O Stream and File Stream
Mode of file openingios::inios::out
SscanfRead formatted file data
ifstreamfin.getLine()
Random File Reading seekgseekp
C++ I/O and File Streams
I/O SystemBuffered file systemStream classes
streamistream--_IO_istream_withassignostreamIostream
Cin is an instance of _IO_istream_withassignInstance of _IO_ostream_withassign
cout cerr
I/O System
Stream formatting flags Can be used with setf(), unsetf()ios::leftios::decios::fixedios::octios::rightios::scientificios::showbaseios::showpointios::showposios::uppercase
Stream formatting flags
Unformatted input/output character character array strings
use text bufferCan provide abstraction of the I/O device
functions for unformatted input and outputcin.read()cin.gcount()cout.write()
Unformatted I/O Operations
Files fstreambase
opening, manipulating, closing fstream, ifstream, ofstream
mode ios::app, ios::ate, ios::binary, ios::in, ios::out, ios::truncfin.getline()
Get pointersRandom Operation Put pointers
pointersSeekg()Seekp()Tellg()Tellp()
File Operations
Handling Exceptions runtime errors Exceptions
try and catch block throw statement Uncaught Exceptions Multiple Catch ---------- identified by operating systems if not handled passed to op system exception as int, char ,class
strings how it passes through functions Derived Class Exceptions catch block hierarchy place derived classes catch up
Handling Exceptions
runtime with virtual functionsshape -drawrect tri
define overriding by placing function in eachShow why virtual function needed?virtual basepointer runtime polymorphismpure virtual functionLate bindingbase point to base - call derive function - runtime-
dynamic
Virtual Functions and Run Time Poymorphism
Templates create reusable codeSTL provides some foundational items--templatized functions and--classesThese can be used to solve a variety of problemsSome STL Elements
ContainersAlgorithmsIterators
STLs are compatible across operating systems
Standard Template Library
Algorithms function templates work with container templates of any
typealgorithm header file
Iterators are objects to cycle through the containers iterator header file needs to be included if iterators are
usedIterator type:
Random AccessBidirectionalForward InputOutput
Lists: classBidirectional linear sequentially
Functionsbegin()end()erase()insert(); (template)push_back();push_front();remove();splice();
Lists
Associative ContainerMap templates take two parametersFunctions:
begin();clear();count();empty();end();erase();insert();size()
Map