This document provides an overview of key concepts in C++ including:
- Similarities and differences between C and C++ and how that impacts language selection
- Common C++ concepts like namespaces, functions, variables, statements, and data types
- Object-oriented programming principles in C++ like encapsulation, inheritance, and polymorphism
- Additional C++ features like templates, the standard template library (STL), streams, and exceptions handling
The document is intended as a high-level introduction and reference for C++ concepts.
2. ο 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. ο 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. ο 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. ο #include <iostream>
ο using namespace std;
ο int main() {
ο int studentId;
ο count << "what is your student ID" << endl;
ο cin>> studentId;
ο count << "Student Id " << studentId << endl;
ο }
6. ο 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. ο To store values
ο case sensitive
ο must start with letter
ο Supportted datatypes: char, int, float, double, boolean,
wchar_t
ο float: usually 32 bits - 6 digit after decimam,
ο double: 64 bits - 10 digit after decimal,
8. ο 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. ο 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. ο in c, define XYZ 100
ο in c++, const int val=1000
11. ο 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
13. ο C++ Compliers Must Support: ANSI Standard
ο Internationalization
ο Template
ο Locales
ο Namespaces
14. ο 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. ο Constructor
ο no return types
ο called at object creation
ο Destructor
ο no return type
16. ο 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
20. ο 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
ο 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
ο 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 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()
25. ο 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. ο 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. ο 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
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
ο container - behave the same way - infinite
33. ο 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