Professional Documents
Culture Documents
Introduction to C++
Namiq Sultan
University of Duhok
Department of Electrical and Computer Engineerin
Compiler Executable
Machine Code
Program Output:
Programming is great fun!
C++ Programming, Namiq Sultan 3
2.1 Parts of a C++ Program
// sample C++ program comment
#include <iostream> Preprocessor directive
using namespace std; which namespace to use
int main() beginning of function named main
{ beginning of block for main
cout << "Hello, there!"; Output statement
return 0;
String constant
}
send 0 to operating system
end of block for main
Chapter 2 slide 4
Table 2-1 Special Characters
Char Name Description
// double slash Marks the beginning of a comment.
# Pound sign Marks the beginning of a preprocessor
directive
<> Opening and Encloses a filename when used with the
closing brackets #include directive
() Opening and Used in naming a function, as in
closing int main ()
parenthesis
{} Opening and Encloses a group of statements, such as
closing braces the contents of a function.
"" Opening and Encloses a string of characters, such as a
closing message that is to be printed on the
quotation marks screen
; Semicolon Marks the end of a complete
programming statement
Output:
Programming is great fun!
Output:
Programming is great fun!
C++ Programming, Namiq Sultan 8
Program 2-4
// An unruly printing program
#include <iostream>
using namespace std;
int main()
{
cout << "The following items were top sellers";
cout << "during the month of June:";
cout << "Computer games";
cout << "Coffee";
cout << "Aspirin";
return 0;
}
Program Output
The following items were top sellersduring the month of June: Computer
gamesCoffeeAspirin
#include <iostream>
using namespace std;
int main()
{
int value;
value = 5;
cout << "The value is " << value << endl;
return 0;
}
Program Output:
The value is 5
dayOfWeek Legal
_employee_num Legal
june1997 Legal
G o o d L u c k \0
• ‘A’ is stored as 65
• "A" is stored as 65 0
Program output:
Month 2 has 28 days.
#include <iostream>
using namespace std;
int main( ){float shares=220.0; float avgPrice=14.67; cout
<<"There were "<<shares<<" shares sold at $"<<avgPrice<<"
per share.\n"; return 0;}
Program Output
There were 220 shares sold at $14.67 per share.
cout << "There were " << shares << " shares sold at $";
cout << avgPrice << " per share.\n";
return 0;
}
Program Output
There were 220.0 shares sold at $14.67 per share.
C++ Programming, Namiq Sultan 51