Date post: | 19-Dec-2015 |
Category: |
Documents |
View: | 223 times |
Download: | 3 times |
CIS 101: Computer Programming and Problem Solving
Lecture 8Usman Roshan
Department of Computer Science
NJIT
C++
• I recommend two textbooks for C/C++– The C Programming Language by Kernighan
and Ritchie (Creators of C)– The C++ Programming Language by
Stroustrup (Creator of C++)
• We will learn C++ syntax and solve problems on quizzes and mid-term using C++.
• We will also learn how to use C++ classes
MATLAB vs C++• Variables
– MATLAB: everything is an array– C++: integers, floats, char, array, classes
• Memory allocation– MATLAB: EASY! nothing to do– C++:
• More work; must define variable before using it (we will see examples in the lab)• Also have to free up allocated space (MATLAB automatically does it for you)
• Pointers– MATLAB: no pointers– C++: gives programmer more flexibility to optimize programs, but can introduce
dangerous bugs! (if there is time we will see pointers)• Loops and if constructs
– Similar: that is good news!• Running programs
– MATLAB: easy, just click on run button!– C++: must compile first, and then execute (we will see later what this means)
So why C++?
• With MATLAB and C++ there is a safe and easy programming environment vs efficiency (time and memory) tradeoff
• MATLAB is easy but can be slow (it can also produce nice plots and graphics)
• C++ is more flexible and can be much faster• NOTE: MATLAB is reasonably fast (as some
benchmarks show) and continues to improve
Interpreter vs compiler
• Interpreter– Used in MATLAB– Read-check-execute loop – Slow but good for learning– Other examples are Perl and Java (Java compilers are also
available now)
• Compiler– C++, C, Pascal, Fortran– Checks program syntax and translates it into machine code for
the processor– Machine code can then run on the processor– Stand-alone (doesn’t require interpreter) and usually much faster
C++ basics
Main function is executed whenyou run the program. (Later we willsee how to pass it parameters)
C++ basics
cout is an operator used to display output to the screen.
cout syntax: cout << <variable> << <variable> << …;
Compiling a C++ program
Click on Build and then Compile.this will now translate this sourcecode into machine code, whichis the CPU language.
Building a C++ program
Now we build. This means linking thecompiled file (which is in machine code)with other machine code required to runthe program on the CPU.
For example when we #include other filesthen their machine code is linked with machinecode for this file. The end-result is one largefile containing machine code for the CPU.This is also called the executable file.
C++ program output
Average of 4 and 0.5 = (4+0.5)/2 = 4.5/2 = 2.25. This says 0 because we specifiedthe variable type as int and not float. So the 0.5 (the second number) gets floored to 0.
Let’s fix the C++ program:using floats now
Now we are asking for floats(real numbers, not necessarily integers)
C++ arrays
• Size cannot be modified unless dynamic variables are defined (we will look at that later). These are static variables. This means their size is fixed.
• In MATLAB you can add to the array and it will automatically allocate (and free up) space in memory.
C++ if and for loops
• Very similar to MATLAB, except no end is required.
• Statements must be grouped within { and }.
C++ loops
Specify an array of size 3
Loop construct(have to stay within3 otherwise you willget an error)Similar to MATLABexcept for parenthesis
C++ loops
Specify an array of size 3
Loop construct(have to stay within3 otherwise you willget an error)
Loop statements are enclosed within { and }
C++ loops and if
If-else construct:Similar to MATLAB exceptthat (1) conditional expressionis inside paranthesisand (2) { } are used to enclosestatements to execute ifcondition is true (or false)