Lecture 1: C++ Program
Structure & Basics
1. First C++ Program
Basic Structure:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
Key Points:
Every C++ program must have a main() function.
#include <iostream> is a preprocessor directive.
cout is used for output.
2. Preprocessors in C++
Preprocessors are directives that begin with # and are processed before
compilation.
Common ones:
#include – include files
#define – define macros
#include <iostream> // inserts iostream header
3. Header Files in C++
Lecture 1: C++ Program Structure & Basics 1
Header files contain declarations of standard input/output, strings, math
functions, etc.
Header Use
<iostream> Input/output using cin, cout
<cmath> Math functions like sqrt(), pow()
<string> String class
<vector> Dynamic arrays
#include <cmath>
4. Namespaces in C++
Avoids name conflicts in large projects.
std is the standard namespace.
#include <iostream>
using namespace std;
int main() {
cout << "C++ uses namespaces";
return 0;
}
Without using namespace std; , we write:
std::cout << "Hello";
5. main() Function
Starting point of C++ program.
Must return an int :
int main() {
// code
Lecture 1: C++ Program Structure & Basics 2
return 0;
}
Important:
Returning 0 indicates successful execution.
6. Comments in C++
Used to improve readability and add documentation.
Type Syntax
Single Line // This is a comment
Multi-line /* Comment block */
7. Input and Output in C++
Output: cout
cout << "Output text";
Input: cin
int x;
cin >> x;
Chaining Example:
int a, b;
cin >> a >> b;
cout << "Sum: " << a + b;
8. Tokens in C++
Tokens = smallest individual elements of a program.
Token Type Example
Lecture 1: C++ Program Structure & Basics 3
Keywords int, return, if, for
Identifiers myVar, count, getValue
Constants 10, 3.14, 'a'
Operators +, -, =, ==, &&
Special Symbols (), {}, [], ;
Strings "Hello"
Lecture 1: C++ Program Structure & Basics 4