This document provides an introduction to C++ programming, covering key concepts like the workspace, basics of the C++ environment, data types, and operations. It discusses setting up a Microsoft Visual Studio workspace and comments. The basics section explains preprocessor directives, header files, the main function framework, and input/output streams. Data types covered include integers, floating point, Boolean, characters, and enums. Operations explained are conditionals, loops like for and while, and logical/comparison operators. The document aims to familiarize readers with fundamental C++ concepts.
7. โข Comments
-Single-line comment
โข Begin with : // this is comment (Fortran : !)
-Multiple-line comment
โข Begin with : /* โฆ /* this is a multiple line
โข End with : โฆ*/ comment. We still have
this line!*/
โข Preprocessor directives
-Processed by preprocessor before compiling
-Begin with # include <math>
Basics of C++ Environment
7
Adding math library to the project.
8. โข Different type of files in C++
โ Header files (.h)
โ cpp files (.cpp)
โ Most important headers:
๏ง iostream.h
๏ง iomanip.h
๏ง math.h
๏ง cstdlib.h
Basics of C++ Environment(cont.)
Standard input/output
Manipulate stream format
Math library function
Conversion function for
text to number , number to
text, memory allocation,
random numbers and various
other utility functions
8
9. Basics of C++ Environment(cont.)
โข Program Framework
int main()
{
โฆโฆโฆโฆโฆโฆ..
โฆโฆโฆโฆโฆโฆ..
โฆโฆโฆโฆโฆโฆ..
return 0;
}
(Frotran Framework:)
PROGRAM name
โฆโฆโฆโฆโฆโฆ..
โฆโฆโฆโฆโฆโฆ..
โฆโฆโฆโฆโฆโฆ..
END
9
10. โข Some important Syntaxes
โ include<>
โ main()
โ cin>> , cout<<
โข These are like READ , PRINT
โ ; (Semicolon)
โข Shows the end of line . (works the same as in Fortran)
Basics of C++ Environment(cont.)
In next slides we introduce the other syntax symbols, these are most
familiar for any program which we could see in any code
10
11. Basics of C++ Environment(cont.)
Phases of C++ Programs:
1. Edit
2. Preprocess
3. Compile
4. Link
5. Load
6. Execute Loader
Primary
Memory
Program is created in
the editor and stored
on disk.
Preprocessor program
processes the code.
Loader puts program
in memory.
CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
executes.
Compiler
Compiler creates
object code and stores
it on disk.
Linker links the object
code with the libraries,
creates a.out and
stores it on disk
Editor
Preprocessor
Linker
CPU
Primary
Memory
.
.
.
.
.
.
.
.
.
.
.
.
Disk
Disk
Disk
Disk
Disk
11
12. Basics of C++ Environment(cont.)
1 // Printing multiple lines with a single statement
2 #include <iostream>
3
4 // function main begins program execution
5 int main()
6 {
7 std::cout << "Welcome to C++!n";
8
9 return 0; // indicate that program ended successfully
10
11 } // end function main
Comments
Preprocessor directive to include
input/output stream header file
<iostream>
We include this library to use the
std::cout command for printing.
Function main appears
exactly once in every
C++ program..
Special Characters
We use these characters
to write some characters
that can not be written in
normal way:
n Enter
t Tab
backslash itself!
Keyword return is
one of several means to
exit function; value 0
indicates program
terminated successfully.
Welcome to C++!
Output:
12
14. Definition of Types
โข Floating point variables
โ float : 6 digits after floating point
โ double : 12 digits after floating point (more
precision)
All of above types are useful to store real values such as:
0.12E5 Or 2.3212
14
15. Definition of Types(cont.)
โข Integer variables
We can use following words to do some alternative
with int type:
โ short โ unsigned int
โ unsigned short โ long
โ int โ unsigned long
These words change range and starting point of
integer variables :
e.g. short int a; //range -32768 to 32767
15
16. Type Min. range Max. range
short -32768 32767
unsigned short 0 65535
int -2147483648 2147483647
unsigned int 0 4294967295
long -9223372036854775808 9223372036854775807
unsigned long 0 18446744073709551615
Definition of Types(cont.)
16
17. Definition of Types(cont.)
โข bool
โ This type has just two values: (true, false)
โ Note : in Fortran we use LOGICAL and .true. And .false.
combination Instead.
โข char
โ This type is used to store ASCII characters
โ e.g. char a =โQโ;
โข enum
โ It creates a list, in witch every elements has a number
and we can use the elements instead of numbers
(notice to example in next slide)
17
18. Definition of Types(cont.)
E.G. :
If we define such an enum :
enum Day{SAT,SUN,MON,TUE,WED,THU,FRI}
Now if we define a variable from Day type then
this variable can accept the values that define
Inside the Day type such as SAT, SUN, MON,โฆ
e.g. Day mybirthday = MON;
18
19. Definition of Types(cont.)
NOTES:
๏ผvariable precision:
1.2 / 2 returns integer or double?
๏ผCasting:
e.g. : a = (int) c; //a is int and c is double (c was 12.63)
If we didnโt use cast in this example, C++ would
store 12 inside a.
19
20. Definition of Types(cont.)
โข We can have our own types with typedef keyword.
e.g. typedef long double real;
real a; //a is a long double variable now
20
Exactly the same type in C++Type in Fortran
shortINTEGER *2
intINTEGER *4
long intINTEGER *8
floatREAL*4
doubleREAL*8
long doubleREAL*16
charCHARACTER
boolLOGICAL
21. Basics of C++ Environment(cont.)
โข MORE NOTES!
๏ผ we use const command to define constant
variables ( Fortran : PARAMETER )
e.g. const int a = 12;
๏ผ there is no need to write & when we want to
write multiple line commands
๏ผ C++ is a case sensitive language ( vs. Fortran )
21
23. Operations in C++
โข Conditional operands
other operands : < , <= , => , >
23
FortranC++
.AND.&&And
.OR.||Or
.NEQV.!=Not equivalent
.EQV.==Equivalent
24. Operations in C++
โข If (condition) {statement }
โข If , else
if (a==true)
{
b=b+1;
}
else
{
b=b-1;
}
It is important that how compiler ,
compile these operations
24
25. Operations in C++
โข for(init;condition;command) {statement}
for(i=0; i<10; i++)
{
b--; // b=b-1
}
variable i will advance from 0 itself to 9 itself
during the loop
25
26. Operations in C++
โข while(condition) {statement}
while(a)//if a is true
{
b++; // b=b+1
if(b==100)
{
break;
}
}
๏ฑ notes :
๏break: breaks the loop and steps out
๏Ctrl+C: manually breaking the loop!
26
27. Operations in C++(cont.)
โข Variable++ / ++Variable
x=y++ is different from x=++y
โข > , < , => , != , == (Comparisons operand) , =
โข ||, && (Logical operand)
โข condition ? expression1 : expression2
โ if(condition) {expression1 } else {expression2}
โข goto : label
you must know what want to do
exactly otherwise this is very dangerous !
27
28. In future :
i. Pointers and related argues
ii. Functions
iii.Class and related concepts
iv.ERRORS
28
Editor's Notes
Comments : (//)make a line comment wherever it appear until end line
Preprocessor examples : 1.includes 2.defines
Comments : (//)make a line comment wherever it appear until end line
Preprocessor examples : 1.includes 2.defines
Comments : (//)make a line comment wherever it appear until end line
Preprocessor examples : 1.includes 2.defines
Comments : (//)make a line comment wherever it appear until end line
Preprocessor examples : 1.includes 2.defines
Comments : (//)make a line comment wherever it appear until end line
Preprocessor examples : 1.includes 2.defines
Comments : (//)make a line comment wherever it appear until end line
Preprocessor examples : 1.includes 2.defines
Comments : (//)make a line comment wherever it appear until end line
Preprocessor examples : 1.includes 2.defines