Learning C++ - Introduction to c++ programming 1
-
Upload
aliaminian -
Category
Engineering
-
view
243 -
download
3
Transcript of Learning C++ - Introduction to c++ programming 1
![Page 1: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/1.jpg)
1
Introduction To C++ ProgrammingAli Aminian & Bardia Nezamzadeh
![Page 2: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/2.jpg)
2
Section Outline
• Workspace• Basics of C++ Environment• Definition of Types• Operations in C++
![Page 3: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/3.jpg)
3
Section Outline
• Workspace• Basics of C++ Environment• Definition of Types• Operations in C++
![Page 4: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/4.jpg)
4
Workspace
• One of the most famous workspaces is “Microsoft Visual Studio”
• Creating a project :lets follow these pictures :
![Page 5: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/5.jpg)
5
Workspace(cont.)
![Page 6: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/6.jpg)
6
Section Outline
• Workspace• Basics of C++ Environment• Definition of Types• Operations in C++
![Page 7: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/7.jpg)
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
Adding math library to the project.
![Page 8: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/8.jpg)
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 fortext to number , number to text, memory allocation, random numbers and various other utility functions
![Page 9: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/9.jpg)
9
Basics of C++ Environment(cont.)
• Program Frameworkint main() { ………………..………………..……………….. return 0;
}
(Frotran Framework:)PROGRAM name………………..………………..……………….. END
![Page 10: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/10.jpg)
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
![Page 11: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/11.jpg)
11
Basics of C++ Environment(cont.)Phases of C++ Programs:
1. Edit2. Preprocess3. Compile4. Link5. Load6. Execute Loader
PrimaryMemory
Program is created inthe editor and storedon disk.
Preprocessor programprocesses the code.
Loader puts programin memory.
CPU takes eachinstruction andexecutes it, possiblystoring new datavalues as the programexecutes.
CompilerCompiler createsobject code and storesit on disk.
Linker links the objectcode with the libraries,creates a.out andstores it on disk
Editor
Preprocessor
Linker
CPU
PrimaryMemory
.
.
.
.
.
.
.
.
.
.
.
.
Disk
Disk
Disk
Disk
Disk
![Page 12: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/12.jpg)
12
Basics of C++ Environment(cont.)1 // Printing multiple lines with a single statement2 #include <iostream>3 4 // function main begins program execution5 int main()6 {7 std::cout << "Welcome to C++!\n";8 9 return 0; // indicate that program ended successfully10 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 CharactersWe 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:
![Page 13: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/13.jpg)
13
Section Outline
• Workspace • Basics of C++ Environment• Definition of Types• Operations in C++
![Page 14: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/14.jpg)
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
![Page 15: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/15.jpg)
15
Definition of Types(cont.)
• Integer variablesWe can use following words to do some alternative with int type:– short – unsigned int– unsigned short – long– int – unsigned longThese words change range and starting point of
integer variables :e.g. short int a; //range -32768 to 32767
![Page 16: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/16.jpg)
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.)
![Page 17: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/17.jpg)
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 numberand we can use the elements instead of numbers(notice to example in next slide)
![Page 18: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/18.jpg)
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 defineInside the Day type such as SAT, SUN, MON,…
e.g. Day mybirthday = MON;
![Page 19: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/19.jpg)
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.
![Page 20: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/20.jpg)
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
Exactly the same type in C++ Type in Fortran
short INTEGER *2
int INTEGER *4
long int INTEGER *8
float REAL*4
double REAL*8
long double REAL*16
char CHARACTER
bool LOGICAL
![Page 21: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/21.jpg)
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 )
![Page 22: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/22.jpg)
22
Section Outline
• Workspace • Basics of C++ Environment• Definition of Types• Operations in C++
![Page 23: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/23.jpg)
23
Operations in C++
• Conditional operands
other operands : < , <= , => , >
Fortran C++.AND. && And
.OR. || Or
.NEQV. =! Not equivalent
.EQV. == Equivalent
![Page 24: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/24.jpg)
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
![Page 25: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/25.jpg)
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
![Page 26: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/26.jpg)
26
Operations in C++
• while(condition) {statement}while(a)//if a is true{
b++; // b=b+1if(b==100){
break;}
} notes :
break: breaks the loop and steps outCtrl+C: manually breaking the loop!
![Page 27: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/27.jpg)
27
Operations in C++(cont.)
• Variable++ / ++Variablex=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 !
![Page 28: Learning C++ - Introduction to c++ programming 1](https://reader030.fdocuments.us/reader030/viewer/2022021500/587b4b3b1a28abff1a8b4c49/html5/thumbnails/28.jpg)
28
In future :
i. Pointers and related arguesii. Functionsiii. Class and related conceptsiv. ERRORS