C++ full code notes
suvathikan
1. Intro to C++
Developed by Bjarne Stroustrup
Extension of C with object-oriented features
File extension: .cpp
Basic Template:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
2. Variables & Data Types
int age = 13;
float price = 99.99;
char grade = 'A';
bool isSmart = true;
string name = "Suvathikan";
Rules: No space, no starting with numbers, no special symbols except _.
3. Input & Output
cin >> age; // Input
cout << age; // Output
4. Operators
Arithmetic: + - * / %
Assignment: = += -= *=
Comparison: == != > < >= <=
Logical: && || !
5. Conditional Statements
if (age > 18) {
cout << "Adult";
} else {
cout << "Kid";
}
Next code :
switch(choice) {
case 1: cout << "Start"; break;
case 2: cout << "Exit"; break;
default: cout << "Invalid";
6. Loops
for (int i = 0; i < 5; i++) {
cout << i;
int i = 0;
while (i < 5) {
cout << i;
i++;
}
do {
cout << i;
i++;
} while (i < 5);
⃣ Functions
cpp
int add(int a, int b) {
return a + b;
✅ Call it: add(5, 10);
⃣ Arrays
cpp
int marks[3] = {90, 80, 70};
cout << marks[0];
⃣ Strings
cpp
string name = "Suvathikan";
cout << name.length();
✅ Use <string> header.
✅ Pointers
cpp
int x = 10;
int* ptr = &x;
cout << *ptr; // Dereference = value
✅ Object-Oriented Programming (OOP)
➤ Class & Object
cpp
class Car {
public:
string brand;
void start() {
cout << "Vroom!";
};
Car c1;
c1.brand = "BMW";
c1.start();
➤ Constructor
cpp
Edit
class Student {
public:
Student() {
cout << "Constructor called!";
};
➤ Inheritance
cpp
class Animal {
public:
void speak() {
cout << "Sound";
};
class Dog : public Animal {};
Dog d;
d.speak();
➤ Polymorphism
cpp
Copy
Edit
class A {
public:
void show() { cout << "A"; }
};
class B : public A {
public:
void show() { cout << "B"; }
};
B b;
b.show(); // Output: B
✅ File Handling
cpp
Copy
Edit
#include <fstream>
ofstream myFile("data.txt");
myFile << "Hello!";
myFile.close();
✅ Advanced Stuff (Quick Glimpse)
STL (Standard Template Library): vector, map, set
Exception Handling: try { ... } catch(...) { ... }
Namespaces
Friend functions
Virtual functions
#. Pro Tips:
Practice in CodeBlocks, Replit, or VS Code
Use #include for extra features like <vector>, <cmath>
C++ is all about logic, efficiency, and clean structure