introduction to c++

33
Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc. Introduction to C++

Upload: blaise

Post on 23-Feb-2016

22 views

Category:

Documents


0 download

DESCRIPTION

Introduction to C++. Sayed Ahmed Just E.T.C T echnologies Inc. Just E.T.C. Education Inc. C and C++. C++ is based on and an extension to C Knowing similarities and dissimilarities between C and C++ will help to select the right language for your application development. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to C++

Sayed Ahmed

Just E.T.C Technologies Inc.Just E.T.C. Education Inc.

Introduction to C++

Page 2: Introduction to C++

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++

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++

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++

#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++

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++

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++

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++

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++

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

Constant Value in C++

Page 11: Introduction to C++

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++

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++

C++ Compliers Must Support: ANSI Standard

InternationalizationTemplateLocalesNamespaces

Modetrn C++ Compliers

Page 14: Introduction to C++

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++

Constructor no return types called at object creation

Destructor no return type

Constructors and Destructors

Page 16: Introduction to C++

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++

Early Binding/Late BindingAccess Modifiers absent = protected

Irrelevant

Page 18: Introduction to C++

inheritance and destructorsDiamond Problem: Virtual Base ClassOverridingPure Virtual Function

Inheritance and Destructors

Page 19: Introduction to C++

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

fstreambasefstreamifstreamofstream

I/O Stream and File Stream

Page 20: Introduction to C++

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++

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++

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++

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++

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++

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++

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++

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++

ContainersVectors ListsMaps

ContainersSequenceAssociative

Page 29: Introduction to C++

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++

Vector and Array Comparisoncontainer - behave the same way - infinite

Vector and Array Comparison

Page 31: Introduction to C++

Lists: classBidirectional linear sequentially

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

Lists

Page 32: Introduction to C++

Associative ContainerMap templates take two parametersFunctions:

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

Map

Page 33: Introduction to C++

Working with TemplatesImagine a situation - complex + many data

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

Working with Templates