Easy C++ Notes
1. Introduction to C++
C++ is a general-purpose programming language created by Bjarne Stroustrup in 1985. It is an extension of
the C programming language and includes object-oriented features.
Features of C++:
- Object-Oriented
- Platform Independent
- Case Sensitive
- Rich Library
- Memory Management
2. Structure of a C++ Program
Every C++ program must have a main() function.
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
Header files: Libraries included using #include.
Easy C++ Notes
Namespace: Allows access to standard C++ library objects.
3. Data Types and Variables
Common Data Types:
- int: Integer numbers
- float: Decimal numbers
- char: Characters
- bool: True/False
- double: Large decimal numbers
Variable declaration:
int age = 21;
float salary = 25000.50;
4. Operators
Types of Operators:
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, >, <, >=, <=
- Logical: &&, ||, !
- Assignment: =, +=, -=, *=, /=
5. Control Flow
Easy C++ Notes
Conditional Statements:
- if, if-else, nested if
- switch
Loops:
- for loop
- while loop
- do-while loop
Example:
for (int i = 0; i < 5; i++) {
cout << i;
6. Functions
Functions are blocks of code designed to perform a task.
Syntax:
returnType functionName(parameters) {
// code
}
Easy C++ Notes
int add(int a, int b) {
return a + b;
main() can call functions: add(3, 4);
7. Arrays and Strings
Array: Collection of similar data types.
int arr[5] = {1, 2, 3, 4, 5};
String: #include <string>
string name = "John";
8. Pointers
A pointer stores the memory address of another variable.
int a = 10;
int* p = &a;
*p accesses value, &a is address.
9. Object-Oriented Programming
Easy C++ Notes
Main OOP Concepts:
- Class & Object
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
class Student {
public:
string name;
int age;
void display() {
cout << name << age;
};
10. Constructors and Destructors
Constructor: Initializes object.
Destructor: Destroys object.
Student() { } // constructor
~Student() { } // destructor
Easy C++ Notes
11. Inheritance
One class inherits properties of another.
class A {
public:
int x;
};
class B : public A {
public:
int y;
};
12. Polymorphism
Function Overloading: Same name, different parameters.
Operator Overloading: Redefining operators.
class Print {
public:
void show(int a) { }
void show(double b) { }
};
Easy C++ Notes
13. File Handling
Used for input/output with files.
#include <fstream>
ofstream file("data.txt");
file << "Hello";
file.close();
ifstream file("data.txt");
file >> data;
14. Exception Handling
Used to handle runtime errors.
try {
// code
} catch (exception &e) {
cout << "Error";
15. Templates
Easy C++ Notes
Generic functions and classes.
template <class T>
T add(T a, T b) {
return a + b;
16. Standard Template Library (STL)
STL contains predefined classes/functions.
Main components:
- Vectors
- Maps
- Sets
- Queues, Stacks
#include <vector>
vector<int> v = {1, 2, 3};
17. Conclusion
C++ is a foundational programming language that prepares learners for advanced topics.
Regular practice and small projects will strengthen understanding.