Top Banner
Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc. Introduction to C++
33
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

Sayed Ahmed

Just E.T.C Technologies Inc.

Just E.T.C. Education Inc.

Introduction to C++

Page 2: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

#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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

in c, define XYZ 100in c++, const int val=1000

Constant Value in C++

Page 11: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

C++ Compliers Must Support: ANSI Standard

InternationalizationTemplateLocalesNamespaces

Modetrn C++ Compliers

Page 14: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

Constructor no return types called at object creation

Destructor no return type

Constructors and Destructors

Page 16: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

Early Binding/Late BindingAccess Modifiers absent = protected

Irrelevant

Page 18: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

inheritance and destructorsDiamond Problem: Virtual Base ClassOverridingPure Virtual Function

Inheritance and Destructors

Page 19: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

manipulators COUT, setw, right, setfill, flush, endl

fstreambasefstreamifstreamofstream

I/O Stream and File Stream

Page 20: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

Mode of file openingios::inios::out

SscanfRead formatted file data

ifstreamfin.getLine()

Random File Reading seekgseekp

C++ I/O and File Streams

Page 21: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

ContainersVectors ListsMaps

ContainersSequenceAssociative

Page 29: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

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: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

Vector and Array Comparisoncontainer - behave the same way - infinite

Vector and Array Comparison

Page 31: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

Lists: classBidirectional linear sequentially

Functionsbegin()end()erase()insert(); (template)push_back();push_front();remove();splice();

Lists

Page 32: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

Associative ContainerMap templates take two parametersFunctions:

begin();clear();count();empty();end();erase();insert();size()

Map

Page 33: Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.

Working with TemplatesImagine a situation - complex + many data

typeOverload may help but time consumingtemplate can helpobject creation define data typetemplate overloading

Working with Templates