Introduction to C++
description
Transcript of Introduction to C++
![Page 1: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/1.jpg)
Sayed Ahmed
Just E.T.C Technologies Inc.Just E.T.C. Education Inc.
Introduction to C++
![Page 2: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/2.jpg)
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++
![Page 3: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/3.jpg)
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
![Page 4: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/4.jpg)
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
![Page 5: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/5.jpg)
#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?
![Page 6: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/6.jpg)
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
![Page 7: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/7.jpg)
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
![Page 8: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/8.jpg)
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
![Page 9: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/9.jpg)
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:
![Page 10: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/10.jpg)
in c, define XYZ 100in c++, const int val=1000
Constant Value in C++
![Page 11: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/11.jpg)
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
![Page 12: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/12.jpg)
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:
![Page 13: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/13.jpg)
C++ Compliers Must Support: ANSI Standard
InternationalizationTemplateLocalesNamespaces
Modetrn C++ Compliers
![Page 14: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/14.jpg)
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
![Page 15: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/15.jpg)
Constructor no return types called at object creation
Destructor no return type
Constructors and Destructors
![Page 16: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/16.jpg)
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
![Page 17: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/17.jpg)
Early Binding/Late BindingAccess Modifiers absent = protected
Irrelevant
![Page 18: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/18.jpg)
inheritance and destructorsDiamond Problem: Virtual Base ClassOverridingPure Virtual Function
Inheritance and Destructors
![Page 19: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/19.jpg)
manipulators COUT, setw, right, setfill, flush, endl
fstreambasefstreamifstreamofstream
I/O Stream and File Stream
![Page 20: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/20.jpg)
Mode of file openingios::inios::out
SscanfRead formatted file data
ifstreamfin.getLine()
Random File Reading seekgseekp
C++ I/O and File Streams
![Page 21: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/21.jpg)
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
![Page 22: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/22.jpg)
Stream formatting flags Can be used with setf(), unsetf()ios::leftios::decios::fixedios::octios::rightios::scientificios::showbaseios::showpointios::showposios::uppercase
Stream formatting flags
![Page 23: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/23.jpg)
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
![Page 24: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/24.jpg)
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
![Page 25: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/25.jpg)
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
![Page 26: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/26.jpg)
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
![Page 27: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/27.jpg)
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
![Page 28: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/28.jpg)
ContainersVectors ListsMaps
ContainersSequenceAssociative
![Page 29: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/29.jpg)
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
![Page 30: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/30.jpg)
Vector and Array Comparisoncontainer - behave the same way - infinite
Vector and Array Comparison
![Page 31: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/31.jpg)
Lists: classBidirectional linear sequentially
Functionsbegin()end()erase()insert(); (template)push_back();push_front();remove();splice();
Lists
![Page 32: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/32.jpg)
Associative ContainerMap templates take two parametersFunctions:
begin();clear();count();empty();end();erase();insert();size()
Map
![Page 33: Introduction to C++](https://reader036.fdocuments.us/reader036/viewer/2022062812/56816428550346895dd5e76c/html5/thumbnails/33.jpg)
Working with TemplatesImagine a situation - complex + many data
typeOverload may help but time consumingtemplate can helpobject creation define data typetemplate overloading
Working with Templates