Introductory C++
What is C++?
• C++ is a high-performance, compiled, and object-oriented programming
language.
• Developed by Bjarne Stroustrup in 1983 as an extension of C.
• Used in game development, system programming, embedded systems, AI,
and more.
• Supports both procedural and object-oriented programming (OOP).
Basic Features of C++
• Fast and efficient (used in performance-critical applications).
• Compiled language (converts code to machine code before execution).
• Supports OOP (Encapsulation, Inheritance, Polymorphism).
• Memory management (allows manual memory allocation).
• Portable (runs on various platforms like Windows, Linux, macOS).
C++ Syntax Basics
Writing a Basic C++ Program
#include <iostream> // Include input-output library
using namespace std;
int main() {
cout << "Hello, C++!" << endl; // Print to console
return 0; // Exit program
}
• #include <iostream> → Includes the standard input/output library.
• using namespace std; → Allows using cout, cin, etc., without std::.
• int main() → The main function, the entry point of a C++ program.
• cout << "Hello, C++!"; → Outputs text to the console.
Comments in C++
// This is a single-line comment
/*
This is a
multi-line comment
*/
Variables & Data Types
int age = 25; // Integer
double price = 19.99; // Floating-point
char grade = 'A'; // Character
bool isCplusplusFun = true; // Boolean
string name = "Alice"; // String (requires <string> library)
Type Example Size
int 25 4 bytes
double 19.99 8 bytes
char 'A' 1 byte
bool true/false 1 byte
string "Hello" Varies
Input and Output
#include <iostream>
using namespace std;
int main() {
string name;
cout << "Enter your name: "; // Output message
cin >> name; // Take input
cout << "Hello, " << name << "!" << endl;
return 0;
}
• cout → Prints output.
• cin → Takes user input.
Operators in C++
Operator Example Meaning
+ a+b Addition
- a-b Subtraction
* a*b Multiplication
/ a/b Division
% a%b Modulus
== a == b Equal to
!= a != b Not equal
Conditional Statements
int age = 20;
if (age >= 18) {
cout << "You are an adult." << endl;
} else {
cout << "You are a minor." << endl;
}
Loops in C++
For Loop
for (int i = 0; i < 5; i++) {
cout << "Iteration: " << i << endl;
}
While Loop
cpp
CopyEdit
int count = 0;
while (count < 5) {
cout << count << endl;
count++;
Functions in C++
#include <iostream>
using namespace std;
// Function definition
void greet() {
cout << "Hello!" << endl;
int main() {
greet(); // Function call
return 0;
}
Function with Parameters & Return Value
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
int main() {
cout << "Sum is: " << add(5, 3) << endl;
return 0;
Arrays in C++
int numbers[3] = {10, 20, 30};
cout << numbers[0]; // Output: 10
Object-Oriented Programming (OOP) in C++
Defining a Class & Object
#include <iostream>
using namespace std;
class Car {
public:
string brand = "Toyota";
void honk() {
cout << "Beep! Beep!" << endl;
};
int main() {
Car myCar; // Create object
cout << myCar.brand << endl;
myCar.honk();
return 0;
}
• Class → Defines a blueprint.
• Object → Instance of a class.
Example Program: Simple Calculator
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
cout << "Sum: " << a + b << endl;
return 0;