This document provides an introduction to C++ programming concepts including variables, data types, constants, and I/O streams. It discusses basic C++ code structure and comments. Integral data types like char, bool, and integers are explained. Floating point types float and double are also introduced. The document demonstrates declaring and initializing variables as well as basic math operations on variables. Constants and the const keyword are described along with the #define preprocessor directive.
7. Resources
â˘Books
âDeitelâC++ How to program
âThe C++ Programming Language (Not for complete starters)
â˘Bunshof good websites
âwww.cplusplus.com
âwww.msdn.com
â˘msdnawesome library
â˘stackoverflow.comand googleare always your best programming buddies
8.
9. What You Will Learn
â˘Concepts you already know
âVariables
âControl Structure
âFunctions
âArrays/ Pointers/ Strings
âStructs
â˘New concepts
âClasses (OOP)
âInheritance (OOP)
âPolymorphism (OOP)
âTemplate
âSTL
âException Handling
âFile Processing
10. Programming Languages War
August 2014, Source: http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
12. C++ Preferences & features
â˘Mooreâs Law
âComputer processing power can be doubled every 18-24 months
âSoftware with Hardware improves together
â˘More complicated Hardware
â˘More advanced Software
âNow, we are in generation that we put the "Mooreâs Law" behind us!
13. C++ Preferences & features
â˘Two kind of Computer programming paradigms:
âImperative
â˘Procedural(Pascal, C, php, etc)
â˘Object-Oriented(C++, C#, Java, ASP.NET, etc)
âDeclarative
â˘Functional(Pascal, C, php, etc)
â˘Logic(Prolog, etc)
____________________________________________________________________________
* Note the need to C++ after inventing C
** Note that the Procedural Functional programming paradigms are still used till our present time and has its unique features
14. C C++
â˘The "C" Programming language is a modular one
â˘Why C++ then?
âObjects, OOP
âC C++ are portables ones
â˘Comparison:
âC: is action Oriented
â˘Procedural
âC++: is object oriented
â˘Compiler checking
â˘Extensible language
âClass
â˘Reusable
16. Where you can find C++? Pretty Everywhere! (Take a look here: http://www.stroustrup.com/applications.html) Microsoft, OfficeGoogleHPAmazonAdobeMozillaMySQLINTELNokiaSunBloombergGame Engines
21. Structure of C++ program
#include<iostream>
voidmain ()
{
}
#include<iostream>
intmain ()
{
// indicate successful termination
return0;
}
#include<iostream>
usingnamespace::std; // Note the new namespace
voidmain()
{
cout<< "we're having fun!";
}
#include<iostream>
usingnamespace::std;// Note the new namespace
intmain ()
{
cout<< "we're having fun!";
return0; // indicate successful termination
}
22. Structure of C++ program
#include<iostream>
usingnamespace::std; // Note the namespace
voidmain()
{cout<< "we're having fun!";}
#include<iostream>
usingnamespace::std;// Note the namespace
intmain ()
{
cout<< "we're having fun!";return0;
}
#include<iostream>
usingnamespace::std;
voidmain()
{
cout<< "We're having fun!";
cout<< "I need to eat!:D ";
}
#include<iostream>
usingnamespace::std;
voidmain()
{
cout<< "we're having fun!" << endl;
cout<< "I need to eat!:D " << endl;
}
We're having fun!Ineed to eat!:D
we're having fun!
I need to eat!:D
23. Structure of C++ program
#include<iostream>
usingnamespace::std;
voidmain()
{
cout<< "we're having fun!"<< "n"<< "I need to eat!:D "
<< "n";
}
#include<iostream>
usingnamespace::std;
voidmain()
{
cout<< "we're having fun! n I need to eat!:D n";
}
we're having fun!
I need to eat!:D
we're having fun!
I need to eat!:D nis the same as endl
24. Comments
// comment
For a one line
/*
comments
*/
For one line or more (multi lines)
25. Structure of C++ program
#include<iostream>
usingnamespace::std;
voidmain()
{
cout<< "foo"; // this is a line!
}
#include<iostream>
usingnamespace::std;
voidmain()
{
cout<< "foo"; /* this is a line! */
}
Compile and run
Compile and run
26. Structure of C++ program
#include<iostream>
usingnamespace::std;
voidmain()
{
cout<< "foo"; // this is
a line!
}
#include<iostream>
usingnamespace::std;
voidmain()
{
cout<< "foo"; /* this is not a single
line! */
}
Compiler error
Compile and run
30. float, double, long double
C++ data types
Structured
Simple
Address
Pointer
Reference
enum
Floating
Array
Struct
Union
Class
Char, Short, int, long, bool
Integral
36. Variables
// Operating with variables
#include <iostream>
usingnamespacestd;
intmain ()
{
// declaring variables:
inta, b;
intresult;
// process:
a = 5; b = 2; a = a + 1;
result = a -b;
// print out the result:
cout<< result;
// terminate the program:
return0;
}
40. Float, double, long double
C++ data types
Structured
Simple
Address
Pointer
Reference
enum
Floating
Array
Struct
Union
Class
Char, Short, int, long, bool
Integral
42. Integral
â˘char, short, int, long, bool
â˘char
âUsed to represent character such as:
â˘Letters
â˘Digits
â˘Special symbols
â' + ', ' & ', ' $ ', ' * '
âEach character is enclosed with single quote mark ' ' and not double ones " "
âSpace is represented by ' ' with space between them.
âASCII & EBCDIC
â˘bool
âWatch out that the "Boolean" type is an integral one!
âbool
â˘false = 0, true = any other number
43. integral
#include<iostream>
usingnamespace::std;
voidmain()
{
charc1 = 'd', c2;
cout<< c2 << c1 << endl;
}
voidmain()
{
boolb1, b2;
if(3 <= 2)
{
b1 = false;
b2 = true;
}
else
{
b1 = 53;
}
cout<< b1 << " -"<< b2 << endl;
}
d
1 -0
Note that:
â˘The default value for a char is NULLrepresented as SPACEâ â but itâs not a space, itâs a NULL!
â˘The char has ' ' and not " "
Note that:
â˘A numeric value is printed when printing a "boolean"
â˘The default value for booltype is false (0)
44. Floating point data types
â˘float, double
âfloat 4 bytes / double 8 bytes
â˘float has a single precision
â˘double has a double precision
âdouble = long double (in new compilers)
âThe size of float, double, long double are machine dependent.
48. Constants
â˘A Constant:
âAny expression that has a âfixedâ value
â˘3 kind of constants:
â˘Integer Numbers
â˘Floating-Point Numbers
â˘Characters & Strings
49. Constants
â˘Integer Numbers
â1225// Decimal
â-982// Decimal
â05356// Octal!
â˘Octal numbers are preceded by 0
â0x3c// Hexadecimal
â˘Hexadecimal numbers are preceded by 0x
â˘Floating Numbers
âDecimal
âExponent
Examples:
â5.0// 5.0 (double)
â5.0f// 5.0 (float)
â45.556779// 45.556779
â8.36e18// 8.36x 10^18
â8.36e-18// 8.36x 10^-18
â˘Characters and Strings
â'Z' //Char âSingle Character
â'M' //Char âSingle Character
â"Whereâs the cat?"//String â Several Character
â"I just donât know!"//String â Several Character
ââcâ //String âOne Character
50. integral
#include<iostream>
usingnamespace::std;
#definePI 3.14;// No â=â Sign
#defineMyTab'tâ// No â=â Sign
#definePonPon":Dâ// No â=â Sign
voidmain()
{
}
#include<iostream>
usingnamespace::std;
voidmain()
{
#definePI 3.14;
#defineMyTab't'
#definePonPon":Dâ
}
51. integral
#include<iostream>
usingnamespace::std;
#defineMyTab't'
voidmain()
{
#definePI 3.14;
floatRadius = 0;
cout<< "Enter the Radius"<< endl;
cin>> Radius;
floatCircle = PI;
Circle = Circle * 2 * Radius;
cout<< "The perimeter of the Circle = â<< Circle
<< MyTab;
}
Enter the Radius
3.2
The perimeter of the Circle = 20.096 Press any key to continue
52. integral
#include <iostream>
using namespace::std;
#define MyTab't'
#define PI 5;
void main()
{
#define PI 3.14;
float Radius = 0;
cout<< "Enter the Radius" << endl;
cin>> Radius;
float Circle = PI;
Circle = Circle * 2 * Radius;
cout<< "The perimeter of the Circle = â<< Circle
<< MyTab;
}
Enter the Radius
3.2
The perimeter of the Circle = 20.096 Press any key to continue
53. integral
#include<iostream>
usingnamespace::std;
voidmain()
{
#definePI 3.14;
floatRadius = 0;
cout<< "Enter the Radius"<< endl;
cin>> Radius;
floatCircle = 2 * PI * Radius;
cout<< "The perimeter of the Circle = "<< Circle;
}
Illegal Indirection 2 * PI * Radius
54. integral
#include<iostream>
usingnamespace::std;
constintx = 20;
voidmain()
{
constinty = 90;
}
Compile and run
#include<iostream>
usingnamespace::std;
constintx = 20;
voidmain()
{
consty = 90;
}
2005 Compiler: Compile & Run
intassumed for consttype when neglecting the type
constcharMe = 'M';
constintHeight = 5;
constcharMyCharTab= 't';// Char tab
constchar*MyStringTab= "t"; // String tab
voidmain()
{
cout<< MyStringTab;
}
Press any key to continue
constcharMyCharTab= 't';// Char tab
constchar*MyStringTab= "t";// String tab
#defineMyStringTab"t"; // String tab
voidmain()
{
cout<< MyStringTab;
}
Press any key to continue
55. Code Cracking
#include<iostream>
usingnamespace::std;
voidmain()
{
cout<<"I'm number 4 or 77, I don't know :D"<<endl;
;
;
;
;
system("pause");
}
I'm number 4 or 77, I don't know :D
56. Related Online CoursesProgramming ParadigmsC++ Memory Management. LISP and Pythonhttp://see.stanford.edu/see/courseinfo.aspx?coll=2d712634-2bf1-4b55-9a3a-ca9d470755eeProgramming methodology -Javahttp://see.stanford.edu/see/courseinfo.aspx?coll=824a47e1-135f-4508-a5aa-866adcae1111
57. Take a look at my other courses, Especially the GUI Course, C++.NEThttp://www.slideshare.net/ZGTRZGTR/
58. Keep in touch and letâs connect
http://www.mohammadshaker.com
mohammadshakergtr@gmail.com
https://twitter.com/ZGTRShaker@ZGTRShakerhttps://de.linkedin.com/pub/mohammad-shaker/30/122/128/
http://www.slideshare.net/ZGTRZGTR
https://www.goodreads.com/user/show/11193121-mohammad-shaker
https://plus.google.com/u/0/+MohammadShaker/
https://www.youtube.com/channel/UCvJUfadMoEaZNWdagdMyCRA
http://mohammadshakergtr.wordpress.com/