C++ was created by Bjarne Stroustrup in 1979 as an extension to the C language. It adds object-oriented features while maintaining the speed and low-level memory control of C.
Let's look at the classic "Hello, World!" program in C++ to understand its basic structure.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}#include <iostream>: This is a preprocessor directive. It tells the compiler to include the standard input/output stream library, which allows us to print to the screen.using namespace std;: This tells the compiler to use the standard (std) namespace. Without this, we would have to write std::cout and std::endl.int main(): This is the main function where the execution of the program begins. It returns an integer (int).cout << "Hello, World!" << endl;: cout (character output) is used to print text. << is the insertion operator. endl inserts a newline character and flushes the output stream.return 0;: This terminates the main() function and returns the value 0 to the operating system, signaling that the program executed successfully.C++ is a strongly typed language, meaning every variable must be declared with a specific data type before it can be used.
int age = 20; // Integer (whole number)
float pi = 3.14f; // Floating point number (decimals)
double precisePi = 3.14159265; // Double precision floating point
char letter = 'A'; // Single character
bool isPassed = true; // Boolean (true or false)C++ uses streams for input and output operations.
cout (Console Output): Used to print data.cin (Console Input): Used to read data from the keyboard.#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age; // Reads an integer from the user
cout << "You are " << age << " years old." << endl;
return 0;
}main() function.<iostream> library is required for standard console input (cin) and output (cout).int, float, char).std namespace contains all the standard C++ library components.The entry point of every C++ program where execution begins.
The header file that provides input and output capabilities, such as cin and cout.
A declarative region that provides a scope to the identifiers inside it, used to prevent name conflicts (e.g., std namespace).
Test your understanding with 3 questions
What is the standard entry point of a C++ program?
Which header file is required to use std::cout and std::cin?
What does 'using namespace std;' do?
2 Modules
2 Modules