15-(3-13) C++ Basics
15-(3-13) C++ Basics
Software Engineering I
David O. Johnson
Spring 2025
Step 1:
• Write the code for a program (source code) using an editor such
as vi or nano (language sensitive), save as file hello.cpp
#include <iostream>
int main ( ) {
std::cout << "Hello, world!\n";
return 0
}
Step 2:
• Compile the program to convert from the source code to an
“executable” or “binary” (or object code):
Step 3:
• If the compiler produces any errors, fix them and recompile
Step 4:
• Once there are no programming errors and you have
executable code, run it:
$ hello.exe
Hello, world!
program.exe
• We can write:
cout instead of std::cout
cin instead of std::cin
endl instead of std::endl
• The most common types are: char, int, float, and double
• Strings are arrays of characters
• Declare a variable before you use it
No exponent operator!!
Beware of division:
• If second argument is an integer, the result will
be an integer (floor – rounded toward zero):
9 / 10 = 0 whereas 9 / 10.0 = 0.9
• Division by 0 will cause a run-time error
== equal to
< less than
& bitwise AND
<= less than or equal
| bitwise OR
> greater than
^ bitwise XOR
>= greater than or equal
~ bitwise NOT
!= not equal
<< bitwise shift left
&& logical AND
>> bitwise shift right
|| logical OR
! logical NOT
Left-Associative:
• Operators are evaluated from left to right.
• For instance, in a + b - c, addition and subtraction, being left-associative, will first
evaluate a + b, and then subtract c from the result.
Right-Associative:
• Although less common in C++, some operators are right-associative, meaning they
are evaluated from right to left.
• An example is the assignment operator =.
• In a = b = c, c is assigned to b, and then the resulting value of b is assigned to a.
int main () {
ofstream myfile ("example.txt"); File example.txt:
if (myfile.is_open())
{ This is a line.
myfile << "This is a line.\n"; This is another line.
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Grading Level
Exceeds
Question Points Meets Expectations Unsatisfactory
Expectations
(80-89%) (0-79%)
(90-100%)
The bit value is
The bit value is
incorrect, but the
1a, b, c, d, e, f 14 correct and the Otherwise
work shows it is a
work is shown.
minor error
Code is basically
2 16 Code is correct correct, but contains Otherwise
a minor syntax error