Complete C++ Notes (Basic to Advanced)
1. Introduction to C++
C++ is an object-oriented programming language developed by Bjarne Stroustrup.
It is an extension of the C language.
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, C++!";
return 0;
}
2. Data Types in C++
- int: Integer (e.g., int x = 5;)
- float: Decimal number (e.g., float y = 3.2;)
- char: Single character (e.g., char c = 'A';)
- bool: Boolean value (e.g., bool flag = true;)
- string: Text (e.g., string name = "Titu";)
3. Input & Output
Example:
#include <iostream>
using namespace std;
int main() {
int a;
cout << "Enter a number: ";
cin >> a;
cout << "You entered: " << a;
return 0;
}
4. Operators
Arithmetic: +, -, *, /, %
Relational: ==, !=, >, <, >=, <=
Logical: &&, ||, !
5. Conditional Statements
Complete C++ Notes (Basic to Advanced)
Example:
if (a > b) {
cout << "A is greater";
} else {
cout << "B is greater";
}
6. Loops
For Loop:
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}
While Loop:
int i = 1;
while (i <= 5) {
cout << i << " ";
i++;
}
7. Arrays
int arr[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
8. Functions
int add(int a, int b) {
return a + b;
}
int main() {
cout << add(4, 5); // Output: 9
}
9. Call by Value vs Reference
Call by Value:
void show(int x) {
x = x + 10;
Complete C++ Notes (Basic to Advanced)
Call by Reference:
void show(int &x) {
x = x + 10;
}
10. OOP Concepts
Class & Object Example:
class Student {
public:
int roll;
void display() {
cout << "Roll = " << roll;
}
};
int main() {
Student s;
s.roll = 10;
s.display();
}
Constructor & Destructor
class Demo {
public:
Demo() {
cout << "Constructor called";
}
~Demo() {
cout << "Destructor called";
}
};
11. Inheritance
class A {
public:
void showA() { cout << "Class A"; }
};
class B : public A {
public:
void showB() { cout << "Class B"; }
Complete C++ Notes (Basic to Advanced)
};
12. File Handling
#include <fstream>
ofstream fout("file.txt");
fout << "Hello File!";
fout.close();
13. Exception Handling
try {
throw 10;
} catch (int x) {
cout << "Exception caught: " << x;
}
14. Templates
template <class T>
T add(T a, T b) {
return a + b;
}