1 / 33

Introduction to C++

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.

blaise
Download Presentation

Introduction to C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. C++ Program Elements • Comments: • Header File: #include <iostream> • Namespace: A defined collection of symbols and variable names within an application • The main () function: must have in a program • Parentheses: A function/code block name is followed by Parentheses () • Braces: Boundary of code blocks

  4. 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 semicolon • Function 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.

  5. How does a C++ program look like? • #include <iostream> • using namespace std; • int main() { • intstudentId; • count << "what is your student ID" << endl; • cin>> studentId; • count << "Student Id " << studentId << endl; • }

  6. C++ Namespace • A C++ program must have a namespace; • C program ends with .c • where C++ program ends with .cpp • Information communication to and from c program is treated as stream. • cout and cin are used for the purpose • cout and cin are practically objects

  7. C++ Variables • To store values • case sensitive • must start with letter • Supportteddatatypes: char, int, float, double, boolean, wchar_t • float: usually 32 bits - 6 digit after decimam, • double: 64 bits - 10 digit after decimal,

  8. Variable Scope • Can be declared anywhere • Local: • inside functions • Static: • Inside functions but do not get destroyed after • Global: • Declared outside the functions • can be accessed from anywhere in the program • Formal/Parameter • Hidden 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

  9. Array: • Group of variable • A group of variables of the same type • in c strings are null terminated arrays • C++ also supports a predefined string class/data type; • include <string>

  10. Constant Value in C++ • in c, define XYZ 100 • in c++, const int val=1000

  11. OOP Concepts • Enables Code Resuse • Enables Data Security from Unauthorized Access • Object: • Attribute + behaviors • Primary 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

  12. Object Oriented Paradigm: • Object Oriented Paradigm: • Define objects - toughest • define messages • define properties • --define attributes • --define behaviors • Object behavior analysis: • Understand the application • Derive Objects • Categorize Objects • Model process flow

  13. Modetrn C++ Compliers • C++ Compliers Must Support: ANSI Standard • Internationalization • Template • Locales • Namespaces

  14. Friend Class • Friend Class • Friend functions and inline functions provide faster, and efficient application • A function can be friend to any number of functions • Use function declaration to declare friend functions • Static Data Members and Static Members • Function - create the common members of classes (across objects)

  15. Constructors and Destructors • Constructor • no return types • called at object creation • Destructor • no return type

  16. Operator Overloading • Operator Overloading • Compile time polymorphism • runtime poly • - inheritance • virtual functions • Operator Overloading: • -unary • -binary • -arithmetic • -assignment • Note: C++ does not support overloading based on return type

  17. Irrelevant • Early Binding/Late Binding • Access Modifiers absent = protected

  18. Inheritance and Destructors • inheritance and destructors • Diamond Problem: Virtual Base Class • Overriding • Pure Virtual Function

  19. I/O Stream and File Stream • manipulators • COUT, setw, right, setfill, flush, endl • fstreambase • fstream • ifstream • ofstream

  20. C++ I/O and File Streams • Mode of file opening • ios::in • ios::out • Sscanf • Read formatted file data • ifstream • fin.getLine() • Random File Reading • seekg • seekp

  21. I/O System • I/O System • Buffered file system • Stream classes • stream • istream--_IO_istream_withassign • ostream • Iostream • Cin is an instance of _IO_istream_withassign • Instance of _IO_ostream_withassign • cout • cerr

  22. Stream formatting flags • Stream formatting flags • Can be used with setf(), unsetf() • ios::left • ios::dec • ios::fixed • ios::oct • ios::right • ios::scientific • ios::showbase • ios::showpoint • ios::showpos • ios::uppercase

  23. Unformatted I/O Operations • Unformatted input/output • character • character array • strings • use text buffer • Can provide abstraction of the I/O device • functions for unformatted input and output • cin.read() • cin.gcount() • cout.write()

  24. File Operations • Files • fstreambase • opening, manipulating, closing • fstream, ifstream, ofstream • mode ios::app, ios::ate, ios::binary, ios::in, ios::out, ios::trunc • fin.getline() • Get pointers • Random Operation Put pointers • pointers • Seekg() • Seekp() • Tellg() • Tellp()

  25. Handling Exceptions • 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

  26. Virtual Functions and Run Time Poymorphism • runtime with virtual functions • shape • -draw • rect • tri • define overriding by placing function in each • Show why virtual function needed? • virtual base • pointer runtime polymorphism • pure virtual function • Late binding • base point to base - call derive function - runtime-dynamic

  27. Standard Template Library • Templates create reusable code • STL provides some foundational items • --templatized functions and • --classes • These can be used to solve a variety of problems • Some STL Elements • Containers • Algorithms • Iterators • STLs are compatible across operating systems

  28. Containers • Vectors • Lists • Maps • Containers • Sequence • Associative

  29. Algorithms • function templates work with container templates of any type • algorithm header file • Iterators are objects to cycle through the containers • iterator header file needs to be included if iterators are used • Iterator type: • Random Access • Bidirectional • Forward • Input • Output

  30. Vector and Array Comparison • Vector and Array Comparison • container - behave the same way - infinite

  31. Lists • Lists: class • Bidirectional linear sequentially • Functions • begin() • end() • erase() • insert(); (template) • push_back(); • push_front(); • remove(); • splice();

  32. Map • Associative Container • Map templates take two parameters • Functions: • begin(); • clear(); • count(); • empty(); • end(); • erase(); • insert(); • size()

  33. Working with Templates • Working with Templates • Imagine a situation - complex + many data type • Overload may help but time consuming • template can help • object creation define data type • template overloading

More Related